我在日常工作中常用Stream方式去重,满足了工作上业务的需求即可,并没有深入了解和尝试其他方式的去重操作,这对于个人的成长是很有局限性的,遂借此机会整理ArrayList的去重方法。
1.使用迭代器遍历去重
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 import java.util.*;public class GFG { public static <T> ArrayList<T> removeDuplicates (ArrayList<T> list) { ArrayList<T> newList = new ArrayList <T>(); for (T element : list) { if (!newList.contains(element)) { newList.add(element); } } return newList; } public static void main (String args[]) { ArrayList<Integer> list = new ArrayList <>( Arrays .asList(1 , 10 , 1 , 2 , 2 , 3 , 3 , 10 , 3 , 4 , 5 , 5 )); System.out.println("ArrayList with duplicates: " + list); ArrayList<Integer> newList = removeDuplicates(list); System.out.println("ArrayList with duplicates removed: " + newList); } }
输出:
1 2 ArrayList with duplicates: [1, 10, 1, 2, 2, 3, 3, 10, 3, 4, 5, 5] ArrayList with duplicates removed: [1, 10, 2, 3, 4, 5]
2.使用LinkedHashSet
可以将ArrayList转换成不允许值重复的Set集合,因此LinkedHashSet是最好的选择,因为它不允许重复.
HashSet也能实现同样的去重效果,但是HashSet与LinkedHashSet的不同之处在于,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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 import java.util.*;public class GFG { public static <T> ArrayList<T> removeDuplicates (ArrayList<T> list) { Set<T> set = new LinkedHashSet <>(); set.addAll(list); list.clear(); list.addAll(set); return list; } public static void main (String args[]) { ArrayList<Integer> list = new ArrayList <>( Arrays .asList(1 , 10 , 1 , 2 , 2 , 3 , 10 , 3 , 3 , 4 , 5 , 5 )); System.out.println("ArrayList with duplicates: " + list); ArrayList<Integer> newList = removeDuplicates(list); System.out.println("ArrayList with duplicates removed: " + newList); } }
输出:
1 2 ArrayList with duplicates: [1, 10, 1, 2, 2, 3, 10, 3, 3, 4, 5, 5] ArrayList with duplicates removed: [1, 10, 2, 3, 4, 5]
更简单的方法可以这样写 1 2 3 Set<String> set = new LinkedHashSet <>(yourList); yourList.clear(); yourList.addAll(set);
3.使用Java
8版本中的Stream.distinct()方法
distinct()方法根据equals()方法返回的结果返回一个没有重复元素的新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 27 28 29 import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.stream.Collectors;class GFG { public static void main (String[] args) { List<Integer> list = new ArrayList <>( Arrays.asList(1 , 10 , 1 , 2 , 2 , 3 , 10 , 3 , 3 , 4 , 5 , 5 )); System.out.println("ArrayList with duplicates: " + list); List<Integer> newList = list.stream().distinct().collect(Collectors.toList()); System.out.println("ArrayList with duplicates removed: " + newList); } }
输出:
1 2 ArrayList with duplicates: [1, 10, 1, 2, 2, 3, 10, 3, 3, 4, 5, 5] ArrayList with duplicates removed: [1, 10, 2, 3, 4, 5]