jOOQ / jOOL

jOOλ - The Missing Parts in Java 8 jOOλ improves the JDK libraries in areas where the Expert Group's focus was elsewhere. It adds tuple support, function support, and a lot of additional functionality around sequential Streams. The JDK 8's main efforts (default methods, lambdas, and the Stream API) were focused around maintaining backwards compatibility and implementing a functional API for parallelism.
http://www.jooq.org/products
Apache License 2.0
2.08k stars 167 forks source link

Grouped and groupBy gives different results when you sort by Enum field. #325

Closed NikitaPanteleev closed 6 years ago

NikitaPanteleev commented 6 years ago

Expected behavior and actual behavior:

https://stackoverflow.com/questions/48787686/org-jooq-lambda-seq-different-behaviour-of-grouped-and-groupby-functions

Grouped and groupBy gives different results when you use them with java Enum field. I created collection from 4 elements and I expect them to be grouped into 2 groups

Steps to reproduce the problem:

import org.jooq.lambda.Seq;
import org.jooq.lambda.tuple.Tuple2;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class Application {
  public static void main(String[] args) {
    List<Foo> foos = Arrays.asList(
        new Foo().setStatus(StatusInfo.ORDER_PLACED).setOrderNo(1L),
        new Foo().setStatus(StatusInfo.ORDER_PLACED).setOrderNo(2L),
        new Foo().setStatus(StatusInfo.ORDER_PLACED).setOrderNo(3L),
        new Foo().setStatus(StatusInfo.ORDER_CANCELLED).setOrderNo(4L)
    );
    List<Tuple2<String, Seq<Foo>>> list1 = Seq.seq(foos)
        .grouped(foo -> {
          System.out.println("Foo1: " + foo + " - " + foo.getStatus().toString());
          return foo.getStatus().toString();
        }).toList();

    Map<String, List<Foo>> list2 = Seq.seq(foos).groupBy(foo -> {
      System.out.println("Foo2: " + foo);
      return foo.getStatus().toString();
    });

    System.out.println("Foos size: " + foos.size());
    System.out.println("Size 1: " + list1.size());
    System.out.println("Size 2: " + list2.keySet().size());
  }

  static class Foo {
    StatusInfo status;
    Long orderNo;

    public StatusInfo getStatus() {
      return status;
    }

    public Foo setStatus(StatusInfo status) {
      this.status = status;
      return this;
    }

    public Long getOrderNo() {
      return orderNo;
    }

    public Foo setOrderNo(Long orderNo) {
      this.orderNo = orderNo;
      return this;
    }
  }

  public enum StatusInfo {
    ORDER_PLACED, ORDER_CANCELLED;
  }
}

Output:

Foo1: Application$Foo@57ad2aa7 - ORDER_PLACED
Foo1: Application$Foo@5b3f61ff - ORDER_PLACED
Foo1: Application$Foo@1ef6d34c - ORDER_PLACED
Foo2: Application$Foo@57ad2aa7
Foo2: Application$Foo@5b3f61ff
Foo2: Application$Foo@1ef6d34c
Foo2: Application$Foo@6fa34d52
Foos size: 4
Size 1: 1
Size 2: 2

Sizes of both List and keySet should be the same, but it looks like the last element is skipped in the first method.

Versions:

NikitaPanteleev commented 6 years ago

Might be duplicated of https://github.com/jOOQ/jOOL/issues/271

Is there any solution for current release?

NikitaPanteleev commented 6 years ago

Not sure that library is still maintained. So, for now, I build the latest master: https://bintray.com/nikitapanteleev/jOOL/jOOL/0.9.13

lukaseder commented 6 years ago

Thank you so much for your patience @NikitaPanteleev. This library is maintained, yes. Indeed, release 0.9.13 is overdue. I'm sorry for the inconvenience.

I'll look into whether this is a duplicate, soon.

lukaseder commented 6 years ago

Thanks again for your patience. I can confirm this seems to be a duplicate of #271. I'll close this and proceed with releasing 0.9.13 today.