测试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 按照类属性去重的更多相关文章

  1. 外部配置属性值是如何被绑定到XxxProperties类属性上的?--SpringBoot源码(五)

    注:该源码分析对应SpringBoot版本为2.1.0.RELEASE 1 前言 本篇接 SpringBoot是如何实现自动配置的?--SpringBoot源码(四) 温故而知新,我们来简单回顾一下上 ...

  2. JavaScript类属性

    对象的类属性(class attribute)是一个字符串,用以表示对象的类型信息.ECMAScript3和ECMAScript5都未提供设置这个属性的方法,并只有一个间接的方法可以查询它.默认的to ...

  3. python 类属性与方法

    Python 类属性与方法 标签(空格分隔): Python Python的访问限制 Python支持面向对象,其对属性的权限控制通过属性名来实现,如果一个属性有双下划线开头(__),该属性就无法被外 ...

  4. 【Python】[面性对象编程] 获取对象信息,实例属性和类属性

    获取对象信息1.使用isinstance()判断class类型2.dir() 返回一个对象的所有属性和方法3.如果试图获取不存在的对象会抛出异常[AttributeError]4.正确利用对象内置函数 ...

  5. MyBatis学习总结(四)——解决字段名与实体类属性名不相同的冲突(转载)

    本文转载自:http://www.cnblogs.com/jpf-java/p/6013307.html 在平时的开发中,我们表中的字段名和表对应实体类的属性名称不一定都是完全相同的,下面来演示一下这 ...

  6. mybatis框架下解决数据库中表的列的字段名和实体类属性不相同的问题

    导包.... 实体类中的属性,getter,setter,tostring,构造等方法就不写了 private int id; private String orderNo; private floa ...

  7. 5.Swift枚举|结构体|类|属性|方法|下标脚本|继承

    1. 枚举: ->在Swift中依然适用整数来标示枚举值,需搭配case关键字 enum  Celebrity{  case DongXie,XiDu,Nandi,BeiGai }  // 从左 ...

  8. Python进阶(三)--global和类属性

    global关键字 一句话概括为:告诉python解释器,global声明的变量为全局作用域内定义的变量.解释器就会到全局作用域内寻找global定义的变量. python的类属性 类属性相当于其他O ...

  9. MyBatis入门学习教程-解决字段名与实体类属性名不相同的冲突

    在平时的开发中,我们表中的字段名和表对应实体类的属性名称不一定都是完全相同的,下面来演示一下这种情况下的如何解决字段名与实体类属性名不相同的冲突. 一.准备演示需要使用的表和数据 CREATE TAB ...

随机推荐

  1. 使用Spring Initializr创建项目

    Spring initializr 是Spring 官方提供的一个很好的工具,可以用来用来创建一个Spring boot 的项目.可以选择使用Maven管理或者使用Gradle管理,还可以选择使用的编 ...

  2. ssh整合之一spring的单独运行环境

    这是本人第一次写博客,不足之处,还希望各位园友指出,在此先谢谢各位了! 先说我们的这三大框架,即struts,spring,hibernate,我们要进行整合的话,第一步是先单独搭建我们的Spring ...

  3. Linux下安装jmeter

    一.用Xftp上传apache-jmeter-2.13.tgz到Linux系统里 二.解压apache-jmeter-2.13.tgz,tar xzfv apache-jmeter-2.13.tgz ...

  4. C# 客户端程序调用外部程序的三种实现

    简介 我们用C#来开发客户端程序的时候,总会不可避免的需要调用外部程序或者访问网站,本篇博客介绍了三种调用外部应用的方法,供参考 实现 第一种是利用shell32.dll,实现ShellExecute ...

  5. 前端之HTML内容

    一.HTML介绍 1.Web服务本质 当我们在浏览器中输入一个url后打开一个页面这个过程实质是一个网络编程中的sockt服务端接受指令并发送指令的一个过程.本质顺序是: 浏览器发请求——>HT ...

  6. Centos6.9minimal安装图形化界面

    有时我们会用到图形化界面来操作,下面介绍是在虚拟机上安装Centos6.9minimal版安装图形化界面(其他系统版本都类似吧,,,),如果是在物理机上安装进入的话要用的远程桌面工具VNC. VNC安 ...

  7. Hive:表1inner join表2结果group by优化

    问题背景 最近遇到一个比较棘手的事情:hive sql优化: lib表(id,h,soj,noj,sp,np)         --一个字典表 mitem表(md,mt,soj,noj,sp,np)- ...

  8. hadoop fs:du统计hdfs文件(目录下文件)大小的用法

    hadoop fs 更多用法,请参考官网:http://hadoop.apache.org/docs/r1.0.4/cn/hdfs_shell.html 以下是我的使用统计文件时使用的记录: [t@d ...

  9. requests post一个json数据

    # post一个json数据 import requests headers={ "Accept":"application/json, text/plain, */*& ...

  10. urllib.parse

    1 url分解 import urllib.parse result = urllib.parse.urlparse('http://www.baidu.com') print(result) 结果为 ...