Java关于List集合去重方案详细介绍
1 常规去重
碰到List去重的问题,除了遍历去重,我们常常想到利用Set集合不允许重复元素的特点,通过List和Set互转,来去掉重复元素。
// 遍历后判断赋给另一个List集合,保持原来顺序 public static void ridRepeat1(List<String> list) { System.out.println("list = [" + list + "]"); List<String> listNew = new ArrayList<String>(); for (String str : list) { if (!listNew.contains(str)) { listNew.add(str); } } System.out.println("listNew = [" + listNew + "]"); } // Set集合去重,保持原来顺序 public static void ridRepeat2(List<String> list) { System.out.println("list = [" + list + "]"); List<String> listNew = new ArrayList<String>(); Set set = new HashSet(); for (String str : list) { if (set.add(str)) { listNew.add(str); } } System.out.println("listNew = [" + listNew + "]"); } // Set去重 由于Set(HashSet)的无序性,不会保持原来顺序 public static void ridRepeat3(List<String> list) { System.out.println("list = [" + list + "]"); Set set = new HashSet(); List<String> listNew = new ArrayList<String>(); set.addAll(list); listNew.addAll(set); System.out.println("listNew = [" + listNew + "]"); } // Set通过HashSet去重(将ridRepeat3方法缩减为一行) 无序 public static void ridRepeat4(List<String> list) { System.out.println("list = [" + list + "]"); List<String> listNew = new ArrayList<String>(new HashSet(list)); System.out.println("listNew = [" + listNew + "]"); } // Set通过TreeSet去重 会按字典顺序重排序 public static void ridRepeat5(List<String> list) { System.out.println("list = [" + list + "]"); List<String> listNew = new ArrayList<String>(new TreeSet<String>(list)); System.out.println("listNew = [" + listNew + "]"); } // Set通过LinkedHashSet去重 保持原来顺序 public static void ridRepeat6(List<String> list) { System.out.println("list = [" + list + "]"); List<String> listNew = new ArrayList<String>(new LinkedHashSet<String>(list)); System.out.println("listNew = [" + listNew + "]"); }
stream去重
//利用java8的stream去重 List uniqueList = list.stream().distinct().collect(Collectors.toList()); System.out.println(uniqueList.toString());
上面的方法在List元素为基本数据类型及String类型时是可以的,但是如果List集合元素为对象,却失效了!
2 对象去重
可利用for循环遍历的方式进行判断去重,很简单不赘述,看如下方法:
2.1 stream去重
// 根据name属性去重 List<User> uniqueNameList = userList .stream() .collect(collectingAndThen( toCollection(() -> new TreeSet<>(comparing(User::getName))), ArrayList::new) ); System.out.println("根据name去重后集合元素:" + uniqueNameList.toString()); // 根据name,age属性去重 List<User> uniqueNameAndAgeList = userList .stream() .collect(collectingAndThen( toCollection(() -> new TreeSet<>(comparing(o -> o.getName() + ";" + o.getAge()))), ArrayList::new) ); System.out.println("根据name,age去重后集合元素:" + uniqueNameAndAgeList.toString());
输出结果:
重写equals()、hashCode()
//重写equals方法 @Override public boolean equals(Object obj) { User user = (User) obj; return name.equals(user.getName()) && (age==user.getAge()); } //重写hashCode方法 @Override public int hashCode() { String str = name + age; return str.hashCode(); }
到此这篇关于Java关于List集合去重方案详细介绍的文章就介绍到这了,更多相关Java List集合去重内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)