I want to use Java Stream to run over a POJO list, such as List<A> below, and convert it to Map Map<String, Set<String>> .
For example, class A:
class A { public String name; public String property; }
I wrote the code below that collects values ββinto a map Map<String, String> :
final List<A> as = new ArrayList<>(); // the list as is populated ... // works if there are no duplicates for name final Map<String, String> m = as.stream().collect(Collectors.toMap(x -> x.name, x -> x.property));
However, since there may be several POJOs with the same name , I want the map value to be Set . All property Lines for the same name key must go into the same set.
How can I do that?
// how do i create a stream such that all properties of the same name get into a set under the key name final Map<String, Set<String>> m = ???
java java-8 java-stream
tkja
source share