当前位置: 首页 > news >正文

Java8 新特性stream、forEach常用方法总结

1、去重

List<Long> list = new ArrayList<>();list.add(1L);list.add(2L);list.add(3L);list.add(3L);list.stream().distinct().collect(Collectors.toList());

2、筛选出符合条件的数据

1)单条件筛选

筛选出性别为男的学生:

List<Student> studentList = list.stream().filter(
s->s.getGender().equals("1")
).collect(Collectors.toList());

2 )多条件筛选

筛选出性别为男并且身高为 1 米 8 以上的学生:

List<Student> studentList = list.stream().filter(
s->s.getGender().equals("1")&& s.getHeight()>=180
).collect(Collectors.toList());

3、forEach遍历列表的每一个元素

import java.util.Arrays;public class Test{public static void main(String[] args){Arrays.asList("测试1", "测试2", "测试3").forEach(System.out::println);}
}

上面这段代码中,我们使用了 forEach 方法遍历列表的每一个元素,并把元素传递给 System.out.println() 方法打印输出到屏幕上。

4、forEach设置多个属性值

 list.forEach((entity) ->{entity.setCreateTime(new Date());entity.setCreateUser(user.getUserId());                         });

5、提取某一列(以name为例)

对象类

@Data
public class StudentInfo implements Serializable{//名称private String name;//性别 true男 false女private Boolean gender;//年龄private Integer age;//身高private Double height;//出生日期private LocalDate birthday;
}

测试数据

//测试数据,请不要纠结数据的严谨性
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)));

提取数据

//输出List
System.out.println(studentList);
//从对象列表中提取一列(以name为例)
List<String> nameList = studentList.stream().map(StudentInfo::getName).collect(Collectors.toList());
//提取后输出name
nameList.forEach(s-> System.out.println(s));

6、求和

List<Apple>

//计算 总金额
BigDecimal discountAll = list.stream().map(Apple::getDiscount).reduce(BigDecimal.ZERO, BigDecimal::add);
System.err.println("discount:"+discount);

List<Map<String, Object>>

BigDecimal discountAll = list.stream().map(m -> m.get("discount") == null ? BigDecimal.ZERO : new BigDecimal(m.get("discount").toString())).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2,BigDecimal.ROUND_DOWN);

未完待续..........

http://www.lryc.cn/news/394543.html

相关文章:

  • C语言4 运算符
  • 【数据分析】Pandas_DataFrame读写详解:案例解析(第24天)
  • quill编辑器使用总结
  • 快手矩阵管理系统:引领短视频运营新潮流
  • 文心一言:探索AI写作的新境界
  • 认证资讯|Bluetooth SIG认证
  • 我国静止无功发生器(SVG)市场规模逐渐扩大 高压SVG为主流产品
  • 【漏洞复现】用友U8 CRM downloadfile 任意文件读取漏洞
  • 计算机网络 | 期末复习
  • 动手实操微软开源的GraphRAG
  • 【网络安全】实验七(ISA防火墙的规则设置)
  • 实验代码结构介绍
  • Java多线程不会?一文解决——
  • Mac上pyenv的安装及使用
  • 【SpringBoot】IDEA查看spring bean的依赖关系
  • 项目代码优化(1)——下单逻辑
  • 探索 WebKit 的缓存迷宫:深入理解其高效缓存机制
  • JVM:介绍
  • 和鲸“101”计划领航!和鲸科技携手北中医,共话医学+AI 实验室建设及创新人才培养
  • Linux 网络抓包工具tcpdump编译
  • 『C++成长记』string模拟实现
  • 【c++】C++ IO流
  • 解密智慧校园基础数据的学年管理功能
  • Python酷库之旅-第三方库Pandas(009)
  • VPN 的入门介绍
  • 移动UI: 什么特征会被认为是简洁风格,用案例告诉你
  • 除了伦敦外,英国还有这些热门留学城市
  • 2390. 从字符串中移除星号
  • UNION、UNION ALL、INTERSECT、MINUS
  • Perl 语言开发(九):深入探索Perl语言的文件处理