List中根据某个实体的属性去重
一、简介
最近在项目中有需要对List<T>我们自定义对象中的某个字段属性进行去重,于是封装了一些方法来进行去重。下面将总结三种方法根据某个字段属性进行List去重的方法。
二、去重
【a】工具类:ListDistinctUtils.java
public class ListDistinctUtils {/*** 使用TreeSet结合Comparator比较器实现List<Student>中stuName字段去重 </br>* * @author weixiaohuai* @modify_by weixiaohuai* @param oldList* @return*/public static <T extends Student> List<T> distinctBySet01(List<T> oldList) {Set<T> set = new TreeSet<>(new Comparator<T>() {@Overridepublic int compare(T o1, T o2) {/*** compareTo : 两个相同数据类型的比较,两个不同类型的数据不能用此方法来比较 <br/>* 如果指定的数与参数相等返回: 0 <br/>* 如果指定的数小于参数返回 : -1 <br/>* 如果指定的数大于参数返回 : 1 <br/>*/return o1.getStuName().compareTo(o2.getStuName());}});set.addAll(oldList);return new ArrayList<>(set);}/*** 使用Set实现List<Student>中stuName字段去重 </br>* * @author weixiaohuai* @modify_by weixiaohuai* @param oldList* @return*/public static <T extends Student> List<T> distinctBySet02(List<T> oldList) {Set<String> set = new HashSet<>();List<T> newList = new ArrayList<>();for (T t : oldList) {if (!set.contains(t.getStuName())) {set.add(t.getStuName());newList.add(t);}}return newList;}/*** 使用Map实现List<Student>中stuName字段去重</br>* * @author weixiaohuai* @modify_by weixiaohuai* @param oldList* @return*/public static <T extends Student> List<T> distinctByMap(List<T> oldList) {Map<String, Object> existMap = new HashMap<String, Object>();List<T> newList = new ArrayList<>();for (T t : oldList) {String name = null != existMap.get(t.getStuName()) ? existMap.get(t.getStuName()).toString() : "";// 如果name为空表示不存在if (StringUtils.isBlank(name)) {existMap.put(t.getStuName(), t.getStuName());newList.add(t);}}return newList;}}
【b】测试用例: Test.java
public class Test {public static void main(String[] args) {/*** 根据List<Student>中Student对象属性(stuName)进行去重*/List<Student> studentsList = new ArrayList<>();Student student1 = new Student(UUID.randomUUID().toString(), "zhangsan", 12);Student student2 = new Student(UUID.randomUUID().toString(), "lisi", 22);Student student3 = new Student(UUID.randomUUID().toString(), "zhangsan", 12);Student student4 = new Student(UUID.randomUUID().toString(), "wangwu", 15);studentsList.add(student1);studentsList.add(student2);studentsList.add(student3);studentsList.add(student4);System.out.println("去重前:" + studentsList);List<Student> newStudentList = ListDistinctUtils.distinctBySet01(studentsList);System.out.println("[distinctBySet01]去重后:" + newStudentList);for (Student student : newStudentList) {System.out.println("学生姓名:" + student.getStuName());}List<Student> newStudentList2 = ListDistinctUtils.distinctBySet02(studentsList);System.out.println("[distinctBySet02]去重后:" + newStudentList2);for (Student student : newStudentList2) {System.out.println("学生姓名:" + student.getStuName());}List<Student> newStudentList3 = ListDistinctUtils.distinctByMap(studentsList);System.out.println("[distinctByMap]去重后:" + newStudentList3);for (Student student : newStudentList3) {System.out.println("学生姓名:" + student.getStuName());}}
}
【c】运行结果:
去重前:[Student [stuId=0ef7d96a-b83a-4810-add1-9d143edf643e, stuName=zhangsan, stuAge=12], Student [stuId=29f119e2-b8ed-41fa-b7d0-d6e7fc87f674, stuName=lisi, stuAge=22], Student [stuId=05cf5e6a-3a73-43f9-8094-f3887a26691f, stuName=zhangsan, stuAge=12], Student [stuId=901cbd1b-da67-4058-b406-6c369df51c49, stuName=wangwu, stuAge=15]]
[distinctBySet01]去重后:[Student [stuId=29f119e2-b8ed-41fa-b7d0-d6e7fc87f674, stuName=lisi, stuAge=22], Student [stuId=901cbd1b-da67-4058-b406-6c369df51c49, stuName=wangwu, stuAge=15], Student [stuId=0ef7d96a-b83a-4810-add1-9d143edf643e, stuName=zhangsan, stuAge=12]]
学生姓名:lisi
学生姓名:wangwu
学生姓名:zhangsan
[distinctBySet02]去重后:[Student [stuId=0ef7d96a-b83a-4810-add1-9d143edf643e, stuName=zhangsan, stuAge=12], Student [stuId=29f119e2-b8ed-41fa-b7d0-d6e7fc87f674, stuName=lisi, stuAge=22], Student [stuId=901cbd1b-da67-4058-b406-6c369df51c49, stuName=wangwu, stuAge=15]]
学生姓名:zhangsan
学生姓名:lisi
学生姓名:wangwu
[distinctByMap]去重后:[Student [stuId=0ef7d96a-b83a-4810-add1-9d143edf643e, stuName=zhangsan, stuAge=12], Student [stuId=29f119e2-b8ed-41fa-b7d0-d6e7fc87f674, stuName=lisi, stuAge=22], Student [stuId=901cbd1b-da67-4058-b406-6c369df51c49, stuName=wangwu, stuAge=15]]
学生姓名:zhangsan
学生姓名:lisi
学生姓名:wangwu
List中根据某个实体的属性去重相关推荐
- Java中集合中根据对象的某个属性去重
场景 有一个List对象集合,根据每一个对象的某个属性去重. 实现 去重方法: private static ArrayList<FlightResult> removeDuplicate ...
- ElementUI中显示是否以及SpringBoot中怎样存储实体类属性和数据库怎样设计字段
场景 Vue+ElementUI+SpringBoot+Mysql 需要设计一些属性为是否,即只有两个选择的属性字段. 注: 博客: https://blog.csdn.net/badao_liuma ...
- java中给对象的List集合去重的几种方法(Lambda)
java中给对象的List集合去重的几种方法 前言 一.lambda表达式的去重方式 二.Stream API中的collect去重方法 三.Stream API 中的distinct方法去重 前言 ...
- 概念结构设计( 实体与属性的划分原则、E-R图的集成)、逻辑结构设计(任务、步骤、转换方法、数据模型的优化、设计用户子模式、物理结构设计)....
概念结构设计 主要内容:[1]E-R图中如何确定实体与属性[2]集成E-R图时如何解决冲突 1 实体与属性的划分原则 [1]具体的应用环境常常对 ...
- ef mysql自动更新_EF Core中怎么实现自动更新实体的属性值到数据库
我们在开发系统的时候,经常会遇到这种需求数据库表中的行被更新时需要自动更新某些列. 数据库 比如下面的Person表有一列UpdateTime,这列数据要求在行被更新后自动更新为系统的当前时间. Pe ...
- adf4351_在ADF实体PK属性中使用MySQL自动增量PK列
adf4351 大家好. 继续进行ADF + MySQL解决方法系列,今天我们将看到需要做些什么才能将MySQL PK自动增量列与ADF实体PK属性一起使用. 如果我们使用的是Oracle数据库,则可 ...
- java 实体属性个数_?Java中比较实用实体转换工具介绍
Java中比较实用实体转换工具介绍 文中源码地址 大家一般编码过程中,经常会遇到DO对象转化为DTO对象,对象和对象之间转换一般需要用到转换工具,毕竟使用getter/setter太过麻烦 DO:D ...
- adf 自动输稿器_在ADF实体PK属性中使用MySQL自动增量PK列
adf 自动输稿器 大家好. 继续进行ADF + MySQL解决方法系列,今天我们将看到需要做些什么才能将MySQL PK自动增量列与ADF实体PK属性一起使用. 如果使用的是Oracle数据库,则可 ...
- 在ADF实体PK属性中使用MySQL自动增量PK列
大家好. 继续进行ADF + MySQL解决方法系列,今天我们将看到要使用MySQL PK自动增量列和ADF实体PK属性来进行的工作. 如果使用的是Oracle数据库,则可以使用oracle.jbo. ...
- swagger中没有新加的实体类属性(新加的属性在swagger中不显示)问题
昨晚在实体类中新加了三个之后就下班了,今天回来用swagger的时候发现swagger中对于这个实体类中的新增属性没显示,纳闷了,我寻思这也不能扒拉出锅了啊,回去看一下属性上的注解,如图 这也没错啊, ...
最新文章
- win10商店下载位置_开始菜单终变身但仍不完美!用这些利器让Win10改头换面吧...
- html如何获取请求头变量的值。_如何使用 Python 爬取微信公众号文章
- Oracle条件查询语句-where
- web前端----JavaScript的DOM(二)
- 三目运算符_C语言知识点:运算符的优先级和结合性
- linux内核:__user,__kernel,__safe,__force,__iomem
- postMessage可太有用了
- 使用 IIS 进行 ASP.NET 2.0 成员/角色管理(2):实现
- 翻译: 欢迎使用 KITTI Vision Benchmark Suite!
- 破解visio2013记录
- Ubuntu/Win10双系统安全删除Ubuntu的方法
- 坚持努力,在黑暗中寻找光明——我的2014
- python 爬取图片、没有后缀名_python爬虫,图片是无格式的
- Linux 操作系统的体系结构
- presenting view controller
- i5集显和独显的区别_核显和独显、集成显卡有什么区别,那个好!
- c语言编程 元旦快乐,不同编程语言打印“元旦快乐!
- 李刚 疯狂Python讲义 读书笔记
- poi设置日期格式和数字格式
- 云IDE 使用体验,git项目快速启动实践