用Java8流中的流来收集映射值的Hashmap 让我们考虑一个HASMAP Map<Integer, List> id1 = new HashMap<Integer,List>();
但我不知道如何将列表检索为这个流操作的输出 问题2)我想再次对hashmap中的键应用一个过滤条件,并检索相应的列表列表用Java8流中的流来收集映射值的Hashmap 让我们考虑一个HASMAP Map<Integer, List> id1 = new HashMap<Integer,List>();,java,hashmap,java-8,collectors,Java,Hashmap,Java 8,Collectors,但我不知道如何将列表检索为这个流操作的输出 问题2)我想再次对hashmap中的键应用一个过滤条件,并检索相应的列表列表 例如:这里我的查询是key=1%(即key可以是1,10,15),输出应该是'list1','list2','list3'(列表列表列表)。如果您确定最多只能得到一个通过过滤器的元素(由过滤器保证),您可以使用findFirst: Optional<List> o = id1.entrySet() .stream()
例如:这里我的查询是key=1%(即key可以是1,10,15),输出应该是'list1','list2','list3'(列表列表列表)。如果您确定最多只能得到一个通过过滤器的元素(由过滤器保证),您可以使用
findFirst
:
Optional<List> o = id1.entrySet()
.stream()
.filter( e -> e.getKey() == 1)
.map(Map.Entry::getValue)
.findFirst();
可选o=id1.entrySet()
.stream()
.filter(e->e.getKey()==1)
.map(map.Entry::getValue)
.findFirst();
在一般情况下,如果筛选器可能匹配多个列表,则可以将它们收集到列表列表中:
List<List> list = id1.entrySet()
.stream()
.filter(.. some predicate...)
.map(Map.Entry::getValue)
.collect(Collectors.toList());
List List=id1.entrySet()
.stream()
.filter(…某些谓词…)
.map(map.Entry::getValue)
.collect(Collectors.toList());
您需要做的是从映射.entrySet()创建流
:
要从中获取值,请.map()
:
如果只有一个条目,请改用该条目(注意:此代码假定有一个值;否则,请使用.orElse()
;有关详细信息,请参阅):
//流-->可选-->V
.findFirst().get()
对于您的第二季度,您的问题已经有了答案。对于Q1,更一般地说,当您知道键的筛选应该给出唯一的值时,根本不需要使用流
只需使用get
或getOrDefault
,即:
List<String> list1 = id1.getOrDefault(1, Collections.emptyList());
List list1=id1.getOrDefault(1,Collections.emptyList());
您也可以这样做
public Map<Boolean, List<Student>> getpartitionMap(List<Student> studentsList) {
List<Predicate<Student>> allPredicates = getAllPredicates();
Predicate<Student> compositePredicate = allPredicates.stream()
.reduce(w -> true, Predicate::and)
Map<Boolean, List<Student>> studentsMap= studentsList
.stream()
.collect(Collectors.partitioningBy(compositePredicate));
return studentsMap;
}
public List<Student> getValidStudentsList(Map<Boolean, List<Student>> studentsMap) throws Exception {
List<Student> validStudentsList = studentsMap.entrySet()
.stream()
.filter(p -> p.getKey() == Boolean.TRUE)
.flatMap(p -> p.getValue().stream())
.collect(Collectors.toList());
return validStudentsList;
}
public List<Student> getInValidStudentsList(Map<Boolean, List<Student>> studentsMap) throws Exception {
List<Student> invalidStudentsList =
partionedByPredicate.entrySet()
.stream()
.filter(p -> p.getKey() == Boolean.FALSE)
.flatMap(p -> p.getValue().stream())
.collect(Collectors.toList());
return invalidStudentsList;
}
公共地图getpartitionMap(列出学生列表){
List allPredicates=getAllPredicates();
Predicate compositePredicate=allPredicates.stream()
.reduce(w->true,谓词::and)
映射学生Map=studentsList
.stream()
.collect(收集器.partitionBy(compositePredicate));
返回学生SMAP;
}
公共列表getValidStudentsList(映射studentsMap)引发异常{
List validStudentsList=studentsMap.entrySet()
.stream()
.filter(p->p.getKey()==Boolean.TRUE)
.flatMap(p->p.getValue().stream())
.collect(Collectors.toList());
返回有效学生名单;
}
公共列表getInValidStudentsList(映射学生映射)引发异常{
列表无效学生列表=
partionedByPredicate.entrySet()
.stream()
.filter(p->p.getKey()==Boolean.FALSE)
.flatMap(p->p.getValue().stream())
.collect(Collectors.toList());
返回无效学生名单;
}
使用flatMap
您将只获得List
而不是List
感谢使用按键集-
id1.keySet().stream()
.filter(x->x==1)
.map(x->id1.get(x))
.collect(收集器.toList())
可能示例过于简单,但此处不需要Java流API。直接用地图就行了
List<String> list1 = id1.get(1); // this will return the list from your map
List list1=id1.get(1);//这将从地图中返回列表
@user2336315是,此代码假定该值存在;OP对此不是很清楚。啊,不要使用半生不熟的泛型类型。很可能,您的地图应该是map
,而不是map
// Stream<Map.Entry<K, V>> --> Stream<Map.Entry<K, V>>
.filter(entry -> entry.getKey() == 1)
// Stream<Map.Entry<K, V>> --> Stream<V>
.map(Map.Entry::getValue)
// Stream<V> --> List<V>
.collect(Collectors.toList())
// Stream<V> --> Optional<V> --> V
.findFirst().get()
List<String> list1 = id1.getOrDefault(1, Collections.emptyList());
public Map<Boolean, List<Student>> getpartitionMap(List<Student> studentsList) {
List<Predicate<Student>> allPredicates = getAllPredicates();
Predicate<Student> compositePredicate = allPredicates.stream()
.reduce(w -> true, Predicate::and)
Map<Boolean, List<Student>> studentsMap= studentsList
.stream()
.collect(Collectors.partitioningBy(compositePredicate));
return studentsMap;
}
public List<Student> getValidStudentsList(Map<Boolean, List<Student>> studentsMap) throws Exception {
List<Student> validStudentsList = studentsMap.entrySet()
.stream()
.filter(p -> p.getKey() == Boolean.TRUE)
.flatMap(p -> p.getValue().stream())
.collect(Collectors.toList());
return validStudentsList;
}
public List<Student> getInValidStudentsList(Map<Boolean, List<Student>> studentsMap) throws Exception {
List<Student> invalidStudentsList =
partionedByPredicate.entrySet()
.stream()
.filter(p -> p.getKey() == Boolean.FALSE)
.flatMap(p -> p.getValue().stream())
.collect(Collectors.toList());
return invalidStudentsList;
}
List<String> list1 = id1.get(1); // this will return the list from your map