Max Power
Max Power

Reputation: 13

How do I use Java streams to convert a set of strings to a set of maps?

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

Answers (1)

Eran
Eran

Reputation: 393821

You can use Collections.singletonMap to map each String to a Map<String,String>. Then you can collect the Maps into a Set :

Set<Map<String,String>> mset = set.stream()
                                  .map(s -> Collections.singletonMap("x",s))
                                  .collect(Collectors.toSet());

Upvotes: 1

Related Questions