-2

I have a class A which has some fields.

Class A{
 String type;
 String x;
 String y;
}

Class B{
    String x;
    String y;

}

Let's say we have a list List<A>. By using Collectors.groupingBy() , is it possible to get output Map<String,List<B>> instead Map<String,List<A>> ? where key in the Map is type field in Class A.

1
  • 1
    Collectors.groupingBy(..., Collectors.mapping(....)) Commented May 13, 2019 at 10:35

1 Answer 1

1

Of course - just chain a mapping() collector to the groupingBy() collector.

Map<String,List<B>> map =
    listA.stream()
         .collect(Collectors.groupingBy(A::getType,
                                        Collectors.mapping(a->new B(a.getX(),a.getY()),
                                                           Collectors.toList())));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.