1、对map按值过滤返回值

 public class TestMapFilter {

     public static void main(String[] args) {
Map<Integer, String> HOSTING = new HashMap<>();
HOSTING.put(1, "linode.com");
HOSTING.put(2, "heroku.com");
HOSTING.put(3, "digitalocean.com");
HOSTING.put(4, "aws.amazon.com"); // Before Java 8
String result = "";
for (Map.Entry<Integer, String> entry : HOSTING.entrySet()) {
if ("heroku.com".equals(entry.getValue())) {
result = entry.getValue();
}
}
System.out.println("Before Java 8: " + result); // Map -> Stream -> Filter -> String
result = HOSTING.entrySet().stream()
.filter(map -> "linode.com".equals(map.getValue()))
.map(map -> map.getValue())
.collect(Collectors.joining());
System.out.println("With Java 8:" + result); // filter more values
result = HOSTING.entrySet().stream()
.filter(x -> {
if (!x.getValue().contains("amazon") && !x.getValue().contains("digital")) {
return true;
}
return false;
})
.map(map -> map.getValue())
.collect(Collectors.joining(",")); System.out.println("With Java 8 : " + result);
}
}

2、按key过滤返回map

 public class TestMapFilter2 {

     public static void main(String[] args) {
Map<Integer, String> HOSTING = new HashMap<>();
HOSTING.put(1, "linode.com");
HOSTING.put(2, "heroku.com");
HOSTING.put(3, "digitalocean.com");
HOSTING.put(4, "aws.amazon.com"); //Map -> Stream -> Filter -> Map
Map<Integer, String> result1 = HOSTING.entrySet().stream()
.filter(map -> map.getKey() == 2)
.collect(Collectors.toMap(h -> h.getKey(), h -> h.getValue())); System.out.println(result1); Map<Integer, String> result2 = HOSTING.entrySet().stream()
.filter(map -> map.getKey() <= 4)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
System.out.println(result2);
}
}

3、Predicate使用

 public class TestMapFilter3 {

     public static <K, V> Map<K, V> filterByValue(Map<K, V> map, Predicate<V> predicate) {
return map.entrySet().stream()
.filter(x -> predicate.test(x.getValue()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
} public static void main(String[] args) { Map<Integer, String> HOSTING = new HashMap<>();
HOSTING.put(1, "linode.com");
HOSTING.put(2, "heroku.com");
HOSTING.put(3, "digitalocean.com");
HOSTING.put(4, "aws.amazon.com");
HOSTING.put(5, "aws2.amazon.com"); Map<Integer, String> result1 = filterByValue(HOSTING, x -> x.contains("heroku"));
System.out.println(result1); Map<Integer, String> result2 = filterByValue(HOSTING, x -> (x.contains("aws") || x.contains("digitalocean")));
System.out.println(result2); Map<Integer, String> result3 = filterByValue(HOSTING, x -> (x.contains("aws") && !x.contains("aws2")));
System.out.println(result3); Map<Integer, String> result4 = filterByValue(HOSTING, x -> x.length() <= 10);
System.out.println(result4);
}
}

Java8-对map过滤的更多相关文章

  1. java8中map的meger方法的使用

    java8中map有一个merge方法使用示例: /** * 打印出包含号码集的label的集合 * * @param args */ public static void main(String[] ...

  2. Java8中Map的遍历方式总结

    在这篇文章中,我将对Map的遍历方式做一个对比和总结,将分别从JAVA8之前和JAVA8做一个遍历方式的对比,亲测可行. public class LambdaMap { private Map< ...

  3. Java8遍历Map、Map转List、List转Map

    1. 遍历Map Map<Integer, String> map = new HashMap<>(); map.put(1, "a"); map.put( ...

  4. java8 stream().map().collect()用法

    有一个集合: List<User> users = getList(); //从数据库查询的用户集合 现在想获取User的身份证号码:在后续的逻辑处理中要用: 常用的方法我们大家都知道,用 ...

  5. java8 forEach Map List[转载]

    java8 forEach 在Map和List中的使用 原始的使用 Map<String, Integer> items = new HashMap<>(); items.pu ...

  6. java8的lambda过滤list遍历集合,排序

    1.根据属性过滤list List<AllManagerBean> testLists = broadCastRoomMapper.allManagerlist(); List<Al ...

  7. java8 按条件过滤集合

    //黄色部分为过滤条件list.stream().filter(user-> user.getId() > 5 && "1组".equals(user. ...

  8. java8中 map和flatmap的理解

    假如我们有这样一个需求给定单词列表["Hello","World"],你想要返回列表["H","e","l&q ...

  9. Java8 Map中新增的方法使用总结

    前言 得益于 Java 8 的 default 方法特性,Java 8 对 Map 增加了不少实用的默认方法,像 getOrDefault, forEach, replace, replaceAll, ...

  10. java代码之美(10)---Java8 Map中的computeIfAbsent方法

    Map中的computeIfAbsent方法 Map接口的实现类如HashMap,ConcurrentHashMap,HashTable等继承了此方法,通过此方法可以在特定需求下,让你的代码更加简洁. ...

随机推荐

  1. JDK8之The type java.util.Map$Entry cannot be resolved

    eclipse+tomcat7+jdk1.6上面报错的方式我的解法方法是吧jre8换成6的就好了选中项目->右键->java build path ->找到jre system li ...

  2. CQL语句

    CQL中默认忽略大小写,若需要大小写敏感,可使用双引号将对象包起来,引用的时候也要用双引号包住 tips: 使用CQL需要预装Python环境 Ⅰ.基本知识点 1.1 数据类型 这边和关系型数据库相近 ...

  3. 源码编译vim

    目录 获取最新版 vim 源码 1 git仓库clone 2, 源码包下载,里面有各个版本的vim压缩包 vim 配置选项 配置示例 参考文章 tip 获取最新版 vim 源码 1 git仓库clon ...

  4. Web开发(XAMPP服务器搭建)

    XAMPP是一个功能强大的搭建服务器环境的软件集成包.它集成了Apache.MySql.php.perl这几个服务器常用的软件.而我们在使用时,省去了安装这些软件的步骤,只需要下载XAMPP,解压缩. ...

  5. 队列->队列的应用(银行业务模拟)

    文字描述 示意图 代码实现 // // Created by Zhenjie Yu on 2019-04-13. // #include <stdio.h> #include <st ...

  6. 修改文件MD5值

    1.查看文件的MD5值 (1)下载MD5Checker http://getmd5checker.com/download.html 或者 链接: https://pan.baidu.com/s/1e ...

  7. 【转】Java遍历Map对象的四种方式

    关于java中遍历map具体哪四种方式,请看下文详解吧. 方式一 这是最常见的并且在大多数情况下也是最可取的遍历方式.在键值都需要时使用. Map<Integer, Integer> ma ...

  8. jQuery通过ajax请求php遍历json数组到table中的代码

    html代码(test.html),js在html底部 具体代码如下所示: <!DOCTYPE html> <html lang="en"> <hea ...

  9. mysql 日期 字符串

    Mysql 中字符串转时间跟Oracle略不同,函数为 str_to_date 应注意的是里面的大小写 如下: MySQL内置函数,在mysql里面利用str_to_date()把字符串转换为日期. ...

  10. 【Idea】idea waiting until last debugger command completes

    https://blog.csdn.net/KingBoyWorld/article/details/73440717 以上方法可以解决 另: 有说: 并未尝试 https://stackoverfl ...