转换地图<;对象,映射<;对象、列表<;对象>&燃气轮机&燃气轮机;设置<;对象>;使用Java8
我有一个map转换地图<;对象,映射<;对象、列表<;对象>&燃气轮机&燃气轮机;设置<;对象>;使用Java8,java,java-8,java-stream,Java,Java 8,Java Stream,我有一个mapmap,我想把它转换成Set,字符串是使用Java8的请求类中的id 请求类 class Request{ private String id; public void setId(String id){ this.id =id; } public String getId(){ return this.id; } } 我知道做这件事的传统方法,但希望使用Java8选项(流、映射、收集…)来实现这一点 我试过了,发现了编译错误 Set<Str
map
,我想把它转换成Set
,字符串是使用Java8的请求类中的id
请求类
class Request{
private String id;
public void setId(String id){
this.id =id;
}
public String getId(){
return this.id;
}
}
我知道做这件事的传统方法,但希望使用Java8选项(流、映射、收集…)来实现这一点
我试过了,发现了编译错误
Set<String> values = map.values().stream()
.map(dateMap -> dateMap.values().stream()
.map(request -> request.stream()
.map(Request::getId)).flatMap(Set::stream).collect(Collectors.toSet()));
Set values=map.values().stream()
.map(dateMap->dateMap.values().stream())
.map(请求->请求.stream()
.map(Request::getId)).flatMap(Set::stream).collect(collector.toSet());
感谢首先,从映射值创建一个流,并将
map
操作链接到映射值,然后使用flatMap
连续展平,最后将map
映射到请求ID并收集到设置的实现
Set<String> resultSet =
map.values()
.stream()
.map(Map::values)
.flatMap(Collection::stream)
.flatMap(Collection::stream)
.map(Request::getId)
.collect(Collectors.toSet());
设置结果集=
map.values()
.stream()
.map(映射::值)
.flatMap(集合::流)
.flatMap(集合::流)
.map(请求::getId)
.collect(收集器.toSet());
向我们展示您的尝试,我们不是代码交付service@LukeGarrigan,我错过了,现在又加了一句。接近但不完全。你能看看如何解决你的问题吗?。还请注意,注释是带有/
的,与“\\\”相对。另外,我认为您应该收集到集合实现,而不是列表实现。@Aominè我认为我的回答中有几个错误。事实上,我不擅长通过智能手机编写代码
Set<String> resultSet =
map.values()
.stream()
.map(Map::values)
.flatMap(Collection::stream)
.flatMap(Collection::stream)
.map(Request::getId)
.collect(Collectors.toSet());