7

I have three Objects as below :

public class A {
     private int a;
     private ...
     private B b;
    //getters setters for B
}

public class B {
     private String name;
     private ... 
     private C c;
    //getters setters for C
}

public class C {
     private Long id;
     private ... 
}

I'm having a List<A> with B's Object in every A and C's Object In every B.

I want to group A list by C.id in a map Map<C,List<A>>

How do I achieve this using groupingBy method of Stream?

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
iamP
  • 307
  • 1
  • 3
  • 13

1 Answers1

8
Map<Long, List<A>> resultSet = 
       myList.stream()
             .collect(Collectors.groupingBy(e -> e.getB().getC().getId()));

edit:

Since you want a Map<C, List<A>> as the receiver type of the result set then you'll need to override equals and hashcode in your C class like this:

class C {
     public Long getId() {
         return id;
     }

     @Override
     public boolean equals(Object o) {
         if (this == o) return true;
         if (o == null || getClass() != o.getClass()) return false;

         C c = (C) o;

         return id != null ? id.equals(c.id) : c.id == null;
     }

     @Override
     public int hashCode() {
         return id != null ? id.hashCode() : 0;
     }

     private Long id;
}

Now, you can do:

Map<C,List<A>> resultSet = 
     myList.stream()
           .collect(Collectors.groupingBy(e -> e.getB().getC()));

This means all A objects that have the same C.id will be in the same bucket (i.e group).

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • I made an edit , I actually need Object of C in map not Id, Its Map> and not Map> but I want to group by C.id – iamP Mar 11 '18 at 13:44
  • @iamP _I want to group A list by C.id in a map Map>_, well if you group by C.id then you cannot have `Map>`. do you want to group by `C` then? you can override `equals` and `hashcode` in `C` then group by `C`. – Ousmane D. Mar 11 '18 at 13:46
  • I can group by C, but I am not sure if Objects with same C.id will be equal – iamP Mar 11 '18 at 13:48