【发布时间】:2022-01-18 07:58:01
【问题描述】:
代码模板:
Map<String, String> hashMap = new HashMap<>();
hashMap.put("RED", "1");
hashMap.put("BLUE", "2");
hashMap.put("BLACK", "3");
Map<String, String> filteredMap = hashMap.entrySet().stream()
.filter(entry -> (entry.getKey().equals("RED") && entry.getValue().equals("1"))
**&&**
(entry.getKey().equals("BLUE") && entry.getValue().equals("2")) )
.collect(Collectors.toMap(map -> map.getKey(), map -> map.getValue()));
我想知道 Map 中是否同时包含 {Red,1} 和 {Blue,2}。如何将两个过滤条件放在一个流中?我尝试了 && 条件,但它不起作用。请帮忙。
【问题讨论】:
标签: java hashmap java-stream