Java8-对map过滤

Posted 风坤

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java8-对map过滤相关的知识,希望对你有一定的参考价值。

1、对map按值过滤返回值

 1 public class TestMapFilter {
 2 
 3     public static void main(String[] args) {
 4         Map<Integer, String> HOSTING = new HashMap<>();
 5         HOSTING.put(1, "linode.com");
 6         HOSTING.put(2, "heroku.com");
 7         HOSTING.put(3, "digitalocean.com");
 8         HOSTING.put(4, "aws.amazon.com");
 9 
10         // Before Java 8
11         String result = "";
12         for (Map.Entry<Integer, String> entry : HOSTING.entrySet()) {
13             if ("heroku.com".equals(entry.getValue())) {
14                 result = entry.getValue();
15             }
16         }
17         System.out.println("Before Java 8: " + result);
18 
19         // Map -> Stream -> Filter -> String
20         result = HOSTING.entrySet().stream()
21                 .filter(map -> "linode.com".equals(map.getValue()))
22                 .map(map -> map.getValue())
23                 .collect(Collectors.joining());
24         System.out.println("With Java 8:" + result);
25 
26         // filter more values
27         result = HOSTING.entrySet().stream()
28                 .filter(x -> {
29                     if (!x.getValue().contains("amazon") && !x.getValue().contains("digital")) {
30                         return true;
31                     }
32                     return false;
33                 })
34                 .map(map -> map.getValue())
35                 .collect(Collectors.joining(","));
36 
37         System.out.println("With Java 8 : " + result);
38     }
39 }

 

 

2、按key过滤返回map

 1 public class TestMapFilter2 {
 2 
 3     public static void main(String[] args) {
 4         Map<Integer, String> HOSTING = new HashMap<>();
 5         HOSTING.put(1, "linode.com");
 6         HOSTING.put(2, "heroku.com");
 7         HOSTING.put(3, "digitalocean.com");
 8         HOSTING.put(4, "aws.amazon.com");
 9 
10         //Map -> Stream -> Filter -> Map
11         Map<Integer, String> result1 = HOSTING.entrySet().stream()
12                 .filter(map -> map.getKey() == 2)
13                 .collect(Collectors.toMap(h -> h.getKey(), h -> h.getValue()));
14 
15         System.out.println(result1);
16 
17         Map<Integer, String> result2 = HOSTING.entrySet().stream()
18                 .filter(map -> map.getKey() <= 4)
19                 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
20         System.out.println(result2);
21     }
22 }

 

3、Predicate使用

 1 public class TestMapFilter3 {
 2 
 3     public static <K, V> Map<K, V> filterByValue(Map<K, V> map, Predicate<V> predicate) {
 4         return map.entrySet().stream()
 5                 .filter(x -> predicate.test(x.getValue()))
 6                 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
 7     }
 8 
 9     public static void main(String[] args) {
10 
11         Map<Integer, String> HOSTING = new HashMap<>();
12         HOSTING.put(1, "linode.com");
13         HOSTING.put(2, "heroku.com");
14         HOSTING.put(3, "digitalocean.com");
15         HOSTING.put(4, "aws.amazon.com");
16         HOSTING.put(5, "aws2.amazon.com");
17 
18         Map<Integer, String> result1 = filterByValue(HOSTING, x -> x.contains("heroku"));
19         System.out.println(result1);
20 
21         Map<Integer, String> result2 = filterByValue(HOSTING, x -> (x.contains("aws") || x.contains("digitalocean")));
22         System.out.println(result2);
23 
24         Map<Integer, String> result3 = filterByValue(HOSTING, x -> (x.contains("aws") && !x.contains("aws2")));
25         System.out.println(result3);
26 
27         Map<Integer, String> result4 = filterByValue(HOSTING, x -> x.length() <= 10);
28         System.out.println(result4);
29     }
30 }

 

以上是关于Java8-对map过滤的主要内容,如果未能解决你的问题,请参考以下文章

Java8 新特性 Stream 无状态中间操作

java8快速实现分组过滤list转map

java8 .stream().sorted().filter().map().collect()用法

使用 Java8 流过滤 Map 的键后映射到列表

Java8 Stream流

Java8 Stream针对List先分组再求和最大值最小值平均值等