[英]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
? 给定一个Set<String>
,我如何使用Java流创建一个Set<Map>
,其中每个字符串s
变成一个具有键"x"
和值s
键/值对的映射?
Something like this, but I need one more level of collect in there somewhere: set.stream().collect(Collectors.toMap(p->"x", v->v))
像这样的东西,但是我需要在某个地方再收集一个级别: set.stream().collect(Collectors.toMap(p->"x", v->v))
You can use Collections.singletonMap to map each String
to a Map<String,String>
. 您可以使用Collections.singletonMap将每个String
映射到Map<String,String>
。 Then you can collect the Map
s into a Set
: 然后,您可以将Map
收集到Set
:
Set<Map<String,String>> mset = set.stream()
.map(s -> Collections.singletonMap("x",s))
.collect(Collectors.toSet());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.