1、废话不多说直接上代码,第一种采取的是重写hashCode和equals的方法,后面两种都是使用java8新特性
2、对象代码:
@Data
public class ImportResume {
private Integer age;
private String phone;
//用于去重
@Override
public boolean equals(Object object){
ImportResume importResume=(ImportResume)object;
return phone.equals(importResume.phone);
}
@Override
public int hashCode(){
String in = phone;
return in.hashCode();
}
}
3、java代码:
public void save(List<ImportResume> list) {
long l = System.currentTimeMillis();
System.out.println("去重前"+list.size());
Set<ImportResume> set=new HashSet<>(list);
System.out.println("set"+set.size());
List<ImportResume> resumeList=new ArrayList<>(set);
long l1 = System.currentTimeMillis();
long l2 = l1 - l;
System.out.println("去重后"+resumeList.size()+"时间:"+l2);
long l3 = System.currentTimeMillis();
ArrayList<ImportResume> collect = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(ImportResume::getPhone))), ArrayList::new));
long l4 = System.currentTimeMillis();
long l5 = l4 - l3;
System.out.println("炫酷的stream"+collect.size()+"时间:"+l5);
long l7 = System.currentTimeMillis();
List<String> names = new ArrayList<>();
List<ImportResume> collect1 = list.stream().filter(item -> {
boolean flag = !names.contains(item.getPhone());
names.add(item.getPhone());
return flag;
}).collect(Collectors.toList());
long l8 = System.currentTimeMillis();
long l6 = l8 - l7;
System.out.println("炫酷的lambada"+collect1.size()+"时间:"+l6);
}
测试得到第一种方法速度最快,下来是第二种
版权声明:本文为weixin_43691942原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。