码迷,mamicode.com
首页 > 编程语言 > 详细

Java8-对map过滤

时间:2019-03-20 22:08:50      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:使用   cat   code   public   integer   int   turn   length   before   

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过滤

标签:使用   cat   code   public   integer   int   turn   length   before   

原文地址:https://www.cnblogs.com/fengkunangel/p/10568021.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!