[英]Hashmap with Streams in Java 8 Streams to collect value of Map
让我们考虑一个 hashmap
Map<Integer, List> id1 = new HashMap<Integer,List>();
我在两个 hashmap 中都插入了一些值。
例如,
List<String> list1 = new ArrayList<String>();
list1.add("r1");
list1.add("r4");
List<String> list2 = new ArrayList<String>();
list2.add("r2");
list2.add("r5");
List<String> list3 = new ArrayList<String>();
list3.add("r3");
list3.add("r6");
id1.put(1,list1);
id1.put(2,list2);
id1.put(3,list3);
id1.put(10,list2);
id1.put(15,list3);
Q1)现在我想对 hashmap 中的键应用过滤条件并检索相应的值(列表)。
例如:这里我的查询是key=1,output应该是'list1'
我写
id1.entrySet().stream().filter( e -> e.getKey() == 1);
但我不知道如何将此 stream 操作的列表检索为 output。
Q2) 同样,我想对 hashmap 中的键应用过滤条件,并检索相应的列表列表。
例如:这里我的查询是key=1%(即key可以是1,10,15),output应该是'list1','list2','list3'(list of lists)。
如果您确定最多只能获得一个通过过滤器的元素(由您的过滤器保证),您可以使用findFirst
:
Optional<List> o = id1.entrySet()
.stream()
.filter( e -> e.getKey() == 1)
.map(Map.Entry::getValue)
.findFirst();
在一般情况下,如果过滤器可能匹配多个 Lists,您可以将它们收集到 List of Lists :
List<List> list = id1.entrySet()
.stream()
.filter(.. some predicate...)
.map(Map.Entry::getValue)
.collect(Collectors.toList());
您需要做的是从Map
的.entrySet()
创建一个Stream
:
// Map<K, V> --> Set<Map.Entry<K, V>> --> Stream<Map.Entry<K, V>>
map.entrySet().stream()
从一开始,您就可以对这些条目进行.filter()
。 例如:
// Stream<Map.Entry<K, V>> --> Stream<Map.Entry<K, V>>
.filter(entry -> entry.getKey() == 1)
并从中获取值,您.map()
:
// Stream<Map.Entry<K, V>> --> Stream<V>
.map(Map.Entry::getValue)
最后,您需要收集到一个List
:
// Stream<V> --> List<V>
.collect(Collectors.toList())
如果您只有一个条目,请改用它(注意:此代码假定存在一个值;否则,请使用.orElse()
;有关更多详细信息,请参阅Optional
的javadoc ):
// Stream<V> --> Optional<V> --> V
.findFirst().get()
对于您的 Q2,您的问题已经有了答案。 对于您的 Q1,更一般地说,当您知道键的过滤应提供唯一值时,根本不需要使用 Streams。
只需使用get
或getOrDefault
,即:
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;
}
使用flatMap
您将只得到List<Student>
而不是List<List<Student>>
。
谢谢
使用 keySet-
id1.keySet().stream()
.filter(x -> x == 1)
.map(x -> id1.get(x))
.collect(Collectors.toList())
也许示例过于简单,但您不需要此处的 Java 流 API。 直接使用地图即可。
List<String> list1 = id1.get(1); // this will return the list from your map
为什么这一切!
列表 list1 = id1.get(1);
HashMap<Integer, String> hashmap = new HashMap<>();
hashmap.put(1, "a");
hashmap.put(2, "b");
List<String> collect = hashmap.keySet().stream()
.map(k -> "key=" + k + " value=" + hashmap.get(k))
.collect(Collectors.toList());
System.out.println(collect);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.