ArrayList数组去重
- 1.LinkedHashSet
- 2.HashSet
- 3.steam的distinct()方法
- 4.steam的filter()方法
- 5.contains()方法
- 6.indexOf()方法
- 7.lastIndexOf()方法
- 8.双重for循环
- [9.sort 排序](#9.sort 排序)
运行结果
java
原数组 : [3, 3, 1, 2, 5, 6, 8, 6, 4, 2]
XXXXXX 去重后数组 : [3, 1, 2, 5, 6, 8, 4]
1.LinkedHashSet
java
List<Integer> list = new ArrayList<>(Arrays.asList(3, 3, 1, 2, 5, 6, 8, 6, 4, 2));
List<Integer> listByLinkedHashSet = new ArrayList<>(new LinkedHashSet<>(list));
System.out.println("原数组 : " + list);
System.out.println("LinkedHashSet 去重后数组 : " + listByLinkedHashSet);
2.HashSet
由于HashSet不能保证添加顺序,所以只能作为判断条件保证顺序
java
List<Integer> list = new ArrayList<>(Arrays.asList(3, 3, 1, 2, 5, 6, 8, 6, 4, 2));
HashSet<Integer> set = new HashSet<Integer>();
List<Integer> listByHashSet = new ArrayList<Integer>();
for (Integer num : list) {
if (set.add(num)) {
listByHashSet.add(num);
}
}
System.out.println("原数组 : " + list);
System.out.println("HashSet 去重后数组 : " + listByHashSet);
如果不计较去重后的数据顺序,可以直接使用HashSet进行去重
HashSet hashSet = new HashSet<>(list);
List listByHashSet = new ArrayList<>(hashSet);
3.steam的distinct()方法
java
List<Integer> list = new ArrayList<>(Arrays.asList(3, 3, 1, 2, 5, 6, 8, 6, 4, 2));
List<Integer> listByStream = list.stream().distinct().collect(Collectors.toList());
System.out.println("原数组 : " + list);
System.out.println("Stream 去重后数组 : " + listByStream);
4.steam的filter()方法
java
List<Integer> list = new ArrayList<>(Arrays.asList(3, 3, 1, 2, 5, 6, 8, 6, 4, 2));
List<Integer> listByFilter = new ArrayList<>(list.size());
list.stream().filter(i -> {
if (listByFilter.indexOf(i) == -1){
listByFilter.add(i);
}
return false;
}).collect(Collectors.toList());
System.out.println("原数组 : " + list);
System.out.println("filter 去重后数组 : " + listByFilter);
5.contains()方法
java
List<Integer> list = new ArrayList<>(Arrays.asList(3, 3, 1, 2, 5, 6, 8, 6, 4, 2));
List<Integer> listByContains = new ArrayList<>(list.size());
for (Integer num : list) {
if (!listByContains.contains(num)) {
listByContains.add(num);
}
}
System.out.println("原数组 : " + list);
System.out.println("Contains 去重后数组 : " + listByContains);
6.indexOf()方法
markup
List<Integer> list = new ArrayList<>(Arrays.asList(3, 3, 1, 2, 5, 6, 8, 6, 4, 2));
List<Integer> listByIndexOf = new ArrayList<>(list.size());
for (int i = 0; i < list.size(); i++) {
if (listByIndexOf.indexOf(list.get(i)) == -1){
listByIndexOf.add(list.get(i));
}
}
System.out.println("原数组 : " + list);
System.out.println("ForLoop 去重后数组 : " + listByIndexOf);
7.lastIndexOf()方法
java
List<Integer> list = new ArrayList<>(Arrays.asList(3, 3, 1, 2, 5, 6, 8, 6, 4, 2));
System.out.println("原数组 : " + list);
for (int i = 0; i < list.size(); i++) {
if (list.indexOf(list.get(i)) != list.lastIndexOf(list.get(i))) {
list.remove(list.lastIndexOf(list.get(i)));
}
}
System.out.println("lastIndexOf 去重后数组 : " + list);
8.双重for循环
java
List<Integer> list = new ArrayList<>(Arrays.asList(3, 3, 1, 2, 5, 6, 8, 6, 4, 2));
System.out.println("原数组 : " + list);
for (int i = 0; i < list.size(); i++) {
for (int j = i+1; j < list.size(); j++) {
if(list.get(i)==list.get(j)) {
list.remove(j);
}
}
}
System.out.println("ForLoop 去重后数组 : " + list);
9.sort 排序
排序之后,如果相邻两个元素是相等的,那么就说明重复了。(但这个方法会改变数组原有顺序)
java
List<Integer> list = new ArrayList<>(Arrays.asList(3, 3, 1, 2, 5, 6, 8, 6, 4, 2));
Collections.sort(list);
List<Integer> listBySort = new ArrayList<>(list.size());
listBySort.add(list.get(0));
for (int i = 1; i < list.size(); i++) {
if (list.get(i-1) != list.get(i)) {
listBySort.add(list.get(i));
}
}
System.out.println("原数组 : " + list);
System.out.println("Sort 去重后数组 : " + listBySort);