如何实现 List 集合去重?
List 去重指的是将 List 中的重复元素删除掉的过程。
List 去重有以下 6 种实现思路:
- 自定义方法去重,通过循环判断当前的元素是否存在多个,如果存在多个,则删除此重复项,循环整个集合最终得到的就是一个没有重复元素的 List;
- 使用 Set 集合去重,利用 Set 集合自身自带去重功能的特性,实现 List 的去重;
- 使用 JDK 8 中 Stream 流的去重功能。
1.自定义去重(contains判断去重(有序))
自定义去重的实现方法有两种,首先我们可以创建一个新集合,通过循环原集合判断循环的元素,是否已存在于新集合,如果不存在则插入,否则就忽略,这样循环完,最终得到的新集合就是一个没有重复元素的集合,具体实现代码如下:
123456789101112131415161718192021222324252627282930public
class
DistinctExample {
public
static
void
main(String[] args) {
// 创建并给 List 赋值
List<Person> list =
new
ArrayList<>();
list.add(
new
Person(
"李四"
,
"123456"
,
20
));
list.add(
new
Person(
"张三"
,
"123456"
,
18
));
list.add(
new
Person(
"王五"
,
"123456"
,
22
));
list.add(
new
Person(
"张三"
,
"123456"
,
18
));
// 去重操作
List<Person> newList =
new
ArrayList<>(list.size());
list.forEach(i -> {
if
(!newList.contains(i)) {
// 如果新集合中不存在则插入newList.add(i);
}
});
// 打印集合newList.forEach(p -> System.out.println(p));
}
}
@Data
class
Person {
private
String name;
private
String password;
private
int
age;
public
Person(String name, String password,
int
age) {
this
.name = name;
this
.password = password;
this
.age = age;
}
}
执行结果如下
2.利用set去重
Set 集合天生具备去重特性,在创建 Set 集合时可以传递一个 List 集合,这样就能实现数据转移和去重的功能了,具体实现代码如下:
1234567891011121314151617181920212223242526public
class
DistinctExample {
public
static
void
main(String[] args) {
// 创建并给 List 赋值
List<Person> list =
new
ArrayList<>();
list.add(
new
Person(
"李四"
,
"123456"
,
20
));
list.add(
new
Person(
"张三"
,
"123456"
,
18
));
list.add(
new
Person(
"王五"
,
"123456"
,
22
));
list.add(
new
Person(
"张三"
,
"123456"
,
18
));
// 去重操作
HashSet<Person> set =
new
HashSet<>(list);
// 打印集合信息set.forEach(p -> System.out.println(p));
}
}
@Data
class
Person {
private
String name;
private
String password;
private
int
age;
public
Person(String name, String password,
int
age) {
this
.name = name;
this
.password = password;
this
.age = age;
}
}
执行结果如下
通过上述结果,发现了一个问题,在使用了 HashSet 去重之后,元素的先后顺序发生了变化。为了能解决这个问题,我们可以使用 LinkedHashSet 来实现去重功能,具体实现代码如下:
1234567891011121314151617181920212223242526public
class
DistinctExample {
public
static
void
main(String[] args) {
// 创建并给 List 赋值
List<Person> list =
new
ArrayList<>();
list.add(
new
Person(
"李四"
,
"123456"
,
20
));
list.add(
new
Person(
"张三"
,
"123456"
,
18
));
list.add(
new
Person(
"王五"
,
"123456"
,
22
));
list.add(
new
Person(
"张三"
,
"123456"
,
18
));
// 去重操作
LinkedHashSet<Person> set =
new
LinkedHashSet<>(list);
// 打印集合信息set.forEach(p -> System.out.println(p));
}
}
@Data
class
Person {
private
String name;
private
String password;
private
int
age;
public
Person(String name, String password,
int
age) {
this
.name = name;
this
.password = password;
this
.age = age;
}
}
执行结果如下
3.使用 Stream 去重
最后一种也是最简单的一种去重方式,我们可以使用 JDK 8 中提供的 Stream 进行去重,Stream 中包含了一个去重方法:distinct,可以直接实现集合的去重功能,具体实现代码如下:
123456789101112131415161718192021222324252627public
class
DistinctExample {
public
static
void
main(String[] args) {
// 创建并给 List 赋值
List<Person> list =
new
ArrayList<>();
list.add(
new
Person(
"李四"
,
"123456"
,
20
));
list.add(
new
Person(
"张三"
,
"123456"
,
18
));
list.add(
new
Person(
"王五"
,
"123456"
,
22
));
list.add(
new
Person(
"张三"
,
"123456"
,
18
));
// 去重操作
list = list.stream().distinct().collect(Collectors.toList());
// 打印集合信息
list.forEach(p -> System.out.println(p));
}
}
@Data
class
Person {
private
String name;
private
String password;
private
int
age;
public
Person(String name, String password,
int
age) {
this
.name = name;
this
.password = password;
this
.age = age;
}
}
执行结果如下
4.迭代器去重(无序)
12345678910111213141516171819202122232425public
static
void
main(String[] args) {
List<Integer> list =
new
ArrayList<Integer>() {{
add(
1
);
add(
3
);
add(
5
);
add(
2
);
add(
1
);
add(
3
);
add(
7
);
add(
2
);
}};
System.out.println(
"原来的集合---"
+list);
Iterator<Integer> iterator = list.iterator();
while
(iterator.hasNext()){
// 获取循环的值
Integer next = iterator.next();
// 如果存在两个相同的值
if
(list.indexOf(next) != list.lastIndexOf(next)){
// 移除最后那个相同的值
iterator.remove();
}
}
System.out.println(
"去重集合:"
+ list);
}
执行结果如下
5.TreeSet去重(无序)
123456789101112131415public
static
void
main(String[] args) {
List<Integer> list =
new
ArrayList<Integer>() {{
add(
1
);
add(
3
);
add(
5
);
add(
2
);
add(
1
);
add(
3
);
add(
7
);
add(
2
);
}};
System.out.println(
"原来的集合---"
+list);
TreeSet<Integer> set =
new
TreeSet<>(list);
System.out.println(
"去重集合:"
+ set);
}
执行结果如下
总结
本文介绍了 5 种集合去重的方法,其中实现最简洁,且去重之后的顺序能和原集合保持一致的实现方法,只有两种:LinkedHashSet 去重和 Stream 去重
LinkedHashSet 在去重的同时又保证了元素所在位置不被更改
Stream 的distinct 方法 是 JDK 8 中新增的,优点是不但写法简单,而且无需创建新的集合,是实现去重功能的首选方法
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了