Reputation: 13
Given a Set<String>
, how do I use Java streams to create a Set<Map>
where each string s
becomes a map with a single key/value pair with key "x"
and value s
?
Something like this, but I need one more level of collect in there somewhere:
set.stream().collect(Collectors.toMap(p->"x", v->v))
Upvotes: 0
Views: 115
Reputation: 393821
You can use Collections.singletonMap to map each String
to a Map<String,String>
. Then you can collect the Map
s into a Set
:
Set<Map<String,String>> mset = set.stream()
.map(s -> Collections.singletonMap("x",s))
.collect(Collectors.toSet());
Upvotes: 1