Java8 按照类属性去重

Posted 哈哈粑

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java8 按照类属性去重相关的知识,希望对你有一定的参考价值。

测试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条数据测试结果

1000条数据测试

 

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

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

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

以上是关于Java8 按照类属性去重的主要内容,如果未能解决你的问题,请参考以下文章

java8 List根据元素对象属性去重

Java8 list根据对象某个属性去重

java8 list中根据对象多属性去重

Java8:按照对象的属性,对List集合分组

java8 List去重

手把手教你如何用java8新特性将List中按指定属性排序,过滤重复数据