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 按照类属性去重的更多相关文章
- 外部配置属性值是如何被绑定到XxxProperties类属性上的?--SpringBoot源码(五)
注:该源码分析对应SpringBoot版本为2.1.0.RELEASE 1 前言 本篇接 SpringBoot是如何实现自动配置的?--SpringBoot源码(四) 温故而知新,我们来简单回顾一下上 ...
- JavaScript类属性
对象的类属性(class attribute)是一个字符串,用以表示对象的类型信息.ECMAScript3和ECMAScript5都未提供设置这个属性的方法,并只有一个间接的方法可以查询它.默认的to ...
- python 类属性与方法
Python 类属性与方法 标签(空格分隔): Python Python的访问限制 Python支持面向对象,其对属性的权限控制通过属性名来实现,如果一个属性有双下划线开头(__),该属性就无法被外 ...
- 【Python】[面性对象编程] 获取对象信息,实例属性和类属性
获取对象信息1.使用isinstance()判断class类型2.dir() 返回一个对象的所有属性和方法3.如果试图获取不存在的对象会抛出异常[AttributeError]4.正确利用对象内置函数 ...
- MyBatis学习总结(四)——解决字段名与实体类属性名不相同的冲突(转载)
本文转载自:http://www.cnblogs.com/jpf-java/p/6013307.html 在平时的开发中,我们表中的字段名和表对应实体类的属性名称不一定都是完全相同的,下面来演示一下这 ...
- mybatis框架下解决数据库中表的列的字段名和实体类属性不相同的问题
导包.... 实体类中的属性,getter,setter,tostring,构造等方法就不写了 private int id; private String orderNo; private floa ...
- 5.Swift枚举|结构体|类|属性|方法|下标脚本|继承
1. 枚举: ->在Swift中依然适用整数来标示枚举值,需搭配case关键字 enum Celebrity{ case DongXie,XiDu,Nandi,BeiGai } // 从左 ...
- Python进阶(三)--global和类属性
global关键字 一句话概括为:告诉python解释器,global声明的变量为全局作用域内定义的变量.解释器就会到全局作用域内寻找global定义的变量. python的类属性 类属性相当于其他O ...
- MyBatis入门学习教程-解决字段名与实体类属性名不相同的冲突
在平时的开发中,我们表中的字段名和表对应实体类的属性名称不一定都是完全相同的,下面来演示一下这种情况下的如何解决字段名与实体类属性名不相同的冲突. 一.准备演示需要使用的表和数据 CREATE TAB ...
随机推荐
- jedis配置
public interface IJedisClientFactory { Jedis getJedis(); } JedisClientFactoryImpl.java @Service publ ...
- python 函数 装饰器 内置函数
函数 装饰器 内置函数 一.命名空间和作用域 二.装饰器 1.无参数 2.函数有参数 3.函数动态参数 4.装饰器参数 三.内置函数 salaries={ 'egon':3000, 'alex':10 ...
- 前端 jQuery
一.jQuery是什么? <1>jQuery由美国人John Resig创建,至今已吸引了来自世界各地众多JavaScript高手加入其team. <2>jQuery是继pro ...
- scrapy的命令行
scrapy --help 列出帮助信息以及常用命令scrapy version 列出scrapy版本scrapy version -v 列出详细的scrapy版本以及各组件信息 scrapy sta ...
- vue-router详解——小白速会
一.概述 vue-router是Vue.js官方的路由插件,它和vue.js是深度集成的,适合用于构建单页面应用. vue的单页面应用是基于路由和组件的,路由用于设定访问路径,并将路径和组件映射起来. ...
- Java高级篇(三)——JDBC数据库编程
JDBC是连接数据库和Java程序的桥梁,通过JDBC API可以方便地实现对各种主流数据库的操作.本篇将介绍一下如何使用JDBC操作数据库(以MySQL为例). 一.JDBC JDBC制定了统一访问 ...
- [LeetCode] Best Time to Buy and Sell Stock with Transaction Fee 买股票的最佳时间含交易费
Your are given an array of integers prices, for which the i-th element is the price of a given stock ...
- 网易云安全两篇论文入选计算机视觉顶级会议ICCV
本文由 网易云发布. 10月22日至29日,全球计算机视觉顶尖专家们共聚威尼斯,参加ICCV2017国际计算机视觉大会,就领域内最新成果展开集中研讨,大会论文集也代表了计算机视觉领域最新的发展方向和 ...
- bzoj 5212: [Zjoi2018]历史
Description 九条可怜是一个热爱阅读的女孩子. 这段时间,她看了一本非常有趣的小说,这本小说的架空世界引起了她的兴趣. 这个世界有n个城市,这n个城市被恰好n?1条双向道路联通,即任意两个城 ...
- poj 1845 (逆元 + 约数和)
题意: 求A^B的所有约数(即因子)之和,并对其取模 9901再输出. 思路: A可以表示为A=(p1^k1)*(p2^k2)*(p3^k3)*....*(pn^kn) 其中pi均为素数 那么A的 ...