List stream的9种常用功能

1、List 转List

List llla = Arrays.asList("1","2").stream().map(Long::parseLong).collect(Collectors.toList());

2、List转类型List、List、List

//(1)、List中的String属性转List

List userNameList = list.stream().map(UserInfo::getUserName).collect(Collectors.toList());

//过滤null

List userNameList1 = list.stream().map(UserInfo::getUserName).filter(Objects::nonNull).collect(Collectors.toList());

//(2)、List中的Integer属性转List

List userIds = list.stream().map(UserInfo::getAmount).map(String::valueOf).collect(Collectors.toList());

//过滤null

List userIds1 = list.stream().map(UserInfo::getAmount).filter(Objects::nonNull).map(String::valueOf).collect(Collectors.toList());

//List属性转List ,如果为 null就为0

List aountList1 = list.stream().map(i -> i.getAmount() == null ? 0 : i.getAmount()).collect(Collectors.toList());

//(3)、List属性求和

// long sum = list.stream().mapToInt(UserInfo::getAmount).sum();

//如果为 null就为0

long sum1 = list.stream().mapToInt(i -> i.getAmount() == null ? 0 : i.getAmount()).sum();

3、List转map

//(1)、List转Map<String,Bean> key重复会报错
// Map<Long, UserInfo> map1 = list.stream().collect(Collectors.toMap(UserInfo::getUserId, i -> i));
//(2)、list bean 中某两个属性 转 map (k1,k2)->k2) 表示如果key相同以后面那个覆盖前面的 。必须对value的null进行处理,因为value为null会报错
Map<Long, String> map = list.stream().collect(Collectors.toMap(UserInfo::getUserId, i -> i.getUserName() == null ? "null" : i.getUserName(), (k1, k2) -> k2));

4、分组

//List分组

// Map<Long, List> collect = list.stream().collect(Collectors.groupingBy(UserInfo::getUserId));

//List分组,null为0

Map<Long, List> collect = list.stream().collect(Collectors.groupingBy(i -> i.getUserId() == null ? 0 : i.getUserId()));

//List分组统计,相当于数据库 select name,count(*)... group by name

Map<Long, Long> collect1 = list.stream().collect(Collectors.groupingBy(i -> i.getUserId() == null ? 0 : i.getUserId(), Collectors.counting()));

log.info("=======" + (collect1));
5、limit

List collect2 = list.stream().limit(1).collect(Collectors.toList());
6、排序

List l3 = list.stream().sorted((s1, s2) -> s2.getAge().compareTo(s1.getAge())).collect(Collectors.toList());
7、判断年龄大于2

boolean check = list.stream().anyMatch(student -> student.getAge() > 2);
8、list拼接成字符串

String str = list.stream().map(UserInfo::getUserName).collect(Collectors.joining(",", "[", "]"));
9、获取年龄的最大值、最小值、平均值、求和等等

IntSummaryStatistics intSummaryStatistics = list.stream().mapToInt(UserInfo::getAge).summaryStatistics();

System.out.println(intSummaryStatistics.getMax());

System.out.println(intSummaryStatistics.getCount());

System.out.println(intSummaryStatistics.getSum());

相关推荐
Evaporator Core6 小时前
Windows批处理脚本自动合并当前目录下由You-get下载的未合并的音视频文件
windows·音视频
开航母的李大14 小时前
Navicat 全量&增量数据库迁移
数据库·oracle
十五年专注C++开发14 小时前
CMake进阶: externalproject_add用于在构建阶段下载、配置、构建和安装外部项目
linux·c++·windows·cmake·自动化构建
guidovans18 小时前
基于tkinter开发电脑工具集(源码在底部)
linux·windows·python·gui·tkinter
Apple_羊先森19 小时前
Oracle表数据维护全流程指南:备份、删除与性能优化
数据库·oracle·性能优化
ayaya_mana1 天前
Notepad--:国产跨平台文本编辑器,Notepad++ 的理想替代方案
linux·windows·macos·编辑器·notepad·notepad--
昏睡红猹2 天前
如何正确使用SetThreadExecutionState来阻止Windows进入睡眠
windows
汤姆花花2 天前
OCI编程高级篇(八) LOB写操作
oracle·asm·dul·oracle oci 编程
laizhenghua2 天前
解决:[64000][257] ORA-00257: 归档程序错误。只有在解析完成后才以 AS SYSDBA 方式连接问题
oracle
霖002 天前
FPGA的PS基础1
数据结构·人工智能·windows·git·算法·fpga开发