在日常工作中,需要对 List 进行去重的场景很多。对于我这样一个菜鸟来说,可能能想到的最简单粗暴的方法就是双层循环去重,但是在看到这篇文章之后,果断收藏了。
https://mp.weixin.qq.com/s/N1MRYswi8v1Lh6pdDz8upQ
来源于微信公众号“Java爱好者”,在此表示感谢。若有侵权,请联系删除~
既然刚才说到了简单粗暴的方法,那么我们就先来看看这种暴力方法:
1.双层循环
import java.util.*;
public class ListDemo {
private void removeRepeat(List<Integer> listInt) {
List<Integer> list = new ArrayList<>(listInt);
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < list.size(); j++) {
if (i != j && list.get(i).equals(list.get(j))) {
list.remove(list.get(j));
}
}
}
System.out.println("去重后的list = " + list);
}
public static void main(String[] args) {
ListDemo listDemo = new ListDemo();
Integer[] array = {1,1,3,5,7,6,4,3,4,5,8,2};
List<Integer> listInt = Arrays.asList(array);
System.out.println("去重前的list = " + listInt);
listDemo.removeRepeat(listInt);
}
}
此方法不在乎原 List 中的数据的顺序。
2.利用 List 的 contains 方法,遍历循环,重新排序,只添加一次数据
import java.util.*;
public class ListDemo {
private void removeRepeat(List<Integer> listInt) {
List<Integer> list = new ArrayList<>(listInt.size());
for (Integer i : listInt) {
if (!list.contains(i)) {
list.add(i);
}
}
System.out.println("去重后的list = " + list);
}
public static void main(String[] args) {
ListDemo listDemo = new ListDemo();
Integer[] array = {1,1,3,5,7,6,4,3,4,5,8,2};
List<Integer> listInt = Arrays.asList(array);
System.out.println("去重前的list = " + listInt);
listDemo.removeRepeat(listInt);
}
}
这种方法对于 String 类型的 List 同样适用。
import java.util.*;
public class ListDemo {
private void removeRepeat(List<String> listStr) {
List<String> list = new ArrayList<>();
for (String str : listStr) {
if (!list.contains(str)) {
list.add(str);
}
}
System.out.println("去重后的list = " + list);
}
public static void main(String[] args) {
ListDemo listDemo = new ListDemo();
String[] array = {"A","B","B","C","A","D","E","C","F","D","G"};
List<String> listStr = Arrays.asList(array);
System.out.println("去重前的list = " + listStr);
listDemo.removeRepeat(listStr);
}
}
3.使用 HashSet
利用 HashSet 不能添加重复数据的特性来对 List 进行去重
import java.util.*;
public class ListDemo {
private void removeRepeat(List<String> listStr) {
List<String> list = new ArrayList<>();
HashSet<String> set = new HashSet<>();
for (String str : listStr) {
if (set.add(str)) {
list.add(str);
}
}
System.out.println("去重后的list = " + list);
}
public static void main(String[] args) {
ListDemo listDemo = new ListDemo();
String[] array = {"A","B","B","C","A","D","E","C","F","D","G"};
List<String> listStr = Arrays.asList(array);
System.out.println("去重前的list = " + listStr);
listDemo.removeRepeat(listStr);
}
}
4.使用 java 8 新特性 stream 对 List 进行去重操作
public static void main(String[] args) {
String[] array = {"A","B","B","C","A","G","E","C","F","D","G"};
List<String> listStr = Arrays.asList(array);
System.out.println("去重前的list = " + listStr);
List<String> result = listStr.stream().distinct().collect(Collectors.toList());
System.out.println("去重后的list = " + result);
}
5.使用 LinkedHashSet 来对 List 进行去重操作
public static void main(String[] args) {
String[] array = {"A","B","B","C","A","G","E","C","F","D","G"};
List<String> listStr = Arrays.asList(array);
System.out.println("去重前的list = " + listStr);
LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>(listStr);
List<String> result = new ArrayList<>(linkedHashSet);
System.out.println("去重后的list = " + result);
}
当需要按照指定的对象属性去去重时,上面这几种方法就不是很适用了,推荐 Stream 的另一种方法:
List<AaDto> collect = list.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(
Comparator.comparing(p->p.getA() + p.getB()))), ArrayList::new);
同时根据结果集 list 中的 A 属性和 B 属性去过滤数据,如果只需要根据唯一的一个属性去过滤的话,就不需要去拼接了。参考:https://www.cnblogs.com/unknows/p/13534953.html