我想List<E>从一个Map<String,List<E>>(E是一个随机类)中提取一个stream().
我想要一个使用java 8流的简单单行方法.
我到现在为止尝试过的事情:
HashMap<String,List<E>> map = new HashMap<>();
List<E> list = map.values(); // does not compile
list = map.values().stream().collect(Collectors.toList()); // does not compile
Run Code Online (Sandbox Code Playgroud) 我想知道如何List<D>从HashMap<E,R>考虑这些约束中提取a :
E 是一个自定义类;R是包含Set<D>自定义对象的自定义类;在之前的情况下,我有一个简单的Map<E,List<R>>,但在这种情况下,我必须访问R具有目标的类Set<D>.
我想在代码的以下部分中做的是获取Set<D>其国家/地区名称等于给定参数的元素.
我尝试过使用相同的解决方案:
Map<E,R> map = new HashMap<E,R>();
public List<D> method(String countryname) {
return map.values().stream().filter((x)->{
return x.getSet().stream().anyMatch((t) -> {
return t.getCountry().equals(countryname);
});
})
.map(R::getSet)
.flatMap(List::stream)
.collect(Collectors.toList()); //does not compile
}
// the R class
class R {
private Set<D> set = new TreeSet<D>();
//getters & setters & other attributes
}
Run Code Online (Sandbox Code Playgroud)