我是靠谱客的博主 饱满夕阳,这篇文章主要介绍集合去重的几种方法,现在分享给大家,希望可以做个参考。

参考:https://mp.weixin.qq.com/s/94vQ_9jWaLZAXmWi_HQEOg

方法1:contains判断去重(有序)

要进行数据去重,我们首先想到的是新建一个集合,然后循环原来的集合,每次循环判断原集合中的循环项,如果当前循环的数据,没有在新集合中存在就插入,已经存在了就舍弃,这样当循环执行完,我们就得到了一个没有重复元素的集合了,实现代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class ListDistinctExample { public 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); method(list); }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/** * 自定义去重 * @param list */ public static void method(List<Integer> list) { // 新集合 List<Integer> newList = new ArrayList<>(list.size()); list.forEach(i -> { if (!newList.contains(i)) { // 如果新集合中不存在则插入 newList.add(i); } }); System.out.println("去重集合:" + newList); } }

以上程序执行的结果,如下所示:图片此方法的优点的:理解起来比较简单,并且最终得到的集合也是有序的,这里的有序指的是新集合的排列顺序和原集合的顺序是一致的;但缺点是实现代码有点多,不够简洁优雅。

方法2:迭代器去重(无序)

自定义 List 去重,除了上面的新建集合之外,我们也可以使用迭代器循环判断每一项数据,如果当前循环的数据,在集合中存在两份或两份以上,就将当前的元素删除掉,这样循环完之后,也可以得到一个没有重复数据的集合,实现代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class ListDistinctExample { public 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); method_1(list); } /** * 使用迭代器去重 * @param list */ public static void method_1(List<Integer> list) { Iterator<Integer> iterator = list.iterator(); while (iterator.hasNext()) { // 获取循环的值 Integer item = iterator.next(); // 如果存在两个相同的值 if (list.indexOf(item) != list.lastIndexOf(item)) { // 移除最后那个相同的值 iterator.remove(); } } System.out.println("去重集合:" + list); } }

以上程序执行的结果,如下所示:图片此方法的实现比上一种方法的实现代码要少一些,并且不需要新建集合,但此方法得到的新集合是无序的,也就是新集合的排列顺序和原集合不一致,因此也不是最优的解决方案。

方法3:HashSet去重(无序)

我们知道 HashSet 天生具备“去重”的特性,那我们只需要将 List 集合转换成 HashSet 集合就可以了,实现代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class ListDistinctExample { public 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); method_2(list); } /** * 使用 HashSet 去重 * @param list */ public static void method_2(List<Integer> list) { HashSet<Integer> set = new HashSet<>(list); System.out.println("去重集合:" + set); } }

以上程序执行的结果,如下所示:图片此方法的实现代码较为简洁,但缺点是 HashSet 会自动排序,这样新集合的数据排序就和原集合不一致了,如果对集合的顺序有要求,那么此方法也不能满足当前需求。

方法4:LinkedHashSet去重(有序)

既然 HashSet 会自动排序不能满足需求,那就使用 LinkedHashSet,它既能去重又能保证集合的顺序,实现代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class ListDistinctExample { public 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); method_3(list); } /** * 使用 LinkedHashSet 去重 * @param list */ public static void method_3(List<Integer> list) { LinkedHashSet<Integer> set = new LinkedHashSet<>(list); System.out.println("去重集合:" + set); } }

以上程序执行的结果,如下所示:图片从上述代码和执行结果可以看出,LinkedHashSet 是到目前为止,实现比较简单,且最终生成的新集合与原集合顺序保持一致的实现方法,是我们可以考虑使用的一种去重方法。

方法5:TreeSet去重(无序)

除了以上的 Set 集合之外,我们还可以使用 TreeSet 集合来实现去重功能,实现代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class ListDistinctExample { public 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); method_4(list); } /** * 使用 TreeSet 去重(无序) * @param list */ public static void method_4(List<Integer> list) { TreeSet<Integer> set = new TreeSet<>(list); System.out.println("去重集合:" + set); } }

以上程序执行的结果,TreeSet 有着和 HashSet 一样的问题,会自动进行自然排序

方法6:Stream去重(有序)

JDK 8 为我们带来了一个非常实用的方法 Stream,使用它可以实现很多功能,比如下面的去重功能:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class ListDistinctExample { public 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); method_5(list); } /** * 使用 Stream 去重 * @param list */ public static void method_5(List<Integer> list) { list = list.stream().distinct().collect(Collectors.toList()); System.out.println("去重集合:" + list); } }

以上程序执行的结果,如下所示:图片Stream 实现去重功能和其他方法不同的是,它不用新创建集合,使用自身接收一个去重的结果就可以了,并且实现代码也很简洁,并且去重后的集合顺序也和原集合的顺序保持一致,是我们最优先考虑的去重方法。

总结

本文我们介绍了 6 种集合去重的方法,其中实现最简洁,且去重之后的顺序能和原集合保持一致的实现方法,只有两种:LinkedHashSet 去重和 Stream 去重,而后一种去重方法无需借助新集合,是我们优先考虑的去重方法。

最后

以上就是饱满夕阳最近收集整理的关于集合去重的几种方法的全部内容,更多相关集合去重内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(73)

评论列表共有 0 条评论

立即
投稿
返回
顶部