初始化List
定义Student对象
public class Student extends Person implements Serializable {
private String name;
private Integer age;
private Integer testScore;
public Student(String name, Integer age, Integer testScore) {
this.name = name;
this.age = age;
this.testScore = testScore;
}
//省略set get方法
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return Objects.equals(getName(), student.getName())
&& Objects.equals(getAge(), student.getAge())
&& Objects.equals(getTestScore(), student.getTestScore());
}
@Override
public int hashCode() {
return Objects.hash(getName(), getAge(), getTestScore());
}
}
初始化包含重复元素的集合List
allStudents = new ArrayList<Student>() {
{
for (int i = 0; i < 100000; i++) {
add(new Student("爱琴孩", i, 99));
add(new Student("爱琴孩", i, 99));
}
}};
通过contains来进行去重
@Test
public void testDistinctByContains() {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
List<Student> distinctStudents = new ArrayList<>();
System.out.println("去重前" + allStudents.size());
for (Student student : allStudents) {
if (distinctStudents.contains(student)) {
continue;
}
distinctStudents.add(student);
}
System.out.println("去重后" + distinctStudents.size());
stopWatch.stop();
System.out.println("采用contains去重耗时" + stopWatch.getTotalTimeMillis());
}
运行结果
看看contains的方法源码,通过遍历来进行匹配
通过HashSet来去重
@Test
public void testDistinctByHashSet() {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
System.out.println("去重前" + allStudents.size());
List<Student> distinctStudents = new ArrayList<>(new HashSet<>(allStudents));
System.out.println("去重后" + distinctStudents.size());
stopWatch.stop();
System.out.println("采用Hashset去重耗时" + stopWatch.getTotalTimeMillis());
}
运行结果
看看HashSet中的contains源码
containsKey如下
通过Stream来去重
@Test
public void testDistinctByStream() {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
System.out.println("去重前" + allStudents.size());
List<Student> distinctStudents = allStudents.stream().distinct().collect(Collectors.toList());
System.out.println("去重后" + distinctStudents.size());
stopWatch.stop();
System.out.println("采用stream去重耗时" + stopWatch.getTotalTimeMillis());
}
运行结果如下
结论
去重效率,HashSet去重>Stream.distinct()去重>遍历contains去重