【java】lambda表达式之List操作

本文详细介绍了如何使用Java Lambda表达式对List进行去重、过滤、抽取、分组、计数、获取最值、匹配和求和等操作。通过实例展示了Lambda在处理学生对象列表时的各种功能,包括按姓名去重、按条件过滤、转换为新对象、按属性分组以及计算属性的最值等。这些技巧对于提升Java代码的简洁性和效率非常有帮助。
摘要由CSDN通过智能技术生成

【java】lambda表达式之List操作

去重

//按学生姓名去重
//可能会改变原有list的顺序
List<Student> list = studentList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Student::getName))), ArrayList::new));

//直接去重
List<Student> list = studentList.stream().distinct().collect(Collectors.toList());

过滤

//按学生姓名过滤
List<Student> list = studentList.stream().filter(item -> "张三".equals(item.getName())).collect(Collectors.toList());

抽取

//按学生姓名抽取形成新对象Person
List<Person> personList = studentList.stream().map(s->{
					Person person = new Person();
					person .setName(s.getName());
					return person ;
				}).collect(Collectors.toList());
//按学生id抽取形成map集合
Map<Long, Person> personMap = studentList.stream().collect(Collectors.toMap(s -> s.getId(), s -> s));
//按学生id抽取形成map集合,取第一个
Map<Long, Person> personMap = studentList.stream().collect(Collectors.toMap(s -> s.getId(), s -> s,(first,last)->first));
//按学生id抽取形成set集合
Set<Long> idSet = studentList.stream().map(s-> s.getId()).collect(Collectors.toSet());

分组

//按学生姓名分组
Map<String, List<Student>> map = students.stream().collect(Collectors.groupingBy(Student::getName));
//分组后保持有序
LinkedHashMap<String, ArrayList<Student>> collect = studentList.stream().collect(Collectors.groupingBy(Student::getName, LinkedHashMap::new, Collectors.toCollection(ArrayList::new)));
//按学生姓名分组,List存放学号
Map<String, List<Long>> map = students.stream().collect(Collectors.groupingBy(Student::getName, Collectors.mapping(Student::getId, Collectors.toList())));
//按学生姓名分组,Set存放学号
Map<String, Set<Long>> map = students.stream().collect(Collectors.groupingBy(Student::getName, Collectors.mapping(Student::getId, Collectors.toSet())));

计数

Map<String, Long> map = students.stream().collect(Collectors.groupingBy(Student::getName, Collectors.counting()));

最值

//最小
Integer min = studentList.stream().map(Student::getAge).min(Student::compareTo).get();

//最大
Integer max = studentList.stream().map(Student::getAge).max(Student::compareTo).get();

// 最大对象
User max = userList.stream().max(Comparator.comparing(Student::getAge)).get();
// 最小对象
User min = userList.stream().min(Comparator.comparing(Student::getAge)).get();

匹配

//查找list中是否都是张三
boolean result = studentList.stream().allMatch((s) -> s.getName().equals("张三"));
//查找list中是否有一个是张三
boolean result = studentList.stream().anyMatch((s) -> s.getName().equals("张三"));
//判断list中没有张三
boolean result = studentList.stream().noneMatch((s) -> s.getName().equals("张三"));

求和

Integer ageSum = studentList.stream().mapToInt(Student::getAge).sum();
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王佑辉

老板,赏点吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值