使用Set集合对List集合进行去重

article/2025/9/22 8:41:41
使用Set集合对List集合进行去重

前段时间正好遇到这样一个需求:我们的支付系统从对方系统得到存储明细对象的List集合,存储的明细对象对象的明细类简化为如下TradeDetail类,需求是这样的,我要对称List集合进行去重,这里的去重的意思是只要对象对象中的accountNo账号是相同的,就认为明细对象是相同的,去重之后要求是List集合或者Set集合。

在进行上面的需求对象去重之前,先来看很简单的List集合去重:
package com.qdfae.jdk.collections;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

import org.junit.Test;

import com.qdfae.jdk.domain.TradeDetail;
import com.qdfae.jdk.domain.User;

/**
* 使用Set集合对List集合进行去重
*
* @author hongwei.lian
* @date 2018年3月9日 下午11:15:52
*/
public class SetTest {
/**
* List集合的泛型为Integer类型
*
* @author hongwei.lian
* @date 2018年3月9日 下午11:32:53
*/
@Test
public void testListToSet1() {
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(1);
Set<Integer> set = new HashSet<>(list);
System.out.println("list的个数为:" + list.size() + "个");
list.forEach(System.out::println);
System.out.println("set的个数为:" + set.size() + "个");
set.forEach(System.out::println);
}
/**
* List集合的泛型为String类型
*
* @author hongwei.lian
* @date 2018年3月9日 下午11:34:15
*/
@Test
public void testListToSet2() {
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("a");
Set<String> set = new HashSet<>(list);
System.out.println("list的个数为:" + list.size() + "个");
list.forEach(System.out::println);
System.out.println("set的个数为:" + set.size() + "个");
set.forEach(System.out::println);
}

/**
* List集合的泛型为自定义类型User
* 需求是userCode一样的便是同一个对象
*
* @author hongwei.lian
* @date 2018年3月10日 上午12:32:12
*/
@Test
public void testListToSet3() {
List<User> list = new ArrayList<>();
list.add(new User(1,"用户一","600001"));
list.add(new User(2,"用户二","600002"));
list.add(new User(3,"用户一","600001"));
list.add(new User(4,"用户一","600001"));
Set<User> set = new HashSet<>(list);
System.out.println("list的个数为:" + list.size() + "个");
list.forEach(System.out::println);
System.out.println("set的个数为:" + set.size() + "个");
set.forEach(System.out::println);
}
}
上面测试使用到的User类源码:
package com.qdfae.jdk.domain;

import java.io.Serializable;

/**
* User实体类
*
* @author hongwei.lian
* @date 2018年3月10日 上午12:33:22
*/
public class User implements Serializable {
private static final long serialVersionUID = -7629758766870065977L;

/**
* 用户ID
*/
private Integer id;
/**
* 用户姓名
*/
private String userName;
/**
* 用户代码
*/
private String userCode;
public User() {}
public User(Integer id, String userName, String userCode) {
this.id = id;
this.userName = userName;
this.userCode = userCode;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getUserCode() {
return userCode;
}

public void setUserCode(String userCode) {
this.userCode = userCode;
}

@Override
public String toString() {
return "User [id=" + id + ", userName=" + userName + ", userCode=" + userCode + "]";
}
}
依次运行上面三个方法的结果是:
testListToSet1()方法结果:

testListToSet2()方法结果:

testListToSet3()方法结果:

上面的testListToSet1()方法和testListToSet2()方法可以去重,那为什么testListToSet3()方法就不能去重呢?仔细想想就会知道,两个对象的地址值不一样,怎么会认为是相同的去重呢,再往深处想,就会想到Object类的hashCode()方法和equals()方法,这两个方法决定了两个对象是否相等。Integer类和String类之所以可以进行去重,是因为这两个类都重写了父类Object类中的hashCode()方法和equals()方法,具体的代码可以去查看JDK源码,这里不再赘述。到这里我们就知道User对象不能去重的原因所在,那么我们根据需求在User类中重写hashCode()方法和equals()方法,重写后的User类源码如下:
package com.qdfae.jdk.domain;

import java.io.Serializable;

/**
* User实体类
*
* @author hongwei.lian
* @date 2018年3月10日 上午12:33:22
*/
public class User implements Serializable {
private static final long serialVersionUID = -7629758766870065977L;

/**
* 用户ID
*/
private Integer id;
/**
* 用户姓名
*/
private String userName;
/**
* 用户代码
*/
private String userCode;
public User() {}
public User(Integer id, String userName, String userCode) {
this.id = id;
this.userName = userName;
this.userCode = userCode;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getUserCode() {
return userCode;
}

public void setUserCode(String userCode) {
this.userCode = userCode;
}
/**
* 针对userCode重写hashCode()方法
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((userCode == null) ? 0 : userCode.hashCode());
return result;
}

/**
* 针对userCode重写equals()方法
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (userCode == null) {
if (other.userCode != null)
return false;
} else if (!userCode.equals(other.userCode))
return false;
return true;
}

@Override
public String toString() {
return "User [id=" + id + ", userName=" + userName + ", userCode=" + userCode + "]";
}
}
我们再次运行testListToSet3()方法结果:

这一次符合我们的需求,接下里再来看开头提出的需求。
准备:
TradeDetail类源码:
package com.qdfae.jdk.domain;

import java.io.Serializable;
import java.math.BigDecimal;

/**
* 交易明细
*
* @author hongwei.lian
* @date 2018年3月10日 下午2:44:35
*/
public class TradeDetail implements Serializable {
private static final long serialVersionUID = 3386554986241170136L;

/**
* 交易明细主键
*/
private Integer id;
/**
* 账号
*/
private String accountNo ;
/**
* 账户名称
*/
private String accountName;
/**
* 交易金额(+表示入金,-表示出金)
*/
private BigDecimal balance;

public TradeDetail() {}
public TradeDetail(Integer id, String accountNo, String accountName, BigDecimal balance) {
this.id = id;
this.accountNo = accountNo;
this.accountName = accountName;
this.balance = balance;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}
public String getAccountNo() {
return accountNo;
}

public void setAccountNo(String accountNo) {
this.accountNo = accountNo;
}

public String getAccountName() {
return accountName;
}

public void setAccountName(String accountName) {
this.accountName = accountName;
}

public BigDecimal getBalance() {
return balance;
}

public void setBalance(BigDecimal balance) {
this.balance = balance;
}
/**
* 针对accountNo重写hashCode()方法
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((accountNo == null) ? 0 : accountNo.hashCode());
return result;
}

/**
* 针对accountNo重写equals()方法
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TradeDetail other = (TradeDetail) obj;
if (accountNo == null) {
if (other.accountNo != null)
return false;
} else if (!accountNo.equals(other.accountNo))
return false;
return true;
}

@Override
public String toString() {
return "TradeDetail [id=" + id + ", accountNo=" + accountNo + ", accountName=" + accountName + ", balance="
+ balance + "]";
}

}
我们首先来按照上面的想法根据需求重写TradeDetail类的hashCode()方法和equals()方法,上面已经给出重写后的TradeDetail类。
我有三种实现方案如下:
package com.qdfae.jdk.collections;

import java.math.BigDecimal;
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.stream.Collectors;

import org.junit.Before;
import org.junit.Test;

import com.qdfae.jdk.domain.TradeDetail;

/**
* List集合去重
*
* @author hongwei.lian
* @date 2018年3月11日 下午8:54:57
*/
public class DuplicateListTest {
/**
* 存储没有去重的明细对象的List集合
*/
private List<TradeDetail> tradeDetailList;
/**
* 存储去重后的明细对象的List集合
*/
private List<TradeDetail> duplicateTradeDetailList;
/**
* 存储去重后的明细对象的Set集合
*/
private Set<TradeDetail> tradeDetailSet;
/**
* 初始化tradeDetailList
*
* @author hongwei.lian
* @date 2018年3月11日 下午9:04:45
*/
@Before
public void InitTradeDetailList() {
tradeDetailList = new ArrayList<>();
tradeDetailList.add(new TradeDetail(1, "600010", "账户一", new BigDecimal(100.00)));
tradeDetailList.add(new TradeDetail(2, "600011", "账户二", new BigDecimal(100.00)));
tradeDetailList.add(new TradeDetail(3, "600010", "账户一", new BigDecimal(-100.00)));
tradeDetailList.add(new TradeDetail(4, "600010", "账户一", new BigDecimal(-100.00)));
}

/**
* 使用Set接口的实现类HashSet进行List集合去重
*
* HashSet实现类
* 构造方法:
* public TreeSet(Comparator<? super E> comparator)
*
* @author hongwei.lian
* @date 2018年3月11日 下午9:37:51
*/
@Test
public void testDuplicateListWithHashSet() {
//-- 前提是TradeDetail根据规则重写hashCode()方法和equals()方法
tradeDetailSet = new HashSet<>(tradeDetailList);
tradeDetailSet.forEach(System.out::println);
}
/**
* 使用Map集合进行List集合去重
*
* @author hongwei.lian
* @date 2018年3月11日 下午9:05:49
*/
@Test
public void testDuplicateListWithIterator() {
duplicateTradeDetailList = new ArrayList<>();
Map<String, TradeDetail> tradeDetailMap = tradeDetailList.stream()
.collect(Collectors.toMap(
tradeDetail -> tradeDetail.getAccountNo(),
tradeDetail -> tradeDetail,
(oldValue, newValue) -> newValue));
tradeDetailMap.forEach(
(accountNo, tradeDetail) -> duplicateTradeDetailList.add(tradeDetail)
);
duplicateTradeDetailList.forEach(System.out::println);
//-- 参考文章
//http://blog.jobbole.com/104067/
//https://www.cnblogs.com/java-zhao/p/5492122.html
}
/**
* 使用Set接口的实现类TreeSet进行List集合去重
*
* TreeSet实现类
* 构造方法:
* public TreeSet(Comparator<? super E> comparator)
*
* @author hongwei.lian
* @date 2018年3月11日 下午9:37:48
*/
@Test
public void testDuplicateListWithTreeSet() {
tradeDetailSet = new TreeSet<>(
(tradeDetail1, tradeDetail2)
->
tradeDetail1.getAccountNo().compareTo(tradeDetail2.getAccountNo())
);
tradeDetailSet.addAll(tradeDetailList);
tradeDetailSet.forEach(System.out::println);
}

}
运行上面三个方法的结果都是:

方案一:根据需求重写自定义类的hashCode()方法和equals()方法
这种方案的不足之处是根据需求重写后的hashCode()方法和equals()方法不一定满足其他需求,这样这个TradeDetail类的复用性就会相当差。
方案二:遍历List集合,取出每一个明细对象,将明细对象的accountNo属性字段作为Map集合key,明细对象作为Map集合的value,然后再遍历Map集合,得到一个去重后的List集合或者Set集合。
这种方案的不足之处是消耗性能,首先是List集合去重转换为Map集合,Map集合再次转换为List集合或者Set集合,遍历也会消耗性能。
方案三:使用TreeSet集合的独有的构造方法进行去重,如下:
public TreeSet(Comparator<? super E> comparator) {
this(new TreeMap<>(comparator));
}
这种方案目前为止是我使用的比较多的方案,不足之处暂时没有发现,TreeSet集合实际上是利用TreeMap的带有一个比较器参数的构造方法实现,看JDK源码很清晰,最重要的是这个参数Comparator接口,这个接口的源码:
Comparator接口部分源码:
@FunctionalInterface
public interface Comparator<T> {

int compare(T o1, T o2);

}
这个compare()方法需要自己根据需求去实现,仔细看上面去重的原理实际上还是使用String类的compareTo()方法,String类的compareTo()方法源码:
public int compareTo(String anotherString) {
int len1 = value.length;
int len2 = anotherString.value.length;
int lim = Math.min(len1, len2);
char v1[] = value;
char v2[] = anotherString.value;

int k = 0;
while (k < lim) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
return c1 - c2;
}
k++;
}
return len1 - len2;
}
基本就想到了这些,如果有好的实现方法,自己还会补充。

http://chatgpt.dhexx.cn/article/aViAbPUm.shtml

相关文章

关于两个List集合对象去重

实际项目开发中&#xff0c;很多业务场景下都会遇见集合去重。在说到两个对象去重之前&#xff0c;首先我们回顾下普通类型的list如何去重&#xff0c;这里先说两个list自带的方法&#xff0c;图画的不是太好&#xff0c;勿喷- -&#xff01; 一&#xff1a;retainAll() List&…

java list集合数据去重方式

1.概述 最近又是一轮代码review , 发现了一些实现去重的代码&#xff0c;在使用 list.contain … 我沉思&#xff0c;是不是其实很多初学者也存在这种去重使用问题&#xff1f; 所以我选择把这个事情整出来&#xff0c;分享一下。 2.contain 去重 首先是造出一个 List 模拟…

Java【List】去重的 6种方法

list集合去重 一、HashSet去重二、TreeSet去重三、LinkedHashSet去重四、迭代器去重五、Stream去重六、contains判断去重等等... 其它实现方法 一、HashSet去重 我们知道 HashSet 天生具备“去重”的特性&#xff0c;那我们只需要将 List 集合转换成 HashSet 集合就可以了&…

List 去重的 6 种方法,这种方法最完美!

在日常的业务开发中&#xff0c;偶尔会遇到需要将 List 集合中的重复数据去除掉的场景。这个时候可能有同学会问&#xff1a;为什么不直接使用 Set 或者 LinkedHashSet 呢&#xff1f;这样不就没有重复数据的问题了嘛&#xff1f; ​ 不得不说&#xff0c;能提这个问题的同学很…

List去除重复数据的五种方式

你知道的越多&#xff0c;不知道的就越多&#xff0c;业余的像一棵小草&#xff01; 你来&#xff0c;我们一起精进&#xff01;你不来&#xff0c;我和你的竞争对手一起精进&#xff01; 编辑&#xff1a;业余草 blog.csdn.net/qq_37939251/article/details/90713643 推荐&…

List元素去重的六种方式

上周的时候完成公司交付的任务&#xff0c;突然间遇到了需要把重复元素去掉的功能&#xff0c;当时我的大脑飞速运转&#xff0c;努力回想以前学习关于list的知识&#xff0c;后来&#xff0c;我发现已经忘得差不多了&#xff0c;所以我就找到了这篇文章&#xff0c;说的很详细…

子图是什么

子图和真子图 设 G <V, E>, <, >是两个图(同为无向,或同为有向图). 若 V 且 E, 则称 为 G 的子图, G 为 的母图, 记作 G 若 V 或 E, 称 为 G 的真子图. 生成子图 若 G 且 V , 则称 为 G 的生成子图 两个导出子图 设 V 且 &#xff08;空集&a…

latex绘制子图,并为子图添加

如下代码所示&#xff1a; 通过\subfigure绘制子图&#xff0c;\centering使图片居中&#xff0c;可以直接在子图中为为图片添加标注&#xff0c;如代码中的{\scriptsize{误差均值}}&#xff0c;该命令表示在子图上面添加标注。有时需要同时在子图的左边也添加竖排标注&#xf…

Python Matplotlib 子图

目录 1、绘制多个子图 2、绘图在指定的子图上 3、subplots命令&#xff1a;快速生成多个子图框架 3.1 快速布局 3.2 画一个图形 3.3 多个图形共用一个轴 3.4 与seaborn联合使用 3.5 使用数组方式指定子图 3.6 嵌套图 在matplotlib中&#xff0c;所有的绘图操作实际上都是以…

Matplotlib多子图绘图后保存单个子图

import numpy as np import matplotlib.pyplot as plt# 用于单独保存子图的函数 def save_subfig(fig,ax,save_path,fig_name):bbox ax.get_tightbbox(fig.canvas.get_renderer()).expanded(1.02, 1.02)extent bbox.transformed(fig.dpi_scale_trans.inverted())fig.savefig(…

Matplotlib子图

子图 **有时候我们需要从多个角度进行数据的比较、分析&#xff0c;因此就需要用到子图。**子图的本质是在一个较大的图形中同时放置一组较小的坐标轴&#xff0c;布局形式可以多种多样&#xff0c;不拘泥于我们在第五集中举的那种网格图的形式。 一般化的子图 我们先进行一…

latex生成子图及并列图

latex生成子图及并列图 \usepackage{graphicx}%插入图片 \usepackage{subfigure} %子图 子图 代码 \begin{figure} \centering \subfigure[Average total cost with different schemes.]{ \includegraphics[width3in]{V9-1-vary-S} } \subfigure[Average time delay with …

【数据分析之道-Matplotlib(三)】Matplotlib 绘制子图

文章目录 专栏导读1、前言2、subplot()函数2.1创建一个包含 2x2 的子图布局&#xff0c;并在每个子图中绘制不同的图形2.2创建一个包含 1x3 的子图布局&#xff0c;并绘制三种不同类型的图形2.3创建一个包含 2 行 1 列的子图布局&#xff0c;绘制散点图和柱状图 3、subplots()函…

matplotlib 基础_子图创建

目录 创建Axes方法1 、 add_subplot 函数&#xff1a;快速创建1.1 设置不等尺寸的子图 2、add_axes 函数&#xff1a;自由设置子图的位置、大小3、指定子图空间&#xff1a;当子图大小成倍数时 一张图理解matplotlib Figure 和 Axes Figure 就是图片&#xff0c;一个完整的图片…

plt.plot画子图时,子图变形,调整子图大小

在画图时候遇到的记录一下 希望可以帮到遇到相同问题的伙伴儿~~ 本来是这样&#xff1a; 本来代码&#xff1a; plt.subplot(1, 2, 1)plt.rcParams[font.sans-serif] [SimHei]plt.rcParams[axes.unicode_minus] Falseplt.rcParams[figure.dpi] 1080plt.rcParams[figure.fi…

关于图论中导出子图的概念

关于图论中导出子图的概念 1、导出子图 A subgraph H is called an induced subgraph of X if for any a , b ∈ E ( H ) a,b \in E(H) a,b∈E(H) if and only if a , b ∈ E ( X ) a,b \in E(X) a,b∈E(X). 2、点导出子图 设S是V(G)的子集&#xff0c;以S为点集&#xf…

极大连通子图与极小连通子图

无向图 连通图&#xff1a; 在无向图中&#xff0c;若从定点V1到V2有路径&#xff0c;则称顶点V1和V2是连通的。如果图中任意一对顶点都是连通的&#xff0c;则称此图是连通图。&#xff08;连通的无向图&#xff09;极大连通子图&#xff1a; 1.连通图只有一个极大连通子图&a…

python数据可视化玩转Matplotlib subplot子图操作,四个子图(一包四),三个子图,子图拉伸

目录 一、创建子图 1.1 下图是绘制的子图&#xff1a; 1.2 代码释义&#xff1a; 二、绘制子图 2.1 代码引入 2.2 图形绘制 三、子图布局 3.1 子图布局说明 四、子图大小 4.1 子图大小调整 五、子图间距 5.1 子图代码调整 六、子图位置 6.1 代码引入 6.2 完整代码…

极大连通子图与极小连通子图(带图讲解)

因为本人对于这一块知识存在疑惑&#xff0c;在学习了相关知识后将自己的理解分享给大家&#xff0c;如有错误&#xff0c;欢迎纠正。 首先我们先明确一下&#xff0c;极小连通子图与极大连通子图是在无向图中进行讨论的。 极大强连通子图是在有向图中进行讨论的&#xff0c;不…

Python 调整子图大小、位置【plt.axes()自定义子图】

效果图&#xff1a; plt.axes()绘图结果 上图一共三个子图&#xff0c;两个横向子图对齐右侧纵向长图。 本来想用plt.subplot()函数画&#xff0c;结果找了半天都是下图这种结果&#xff1a; 只能合并列&#xff0c;不能合并行 plt.subplot()绘图结果 因此&#xff0c;我放…