Java8-对map过滤
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过滤的更多相关文章
- java8中map的meger方法的使用
java8中map有一个merge方法使用示例: /** * 打印出包含号码集的label的集合 * * @param args */ public static void main(String[] ...
- Java8中Map的遍历方式总结
在这篇文章中,我将对Map的遍历方式做一个对比和总结,将分别从JAVA8之前和JAVA8做一个遍历方式的对比,亲测可行. public class LambdaMap { private Map< ...
- Java8遍历Map、Map转List、List转Map
1. 遍历Map Map<Integer, String> map = new HashMap<>(); map.put(1, "a"); map.put( ...
- java8 stream().map().collect()用法
有一个集合: List<User> users = getList(); //从数据库查询的用户集合 现在想获取User的身份证号码:在后续的逻辑处理中要用: 常用的方法我们大家都知道,用 ...
- java8 forEach Map List[转载]
java8 forEach 在Map和List中的使用 原始的使用 Map<String, Integer> items = new HashMap<>(); items.pu ...
- java8的lambda过滤list遍历集合,排序
1.根据属性过滤list List<AllManagerBean> testLists = broadCastRoomMapper.allManagerlist(); List<Al ...
- java8 按条件过滤集合
//黄色部分为过滤条件list.stream().filter(user-> user.getId() > 5 && "1组".equals(user. ...
- java8中 map和flatmap的理解
假如我们有这样一个需求给定单词列表["Hello","World"],你想要返回列表["H","e","l&q ...
- Java8 Map中新增的方法使用总结
前言 得益于 Java 8 的 default 方法特性,Java 8 对 Map 增加了不少实用的默认方法,像 getOrDefault, forEach, replace, replaceAll, ...
- java代码之美(10)---Java8 Map中的computeIfAbsent方法
Map中的computeIfAbsent方法 Map接口的实现类如HashMap,ConcurrentHashMap,HashTable等继承了此方法,通过此方法可以在特定需求下,让你的代码更加简洁. ...
随机推荐
- Python练手例子(6)
31.请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母. 程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母. letter ...
- ASP.NET JS调用WebService——简单例子
一.创建好WebService 二.编辑页面与js 三. 运行页面并点击按钮,结果如下 简单调用吧!
- Hibernate 的hql查询简介【申明:来源于网络】
Hibernate 的hql查询简介[申明:来源于网络] Hibernate 的hql查询简介:http://blog.csdn.net/leaf_130/article/details/539329 ...
- NIOS_UART
1.Fifoed avalon UART带缓冲区,使用非常方便,google下载,google上有的技术资料,百度上找不到,以为没有这个事情: 2.两种UART如果想用操作寄存器的方式操作,需要在al ...
- WcPro项目(WordCount优化)
1 基本任务:代码编写+单元测试 1.1 项目GitHub地址 https://github.com/ReWr1te/WcPro 1.2 项目PSP表格 PSP2.1 PSP阶段 预估耗时(分钟) 实 ...
- mysql - Truncated incorrect DOUBLE value: 'undefined'
mysql - Truncated incorrect DOUBLE value: 'undefined' 我是怎么遇到这个问题的? 我要从多个表里,查询统计数据,保存到统计表里,需要执行下面这种结构 ...
- Head First Python-python面向对象
与大多数其他的编程语言一样,Python容许创建并定义面向对象的类,类可以将代码与代码处理的数据相关联. 对于更加复杂的数据,一般的列表已经不能满足需求了. 我们可以使用字典dict将数据值与键相关联 ...
- nginx修改上传文件大小限制
问题: 项目上线,图片上传报413错误,找了半天,原来是nginx限制了上传大小 在nginx.conf的server的location中加client_max_body_size 10m;
- Linux主机之间ssh免密登录配置方法
由于公司的生产环境有很多台Linux的CentOS服务器, 为了方便机子(假设两台机子A,B)互相之间免密ssh, scp命令操作,配置如下 1. 在A.B上分别创建本机的公钥和私钥,输入命令后连续三 ...
- Java面试题和解答(四)
1.JVM什么情况下会GC,GC策略有哪些 当应用程序分配新的对象,GC的代的预算大小已经达到阈值,比如GC的第0代已满:代码主动显式调用System.GC.Collect():其他特殊情况,比如,系 ...