List 过滤

List对象类(StudentInfo)

StudentInfo对象类

测试数据

//测试数据,请不要纠结数据的严谨性
List<StudentInfo> studentList = new ArrayList<>();
studentList.add(new StudentInfo("李小明",true,18,1.76,LocalDate.of(2001,3,23)));
studentList.add(new StudentInfo("张小丽",false,18,1.61,LocalDate.of(2001,6,3)));
studentList.add(new StudentInfo("王大朋",true,19,1.82,LocalDate.of(2000,3,11)));
studentList.add(new StudentInfo("陈小跑",false,17,1.67,LocalDate.of(2002,10,18)));

输出Students列表

 //输出List
StudentInfo.printStudents(studentList);

输出结果如下图:

使用filter()过滤List

//查找身高在1.8米及以上的男生
List<StudentInfo> boys = studentList.stream().filter(s->s.getGender() && s.getHeight() >= 1.8).collect(Collectors.toList());
//输出查找结果
StudentInfo.printStudents(boys);

结果如下图:

转map

常用方式

代码如下:

public Map<Long, String> getIdNameMap(List<Account> accounts) {return accounts.stream().collect(Collectors.toMap(Account::getId, Account::getUsername));
}

收集成实体本身map

代码如下:

public Map<Long, Account> getIdAccountMap(List<Account> accounts) {return accounts.stream().collect(Collectors.toMap(Account::getId, account -> account));
}

account -> account是一个返回本身的lambda表达式,其实还可以使用Function接口中的一个默认方法代替,使整个方法更简洁优雅:

public Map<Long, Account> getIdAccountMap(List<Account> accounts) {return accounts.stream().collect(Collectors.toMap(Account::getId, Function.identity()));
}

重复key的情况

代码如下:

public Map<String, Account> getNameAccountMap(List<Account> accounts) {return accounts.stream().collect(Collectors.toMap(Account::getUsername, Function.identity()));
}

这个方法可能报错(java.lang.IllegalStateException: Duplicate key),因为name是有可能重复的。toMap有个重载方法,可以传入一个合并的函数来解决key冲突问题:

public Map<String, Account> getNameAccountMap(List<Account> accounts) {return accounts.stream().collect(Collectors.toMap(Account::getUsername, Function.identity(), (key1, key2) -> key2));
}

这里只是简单的使用后者覆盖前者来解决key重复问题。

指定具体收集的map

toMap还有另一个重载方法,可以指定一个Map的具体实现,来收集数据:

public Map<String, Account> getNameAccountMap(List<Account> accounts) {return accounts.stream().collect(Collectors.toMap(Account::getUsername, Function.identity(), (key1, key2) -> key2, LinkedHashMap::new));
}

转自:https://zacard.net/2016/03/17/java8-list-to-map/

java8 List操作相关推荐

  1. java8 stream操作总结

    昨天看到java9 支持 reactive stream,但是java被oracle收购之后,我为什么一定要用reactive stream呢,掌握java8 stream感觉跟python中pand ...

  2. java8 流操作_java8中的流操作

    Stream 流是 Java 8 新提供给开发者的一组操作集合的 API,将要处理的元素集合看作一种流, 流在管道中传输, 并且可以在管道的节点上进行处理, 比如筛选.排序.聚合等.元素流在管道中经过 ...

  3. Java8 stream操作

    这里记录一下java8的stream语法使用 /*** @author fengzp* @date 2017/4/11下午4:49* @email fengzp@gzyitop.com* @compa ...

  4. 【java8 stream操作List集合】

    内容简介 本文主要说明在Java8及以上版本中,使用stream().filter()来过滤一个List对象,查找符合条件的对象集合. list.stream().mapToDouble(User:: ...

  5. JAVA8 Stream的系列操作,Optional使用---- java养成

    Java养成计划----学习打卡第六十天 内容导航 Stream回顾 Stream的中间操作 筛选和排序 映射 map和flatmap的区别 排序 Stream的终止操作 匹配与查找 归约 收集 Op ...

  6. Java8 Stream流操作在用户系统中的妙用

    在做目前这个项目的时候,发现以前有一个筛选的需求,老程序员是这么做的,先请求Http服务器得到一长串json数据,大概用A4纸打了40多页那么多,然后将这些对象写入到sqlite数据库中,再用数据库查 ...

  7. Java8新特性学习_001_(Lambda表达式,函数式接口,方法引用,Stream类,Optional类)

    目录 ■代码 ■代码运行结果 ■代码说明 ・44行:Stream的.foreach方法ー参数类型:函数式接口 ・82行:Interface中,default方法 ・92行   Stream的.max方 ...

  8. 【JAVA8】Map新方法,别再重复造车轮了

    文章目录 getOrDefault forEach compute computeIfAbsent computeIfPresent merge putIfAbsent remove(key,valu ...

  9. supplier java8_关于JAVA8中关于Supplier的个人理解

    什么是Supplier supplier接口是JAVA8以后配合lambda表达式和函数式接口编程(FunctionInterface,以下简称FI)组合使用的一个接口,对外表现为双冒号": ...

最新文章

  1. 使用第三方SDK(如微信、qq、快看、头条等),调用接口405 Method Not Allowed
  2. 赋能云HBase备份恢复 百T级别数据量备份恢复支持
  3. 启动springboot报错Error creating bean with name 'dataSource' defined in class path resource
  4. IE – File - Work Offline
  5. 105.敏捷开发模型
  6. 牛客网暑期ACM多校训练营(第三场)A - PAXM Team(01背包)
  7. java permgen_Java 8:从PermGen到元空间
  8. java es api jar包_Elasticsearch 搜索服务器 Java API 使用详解
  9. USACO 2.33 Zero Sum
  10. linux笔记:shell编程-正则表达式
  11. C/C++ map函数统计每个字母出现的次数
  12. android 计算器边框,Android计算器——入门
  13. np.roll()的理解和用法
  14. 【微信小程序】项目开发-----百度翻译API接口开发微信翻译小程序
  15. 虚拟服务器添加打印机,在VMware Workstation 7虚拟机中应用主机打印机
  16. 华为鸿蒙源自那句话,华为鸿蒙系统名字源自山海经它的很多自主专利都源于这本古籍,为什么?...
  17. 区块链:分布式系统核心技术
  18. js根据数组对象属性删除里面的指定对象
  19. pandas库--DataFrame常用操作
  20. B站韩顺平java学习笔记(三)--运算符章节

热门文章

  1. ​Au入门系列之三:调整音量
  2. 1087: Time
  3. Android小应用——监控屏幕使用时间
  4. RAID中条带的概念
  5. 安徽师范大学计算机与信息学院研究生导师,安徽师范大学数学计算机科学学院导师介绍:郭要红...
  6. 13 Django之中间件
  7. 成功解决:Updates were rejected because the tip of your current branch is behind its remote...【解决方法】
  8. Python自动化第二周之数据模块
  9. 360安全卫士造成网站不能访问的解决办法
  10. 设计模式——单例模式(创建型模式)