iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >使用java8 新特性stream流对List<Map<String, Object>>集合进行遍历、过滤、查询、去重、排序、分组
  • 315
分享到

使用java8 新特性stream流对List<Map<String, Object>>集合进行遍历、过滤、查询、去重、排序、分组

listjava 2023-09-08 22:09:33 315人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

对于一个List类型的数据,可以使用Java 8的新特性stream流来进行遍历、过滤、查询、去重、排序、分组等操作。 遍历: List dataList = new ArrayList();//添加数据Map map1 = new Has

对于一个List>类型的数据,可以使用Java 8的新特性stream流来进行遍历、过滤、查询、去重、排序、分组等操作。

遍历:

List> dataList = new ArrayList>();//添加数据Map map1 = new HashMap();map1.put("id", 1);map1.put("name", "张三");dataList.add(map1);Map map2 = new HashMap();map2.put("id", 2);map2.put("name", "李四");dataList.add(map2);//使用stream流进行遍历dataList.stream().forEach(map -> {    System.out.println("id:" + map.get("id") + ",name:" + map.get("name"));});

过滤:

List> dataList = new ArrayList>();//添加数据Map map1 = new HashMap();map1.put("id", 1);map1.put("name", "张三");map1.put("age", 20);dataList.add(map1);Map map2 = new HashMap();map2.put("id", 2);map2.put("name", "李四");map2.put("age", 30);dataList.add(map2);//使用stream流进行过滤dataList.stream().filter(map -> (Integer) map.get("age") > 25).forEach(map -> {    System.out.println("id:" + map.get("id") + ",name:" + map.get("name"));});

查询:

List> dataList = new ArrayList>();//添加数据Map map1 = new HashMap();map1.put("id", 1);map1.put("name", "张三");map1.put("age", 20);dataList.add(map1);Map map2 = new HashMap();map2.put("id", 2);map2.put("name", "李四");map2.put("age", 30);dataList.add(map2);//使用stream流进行查询Optional> optionalMap = dataList.stream().filter(map -> (Integer) map.get("id") == 1).findFirst();if (optionalMap.isPresent()) {    Map map = optionalMap.get();    System.out.println("id:" + map.get("id") + ",name:" + map.get("name") + ",age:" + map.get("age"));}

去重:

List> dataList = new ArrayList>();//添加数据Map map1 = new HashMap();map1.put("id", 1);map1.put("name", "张三");map1.put("age", 20);dataList.add(map1);Map map2 = new HashMap();map2.put("id", 2);map2.put("name", "李四");map2.put("age", 30);dataList.add(map2);Map map3 = new HashMap();map3.put("id", 2);map3.put("name", "李四");map3.put("age", 30);dataList.add(map3);//使用stream流进行去重List> distinctList = dataList.stream().distinct().collect(Collectors.toList());distinctList.stream().forEach(map -> {    System.out.println("id:" + map.get("id") + ",name:" + map.get("name") + ",age:" + map.get("age"));});

排序:

List> dataList = new ArrayList>();//添加数据Map map1 = new HashMap();map1.put("id", 1);map1.put("name", "张三");map1.put("age", 20);dataList.add(map1);Map map2 = new HashMap();map2.put("id", 2);map2.put("name", "李四");map2.put("age", 30);dataList.add(map2);Map map3 = new HashMap();map3.put("id", 3);map3.put("name", "王五");map3.put("age", 25);dataList.add(map3);//使用stream流进行排序List> sortedList = dataList.stream().sorted((map1, map2) -> (Integer) map1.get("age") - (Integer) map2.get("age")).collect(Collectors.toList());sortedList.stream().forEach(map -> {    System.out.println("id:" + map.get("id") + ",name:" + map.get("name") + ",age:" + map.get("age"));});

分组:

List> dataList = new ArrayList>();//添加数据Map map1 = new HashMap();map1.put("id", 1);map1.put("name", "张三");map1.put("age", 20);dataList.add(map1);Map map2 = new HashMap();map2.put("id", 2);map2.put("name", "李四");map2.put("age", 30);dataList.add(map2);Map map3 = new HashMap();map3.put("id", 3);map3.put("name", "王五");map3.put("age", 25);dataList.add(map3);//使用stream流进行分组Map>> groupMap = dataList.stream().collect(Collectors.groupingBy(map -> (Integer) map.get("age")));for (Map.Entry>> entry : groupMap.entrySet()) {    Integer age = entry.geTKEy();    List> list = entry.getValue();    System.out.println("年龄为" + age + "的人数为" + list.size());}

来源地址:https://blog.csdn.net/MidYear/article/details/131722819

--结束END--

本文标题: 使用java8 新特性stream流对List<Map<String, Object>>集合进行遍历、过滤、查询、去重、排序、分组

本文链接: https://www.lsjlt.com/news/400664.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作