Java8 按照类属性去重

时间:2021-01-29 12:14:13

测试po

package com.shiwulian.test.po;

public class Person {

private String id;
private String name;
private Integer age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = age;
}

@Override
public String toString() {
return '['+id+','+name+','+age.toString()+']';
}

}

测试

package com.shiwulian.test.po;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class PersonTest {

public static void main(String[] args) {
long beginTime = 0;
long endTime = 0;
long costTime = 0;
Person p1 = new Person("1", "jack",15);
Person p2 = new Person("2", "tom",15);
Person p3 = new Person("3", "lala",16);
Person p4 = new Person("4", "lala",16);
Person p5 = new Person("5", "rose",14);

List<Person> persons = new ArrayList<>();
for (int i = 0; i < 1000000; i++) {
int yushu = i%5;
if(yushu == 1){
persons.add(p1);
}
if(yushu == 2){
persons.add(p2);
}
if(yushu == 3){
persons.add(p3);
}
if(yushu == 4){
persons.add(p4);
}
if(yushu == 0){
persons.add(p5);
}

}
List<Person> personUnique = null;
//function1
beginTime = System.currentTimeMillis();
personUnique = removeDupliType1(persons); 
endTime = System.currentTimeMillis();
costTime = endTime - beginTime;
System.out.println("function1 消耗时间:"+costTime);

//function2
beginTime = System.currentTimeMillis();
personUnique = removeDupliType2(persons); 
endTime = System.currentTimeMillis();
costTime = endTime - beginTime;
System.out.println("function2 消耗时间:"+costTime);

//function3
beginTime = System.currentTimeMillis();
personUnique = removeDupliType3(persons); 
endTime = System.currentTimeMillis();
costTime = endTime - beginTime;
System.out.println("function3 消耗时间:"+costTime);

//function4
beginTime = System.currentTimeMillis();
personUnique = removeDupliType4(persons); 
endTime = System.currentTimeMillis();
costTime = endTime - beginTime;
System.out.println("function4 消耗时间:"+costTime);

}

//function1
public static List<Person> removeDupliType1(List<Person> persons) {
Set<Person> personUnique = new TreeSet<>((o1, o2) -> o1.getName().compareTo(o2.getName()));
personUnique.addAll(persons);
return new ArrayList<>(personUnique);
}

//function2
public static List<Person> removeDupliType2(List<Person> persons) {
Set<String> nameSet = new HashSet<>();
List<Person> personUnique = persons.stream().filter(p -> nameSet.add(p.getName())).collect(Collectors.toList());
return personUnique;
}

//function3
public static List<Person> removeDupliType3(List<Person> persons) {

List<Person> personUnique = persons.stream().collect(collectingAndThen(
toCollection(() -> new TreeSet<>(comparing(Person::getName))), ArrayList::new)
); 
return personUnique;
}

//function4
public static List<Person> removeDupliType4(List<Person> persons) {
List<Person> personUnique = persons.stream().filter(distinctByKey(p -> ((Person) p).getId())).collect(Collectors.toList());
return personUnique;
}
public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
Map<Object, Boolean> map = new ConcurrentHashMap<>();
return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}

}

1000000条数据测试结果

Java8 按照类属性去重

1000条数据测试

Java8 按照类属性去重

测试总结:数据量较大的情况下(>1000000) function2 较快

数据量较小的情况下(<1000) function4较快 但是区别不大

以上凭借网上小伙伴的智慧,自己加以总结,希望大家批评指正。