Grouping a list of main objects by their nested object's id

Given the following class A :

public class A {     private int id; //this field is unique     private int a_1; //this field ain't unique     private String a_2;     private String a_3;          //setters + getters } 

We can group a list of random objects of type A as follow by their respective a_1 :

Map<String, List<A>> sortedmap = list.stream()                 .collect(Collectors.groupingBy(A::getA_1, Collectors.toList())); 

Now given two classes B and C such as :

public class B{     private int id; //this field is unique for all entities of type B     private String b_1;     private C b_C; //many objects of type B may have a reference to the same object of type C          //setters + getters }  public class C{     private int id; //this field is unique for all entities of type C     private String c_1;     private D c_D; //many objects of type C may have a reference to the same object of type D          //setters + getters }  public class D{     private int id; //this field is unique for all entities of type D     private String d_1;     private String d_2;          //setters + getters } 

How can we sort a list of random objects of type B by their respective b_C.getC_D().getId() field?

Add Comment
2 Answer(s)

You can group the results in a TreeMap which is ordered by default:

public static void main(String[] args) {     D d1 = new D();d1.setId(1);     D d2 = new D();d2.setId(2);          C c1 = new C();c1.setC_D(d1);     C c2 = new C(); c2.setC_D(d2);          B b1 = new B(); b1.setB_C(c1); b1.setId(1);     B b2 = new B(); b2.setB_C(c2); b2.setId(2);     B b3 = new B(); b3.setB_C(c2); b3.setId(3);          Map<String, List<B>> result = Arrays.asList(b1,b2,b3)             .stream()             .collect(Collectors.groupingBy(b -> ""+b.getB_C().getC_D().getId(),                      TreeMap::new,                       Collectors.toList()));              System.out.println(result); } 

The result is:

{1=[B [id=1]], 2=[B [id=2], B [id=3]]} 

PS. @javaistaucheineinsel nice username 😉

Add Comment

I’d add custom getters to support this (not compiled, forgive typos):

public class B{     private int id; //this field is unique for all entities of type B     private String b_1;     private C b_C; //many objects of type B may have a reference to the same object of type C          //setters + getters      public int getBCDId() {         return b_C.getCDId();     } }  public class C{     private int id; //this field is unique for all entities of type C     private String c_1;     private D c_D; //many objects of type C may have a reference to the same object of type D          //setters + getters      public int getCDId() {         return c_D.getId();     } } 

Now you can sort and/or group B objects by the value they return from their getBCDId methods. Just like you would do for B’s own attributes.

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.