I have a class like this:
public class A {
private List<String> stringList;
private String someString;
}
I have a list of these objects like so:
List<A> list = //some method to generate list
I want to conver this to a Map<String, String>
where each string in the stringList
maps to the same someString
value (like a multimap). How can I do this using java 8 stream?
I could convert this to a flat map like so:
list.stream.flatMap(....
But I'm not sure where to go from there.
Try this.
Map<String, String> r = list.stream()
.flatMap(a -> a.stringList.stream()
.map(k -> new AbstractMap.SimpleEntry<>(k, a.someString)))
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));