I have a list of objects of class A:
List<A> list; class A { String name; String lastname; //Getter and Setter methods } I want to convert this list to a map from name to a set of lastnames:
Map<String, Set<String>> map; For example, for the following list:
John Archer, John Agate, Tom Keinanen, Tom Barren, Cindy King
The map would be:
John -> {Archer, Agate}, Tom -> {Keinanen, Barren}, Cindy -> {King}
I tried the following code, but it returns a map from name to objects of class A:
list.stream.collect(groupingBy(A::getFirstName, toSet()));