1

I have following situation. (pseudocode)

class A {
  id;
  List<B> bs;
}

class B {}

I wonder how to convert List os As -> Map of Bs

List<A> as;

// the Map key is A.id (Map<A.id, List<B>>)
Map<Integer, List<B>> bs = as.stream()
                       .map(a ->a.getBs())
                       .collect(// I dont know what to add here ???);
shmosel
  • 49,289
  • 6
  • 73
  • 138
Tomas Marik
  • 4,053
  • 3
  • 31
  • 62

1 Answers1

3

Seems like you want sometime like this:

 Map<Integer, List<B>> bs = as.stream()
        .collect(Collectors.toMap(A::getId, A::getBs));
senerh
  • 1,315
  • 10
  • 20
Eugene
  • 117,005
  • 15
  • 201
  • 306