Lambda表达式

it2024-03-21  72

Lambda表达式

简述:缺点:Lambda基本操作:遍历:基本语法 :stream() 操作: 排序 (sorted()):升序降序 去重 : (distinct())过滤:(filter())提取:(map())统计:求和(sum())平均值(average())最大值 (max())最小值 (min()) 分组:(groupingBy())单一分组:多重分组: 集合转换 (list,set,map)转换成 list (.toList())转换成 set (.toSet())转换成 map (.toMap(k,v))

简述:

​ java 8 提供的 Lambda 表达式 ,利用流和 Lambda 表达式对List集合进行处理。在开发的过程中,最重要的就是数据处理,而在java8以前,我们对List集合的处理就停留在传统的 for , if 这样的逻辑上,在某些方面是对数据处理是很繁琐的。而 java 8 提供的特性Lambda 表达式就很好的解决了这一问题。让代码更加的简洁,效率更加的高。

缺点:

​ (1).若不用并行计算,很多时候计算速度没有比传统的 for 循环快。

​ (2).容易使用 debug 模式调试。

​ (3).在 lambda 语句中直接强制类型转换不方便。

​ (4).不可以在 foreach 中修改 foreach 外面的值。

Lambda基本操作:
遍历:
list.forEach(student1 -> System.out.println(student1.getWorke()));
基本语法 :

​ 实体类 : : 属性 : 表示这个对象中的某个属性 。

​ 例如 : Student :: getAge

​ .collect (Collectors . 转换的类型) : 表示最后的结果转换成什么类型。

​ 例如 : .collect (Collectors . toList()) 表示最后的结果转换成 list 集合。

​ .collect (Collectors . toSet()) 表示最后的结果转换成 set 集合。

​ collect(Collectors.toMap(ppi003601 -> “键”, Function.identity()));

Function.identity() : 表示当前对象,并且递增取当前对象。

stream() 操作:
排序 (sorted()):
升序

根据对象中的年龄来排序

List<Student> collect = list.stream().sorted(Comparator.comparing(Student::getAge)).collect(Collectors.toList());
降序

根据对象中的年龄来降序

List<Student> collect1 = list.stream().sorted(Comparator.comparing(Student::getAge).reversed()).collect(Collectors.toList());
去重 : (distinct())
List<Student> distinctList = list.stream().distinct().collect(Collectors.toList());
过滤:(filter())
List<Student> collect2 = list.stream().filter(student1 -> student1.getAge() > 20).collect(Collectors.toList());
提取:(map())

解释 : 集合中提取出来单个对象中的某个属性。

Set<String> nameSet = list.stream().map(Student::getName).collect(Collectors.toSet());
统计:
求和(sum())
int sum = list.stream().mapToInt(Student::getAge).sum();
平均值(average())
double asDouble = list.stream().mapToInt(Student::getAge).average().getAsDouble();
最大值 (max())
int max = list.stream().mapToInt(Student::getAge).max().getAsInt();
最小值 (min())
int min = list.stream().mapToInt(Student::getAge).min().getAsInt();
分组:(groupingBy())
单一分组:
Map<String, List<Student>> collect3 = list.stream().collect(Collectors.groupingBy(Student::getName));
多重分组:
Map<String, Map<String, List<Student>>> collect4 = list.stream().collect(Collectors.groupingBy(Student::getName, Collectors.groupingBy(Student::getSex)));
集合转换 (list,set,map)
转换成 list (.toList())
List<String> nameList = list.stream().map(Student::getName).collect(Collectors.toList());
转换成 set (.toSet())
Set<String> nameSet = list.stream().map(Student::getName).collect(Collectors.toSet());
转换成 map (.toMap(k,v))

表示将这个集合转换成一个 key 为 name , value 为 Student 对象的Map 集合,前提要保证这个key唯一

Map<String, Student> studentMap = list.stream().collect(Collectors.toMap(s -> s.getName, Function.identity()));

当多个条件才能确定唯一的话,可以将这些条件拼接起来做为key

Map<String, Student> studentMap = list.stream().collect(Collectors.toMap(s -> s.getName + s.getAge + s.getClassName,Function.identity()));

推荐一个问答平台 : noproblems.

最新回复(0)