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 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 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 removeDupliType1(List persons) {
Set personUnique = new TreeSet<>((o1, o2) -> o1.getName().compareTo(o2.getName()));
personUnique.addAll(persons);
return new ArrayList<>(personUnique);
}

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

//function3
public static List removeDupliType3(List persons) {

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

//function4
public static List removeDupliType4(List persons) {
List personUnique = persons.stream().filter(distinctByKey(p -> ((Person) p).getId())).collect(Collectors.toList());
return personUnique;
}
public static Predicate 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较快 但是区别不大

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值