[英]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.