Lambda表达式(2)|(List对象集合去重)
前言
本章节主要介绍java8的新特性Lambda表达式基于List实现根据object字段的对数据进行去重。
一、java8中Lambda是什么?
Lambda 表达式是 JDK8 的一个新特性,可以取代大部分的匿名内部类,写出更优雅的 Java 代码,尤其在集合的遍历和其他集合操作中,可以极大地优化代码结构。语法形式为 () -> {},其中 () 用来描述参数列表,{} 用来描述方法体,-> 为 lambda运算符。
二、数据准备
1.创建实体
创造Object实体
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class EmployeeDto {
private String id;
private String name;
private String no_id;
private String dept;
private int year;
private EmpInfoPo info;
}
2.初始化数据
构建List实体数据
{"dept":"研发部1","id":"001","name":"员工1号","no_id":"YF001","year":2}
{"dept":"研发部2","id":"002","name":"员工2号","no_id":"YF001","year":5}
{"dept":"研发部3","id":"003","name":"员工3号","no_id":"YF001","year":7}
{"dept":"研发部4","id":"004","name":"员工4号","no_id":"YF001","year":6}
{"dept":"研发部5","id":"005","name":"员工5号","no_id":"YF001","year":4}
{"dept":"研发部6","id":"006","name":"员工1号","no_id":"YF001","year":2}
三、Lambda方式去重
1.对象去重方式
集合方式排序、stream方式排序对比
Collectors.collectingAndThen()数据流转处理函数
distinctByKey()自定义比较器
/**
* 函数去重 利用TreeSet无重复特性
* collect 收集器
* Collectors 收集器的方法
*/
public static void exceptRepeatV1(List<EmployeeDto> employeeList){
ArrayList<EmployeeDto> collect = employeeList
.stream()
.collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(EmployeeDto::getName))), ArrayList::new));
print(collect);
}
/**
* 自定义方式去重
* @param employeeList
*/
public static void exceptRepeatV2(List<EmployeeDto> employeeList){
List<EmployeeDto> collect = employeeList.stream()
.filter(distinctByKey(e -> e.getName()))
.collect(Collectors.toList());
print(collect);
}
/**
* lambda方式去重,存在重复的保留第一条
* @param employeeList
*/
public static void exceptRepeatV2(List<EmployeeDto> employeeList){
List<EmployeeDto> list= employeeList.stream()
.collect(Collectors.toMap(EmployeeDto::getId, p -> p, (p1, p2) -> p1))
.values()
.stream()
.collect(Collectors.toList());
}
/**
* 自定义比较方法
* @param keyExtractor
* @param <T>
* @return
*/
private static <T> Predicate<T> distinctByKey(Function<? super T,?> keyExtractor){
Map<Object,Boolean> map=new ConcurrentHashMap<>();
return t -> map.putIfAbsent(keyExtractor.apply(t),Boolean.TRUE)==null;
}
去重结果
{"dept":"研发部1","id":"001","name":"员工1号","no_id":"YF001","year":2}
{"dept":"研发部2","id":"002","name":"员工2号","no_id":"YF001","year":5}
{"dept":"研发部3","id":"003","name":"员工3号","no_id":"YF001","year":7}
{"dept":"研发部4","id":"004","name":"员工4号","no_id":"YF001","year":6}
{"dept":"研发部5","id":"005","name":"员工5号","no_id":"YF001","year":4}
2.某个字段去重收集
例如:有一份用户排名名单,现在想要获取用户的id,每个用户排名取最靠前排名
LinkedHashSet数据不重复,且有序(插入的顺序不会变)。
//初始化参数
List<User> userList = new ArrayList<>();
userList.add(new User(4, "张三", 25));
userList.add(new User(2, "李四", 30));
userList.add(new User(3, "王五", 35));
userList.add(new User(2, "赵六", 40));
//去重代码
LinkedHashSet<Integer> collect = userList.stream().map(u -> u.getId()).collect(Collectors.toCollection(LinkedHashSet::new));
System.out.println("去重后的ID列表:" + collect);
去重结果
去重后的ID列表:[4, 2, 3]
2.非对象去重方式
集合方式排序、stream方式排序对比
//参数
List<Integer> userList1 = Lists.newArrayList(1,2,3,1,5);
//去重
List<Integer> dis = userList1.stream()
.distinct()
.collect(Collectors.toList());
//输出
[1, 2, 3, 5]
总结
以上就是Java8新特性Lambda表达式实现集合List去重常见的两种场景。后续会慢慢完善Lambda表达式的去重遍历过滤等