简介
本文用示例介绍Java的List(ArrayList、LinkedList等)的去重的方法。
List去重的方法
一共有这几种方法(按推荐顺序排列):
- JDK8的stream的distinct
- 转为HashSet(分不保持顺序和保持顺序两种)
- 转为TreeSet
- 使用retainAll/removeAll、contains、equals等基本方法
直接去重
package com.example.a; import java.util.*; import java.util.stream.Collectors; public class Demo { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(1); list.add(3); list.add(2); list.add(3); System.out.println("源数据: " + list); System.out.println("stream: " + stream(list)); System.out.println("hashSetWithoutOrder:" + hashSetWithoutOrder(list)); System.out.println("hashSetWithOrder: " + hashSetWithOrder(list)); System.out.println("treeSet: " + treeSet(list)); } //JDK1.8的stream去重 private static List<Integer> stream(List<Integer> list) { return list.stream().distinct().collect(Collectors.toList()); } //HashSet(不保持顺序) private static List<Integer> hashSetWithoutOrder(List<Integer> list) { HashSet<Integer> h = new HashSet<>(list); return new ArrayList<>(h); } // 删除ArrayList中重复元素,保持顺序 public static List<Integer> hashSetWithOrder(List<Integer> list) { Set<Integer> set = new HashSet<>(); List<Integer> newList = new ArrayList<>(); for (Integer element : list) { if (set.add(element)) newList.add(element); } return newList; } //TreeSet(自动排序) public static List<Integer> treeSet(List<Integer> list) { TreeSet<Integer> set = new TreeSet<>(list); return new ArrayList<>(set); } }
执行结果
源数据: [1, 3, 2, 3] stream: [1, 3, 2] hashSetWithoutOrder:[1, 2, 3] hashSetWithOrder: [1, 3, 2] treeSet: [1, 2, 3]
根据对象属性去重
法1:TreeSet
Set<User> setByName = new TreeSet<User>((o1, o2) -> o1.getName().compareTo(o2.getName())); setByName.addAll(list); List<User> listByName = new ArrayList<>(setByName); System.out.println(listByName); //[User{name='Pepper', age=20, Phone='123'}, User{name='Tony', age=20, Phone='12'}] Set<User> setByNameAndAge = new TreeSet<User>((o1, o2) -> { return (o1.getName() + o1.getAge()).compareTo((o2.getName() + o2.getAge())); // return o1.getName().compareTo(o2.getName()) == 0 // ? o1.getAge().compareTo(o2.getAge()) // : o1.getName().compareTo(o2.getName()); }); setByNameAndAge.addAll(list); List<User> listByNameAndAge = new ArrayList<>(setByNameAndAge); System.out.println(listByNameAndAge); //[User{name='Pepper', age=20, Phone='123'}, // User{name='Tony', age=20, Phone='12'}, // User{name='Tony', age=22, Phone='1234'}]
法2:stream+TreeSet
List<User> streamByNameList = list.stream().collect(Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(User::getName))), ArrayList::new )); System.out.println(streamByNameList); //[User{name='Pepper', age=20, Phone='123'}, User{name='Tony', age=20, Phone='12'}] List<User> streamByNameAndAgeList = list.stream().collect(Collectors.collectingAndThen( Collectors.toCollection( () -> new TreeSet<>(Comparator.comparing(o -> o.getName() + o.getAge()))), ArrayList::new )); System.out.println(streamByNameAndAgeList); //[User{name='Pepper', age=20, Phone='123'}, // User{name='Tony', age=20, Phone='12'}, // User{name='Tony', age=22, Phone='1234'}]
所有代码
package org.example.a; import java.util.*; import java.util.stream.Collectors; class User { private String name; private Integer age; private String Phone; public User(String name, Integer age, String phone) { this.name = name; this.age = age; Phone = phone; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getPhone() { return Phone; } public void setPhone(String phone) { Phone = phone; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + ", Phone='" + Phone + '\'' + '}'; } } public class Demo { public static void main(String[] args) { List<User> list = new ArrayList<User>() {{ add(new User("Tony", 20, "12")); add(new User("Pepper", 20, "123")); add(new User("Tony", 22, "1234")); }}; Set<User> setByName = new TreeSet<User>((o1, o2) -> o1.getName().compareTo(o2.getName())); setByName.addAll(list); List<User> listByName = new ArrayList<>(setByName); System.out.println(listByName); //[User{name='Pepper', age=20, Phone='123'}, User{name='Tony', age=20, Phone='12'}] Set<User> setByNameAndAge = new TreeSet<User>((o1, o2) -> { return (o1.getName() + o1.getAge()).compareTo((o2.getName() + o2.getAge())); // return o1.getName().compareTo(o2.getName()) == 0 // ? o1.getAge().compareTo(o2.getAge()) // : o1.getName().compareTo(o2.getName()); }); setByNameAndAge.addAll(list); List<User> listByNameAndAge = new ArrayList<>(setByNameAndAge); System.out.println(listByNameAndAge); //[User{name='Pepper', age=20, Phone='123'}, // User{name='Tony', age=20, Phone='12'}, // User{name='Tony', age=22, Phone='1234'}] List<User> streamByNameList = list.stream().collect(Collectors.collectingAndThen( Collectors.toCollection( () -> new TreeSet<>(Comparator.comparing(User::getName))), ArrayList::new )); System.out.println(streamByNameList); //[User{name='Pepper', age=20, Phone='123'}, User{name='Tony', age=20, Phone='12'}] List<User> streamByNameAndAgeList = list.stream().collect(Collectors.collectingAndThen( Collectors.toCollection( () -> new TreeSet<>(Comparator.comparing(o -> o.getName() + o.getAge()))), ArrayList::new )); System.out.println(streamByNameAndAgeList); //[User{name='Pepper', age=20, Phone='123'}, // User{name='Tony', age=20, Phone='12'}, // User{name='Tony', age=22, Phone='1234'}] } }
请先
!