今天写需求时碰到一个问题,用户可以通过表格导入设备数据,每个设备的资产编号不可重复,后端拿到表格数据要先进行数据判重,如果资产编号重复,重复的数据被抛出,并将失败数据添加失败原因后返回给前端展示。此处展示如何取出list集合中重复数据。
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
public class DeduplicationUtil {
/**
* 自定义函数去重(采用 Predicate函数式判断,采用 Function获取比较key)
* 内部维护一个 ConcurrentHashMap,并采用 putIfAbsent特性实现
*/
public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
Map<Object,Boolean> seen = new ConcurrentHashMap<>(2);
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
/**
* 只获取重复的数据
*/
public static <T> Predicate<T> distinctNotByKey(Function<? super T, ?> keyExtractor) {
Map<Object,Boolean> seen = new ConcurrentHashMap<>(2);
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) != null;
}
}
public class test {
public static void main(String[] args) {
//创建一个list用来测试
List<Student> testList = new ArrayList<Student>();
Student student = new Student();
Student student2 = new Student();
Student student3 = new Student();
student.setStuName("张三");
student2.setStuName("王二");
student3.setStuName("王二");
testList.add(student);
testList.add(student2);
testList.add(student3);
List<Student> repeatList = new ArrayList<Student>();//用于存放重复的元素的list
// 第一种方法:两个循环
for (int i = 0; i < testList.size() - 1; i++) {
for (int j = testList.size() - 1; j > i; j--) {
if (testList.get(j).getStuName().equals(testList.get(i).getStuName())) {
repeatList.add(testList.get(j));//把相同元素加入list(找出相同的)
testList.remove(j);//删除重复元素
}
}
}
//第二种方法:利用map.containsKey()
Map<String, Integer> map = new HashMap<>();
for (Student s : testList) {
//1:map.containsKey() 检测key是否重复
if (map.containsKey(s.getStuName())) {
Student student1 = new Student();
student1.setStuName(s.getStuName());
repeatList.add(student1);//获取重复的学生名称并存放到新的list中
Integer num = map.get(s.getStuName());
map.put(s.getStuName(), num + 1);
} else {
map.put(s.getStuName(), 1);
}
}
//第三种方式 利用工具类
List<String> repeatList = testList.stream().filter(DeduplicationUtil.distinctNotByKey(Student::getStuName)).map(Student::getStuName).collect(Collectors.toList());
//去除重复元素
if (CollectionUtil.isNotEmpty(repeatList)) {
Iterator<Student> iterator = testList.iterator();
while (iterator.hasNext()) {
Student next = iterator.next();
if (repeatList.contains(next.getStuName())) {
iterator.remove();
}
}
}
}
}
class Student {
private String stuName;
private Long studentId;
public Student(String stuName, Long studentId) {
this.stuName = stuName;
this.studentId = studentId;
}
public Student() {
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public Long getStudentId() {
return studentId;
}
public void setStudentId(Long studentId) {
this.studentId = studentId;
}
}