list集合去重
- 一、HashSet去重
- 二、TreeSet去重
- 三、LinkedHashSet去重
- 四、迭代器去重
- 五、Stream去重
- 六、contains判断去重
- 等等... 其它实现方法
一、HashSet去重
我们知道 HashSet
天生具备“去重”的特性,那我们只需要将 List 集合转换成 HashSet 集合就可以了,实现代码如下:
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);}
}
以上程序执行的结果:
二、TreeSet去重
除了HashSet集合之外,我们还可以使用 TreeSet
集合来实现去重功能,实现代码如下:
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);}
}
以上程序执行的结果:
三、LinkedHashSet去重
如果上面的Set 不能满足需求,那就使用 LinkedHashSet
,它既能去重又能保证集合的顺序,实现代码如下:
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);}
}
以上程序执行的结果:
四、迭代器去重
使用迭代器循环判断每一项数据,如果当前循环的数据,在集合中存在两份或两份以上,就将当前的元素删除掉,这样循环完之后,即可得到一个没有重复数据的集合
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);}
}
以上程序执行的结果:
五、Stream去重
JDK 8 为我们带来了一个非常实用的方法 Stream,使用它可以实现很多功能。(个人感觉Stream很好用👍)
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);}
}
以上程序执行的结果:
六、contains判断去重
新建一个集合,然后循环原来的集合,每次循环判断原集合中的循环项,如果当前循环的数据,没有在新集合中存在就插入,已经存在了就舍弃,就得到了一个没有重复元素的集合了
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);}/*** 自定义去重* @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);}
}