网站首页 > 技术文章 正文
list集合去重的方式有很多,你习惯使用哪一种?
- 使用Set集合去重:将List集合转换成Set集合,由于Set集合不允许元素重复,因此可以达到去重的目的。例如:
List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 2, 4, 3));
Set<Integer> set = new HashSet<>(list);
List<Integer> result = new ArrayList<>(set); // [1, 2, 3, 4]
- 使用Java 8的Stream去重:通过将List集合转换成Stream流,然后使用distinct()方法去重,最后转换回List集合。例如:
List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 2, 4, 3));
List<Integer> result = list.stream().distinct().collect(Collectors.toList()); // [1, 2, 3, 4]
- 使用LinkedHashSet去重:使用LinkedHashSet集合可以保留原来的顺序,并且去除重复元素。例如:
List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 2, 4, 3));
LinkedHashSet<Integer> set = new LinkedHashSet<>(list);
List<Integer> result = new ArrayList<>(set); // [1, 2, 3, 4]
- 使用for循环去重:通过遍历List集合,使用if语句判断元素是否已经存在于结果List中,如果不存在,则加入结果List中。例如:
List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 2, 4, 3));
List<Integer> result = new ArrayList<>();
for (Integer num : list) {
if (!result.contains(num)) {
result.add(num);
}
}
// [1, 2, 3, 4]
- 使用TreeSet去重:与HashSet不同,TreeSet是有序的集合,同时也不允许重复元素。可以使用TreeSet对List进行去重,达到去重和排序的目的。例如:
List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 2, 4, 3));
TreeSet<Integer> set = new TreeSet<>(list);
List<Integer> result = new ArrayList<>(set); // [1, 2, 3, 4]
需要注意的是,使用TreeSet进行去重时,要求元素类型必须实现Comparable接口,或者在创建TreeSet时指定一个比较器Comparator。
- 使用Collections类去重:Collections类提供了一些静态方法可以对List进行去重,如下所示:
List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 2, 4, 3));
Collections.sort(list); // 先排序
List<Integer> result = new ArrayList<>(new LinkedHashSet<>(list));
// 使用LinkedHashSet去重并保留顺序
//这里使用了LinkedHashSet来去重,保留了原来的顺序。
- 使用Java 8的Collectors.toMap()去重:Java 8的Collectors.toMap()方法可以将List转换为Map,并指定去重的key值,实现去重的目的。例如:
List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 2, 4, 3));
Map<Integer, Integer> map = list.stream().collect(Collectors.toMap(Function.identity(), Function.identity(), (a, b) -> a));
List<Integer> result = new ArrayList<>(map.keySet()); // [1, 2, 3, 4]
这里使用了Function.identity()来指定key值,使用(a, b) -> a来合并相同的key值。最后将Map的key值转换为List集合即可。
- 使用Stream的reduce()方法去重:使用reduce()方法将List集合转换为Set集合实现去重,例如:
List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 2, 4, 3));
Set<Integer> set = list.stream().reduce(new HashSet<Integer>(), (s, e) -> {
s.add(e);
return s;
}, (s1, s2) -> {
s1.addAll(s2);
return s1;
});
List<Integer> result = new ArrayList<>(set); // [1, 2, 3, 4]
这里的reduce()方法接受三个参数,第一个参数是初始值,第二个参数是一个Lambda表达式,用于将List中的元素加入Set集合,第三个参数用于合并多个Set集合。
- 使用Stream的collect()方法去重:使用collect()方法结合Collectors.toSet()方法将List集合转换为Set集合实现去重,例如:
List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 2, 4, 3));
Set<Integer> set = list.stream().collect(Collectors.toSet());
List<Integer> result = new ArrayList<>(set); // [1, 2, 3, 4]
这里使用了Collectors.toSet()方法将Stream流转换为Set集合。
总结:以上的9种方式几乎涵盖了集合、steam流、Java基础类的应用,在工作中能熟练用2-3种就足够了,但是如果别人用了其它的方式,你也要能看懂代码的意思。
有一个需要注意的点就是,对于自定义的类进行去重,需要确保类正确实现了equals()方法和hashCode()方法,否则可能无法正确去重。你学会了吗?
猜你喜欢
- 2024-10-18 【Python】map函数的常见用法,你知道多少?
- 2024-10-18 Python 中的数据可视化:将列表转换为图形
- 2024-10-18 Java 把一个 List 转换为字符串(java list转成字符串)
- 2024-10-18 Java Stream API:将线性集合添加到Map,键为对象属性
- 2024-10-18 SpringBoot读取配置文件中的数据到map和list
- 2024-10-18 「Java」咦,它就是Map和List的儿子吧
- 2024-10-18 一日一技:举例说明python中的map()方法
- 2024-10-18 详解 Python Map 函数(python map函数的用法)
- 2024-10-18 你应该知道的Java Map 的七个常见问题!
- 2024-10-18 Java核心数据结构(List、Map、Set)原理与使用技巧
- 最近发表
- 标签列表
-
- cmd/c (64)
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- sqlset (64)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- chromepost (65)
- c++int转char (75)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- js数组插入 (83)
- linux删除一个文件夹 (65)
- mac安装java (72)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)