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等继承了此方法,通过此方法可以在特定需求下,让你的代码更加简洁. ...
随机推荐
- Selenium 3 学习小结
4个类+常用的46个方法 从以下知识内容对selenium 3自动化框架进行初步学习: 1.安装selenium pip install selenium pip list 2.驱动.关闭浏览器 首先 ...
- php发送邮箱重置密码链接,并在重置成功后使链接失效 (ThinkPHP5)
/** * 重置密码页,验证链接有效性,页面发送邮件调用sendResetPwdEmail()方法 */ public function resetPwd() { $param = input('') ...
- 移动端无法复制:使用clipboard.js碰到的一个小问题
移动端无法复制:使用clipboard.js碰到的一个小问题 直接看下面的代码:在移动端访问,点击,能正常复制. <html> <head> <meta http-e ...
- LeetCode 171 Excel Sheet Column Number 解题报告
题目要求 Given a column title as appear in an Excel sheet, return its corresponding column number. For e ...
- 允许浏览器下载exe.config文件
进入系统目录 %windir%\System32\inetsrv\config\ 编辑IIS配置文件 applicationHost.config 找到如下行 <requestFiltering ...
- Movavi Video Editor 15 Plus(视频编辑软件) 中文版
Movavi Video Editor 15 Plus Mac版是Movavi系列中的一款视频编辑器,Movavi Video Editor Plus 15破解版提供了全面的视频功能,另外还支持为视频 ...
- Asp.net MVC WebApi项目的自动接口文档及测试功能打开方法
https://blog.csdn.net/foren_whb/article/details/78866133
- JavaScript 当前URL取参返回字典
getParam : function(){ return (key, strURL = window.location.search) => new RegExp("(^|\\?|& ...
- MyEclipse破解步骤
1.下载安装(注:安装后不要打开myeclipse,以下步骤完成方可打开) 2.解压破解文件 3.打开文件夹patch 3.全部复制 4.找到myeclipse的安装目录,打开plugins文件夹 5 ...
- 关于使用CodeFirst,修改类或上下文时操作数据库报错解决方法
在操作已经创建好的数据库时,若是添加新的实体类或者修改原有数据库上下文,会报如下错误: The model backing the 'StudentDbContext' context has cha ...