Java實現List去重的方法詳解
簡介
本文用示例介紹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:" + hashSetWithOrder(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<Integer>(list); return new ArrayList<>(h); } // 刪除ArrayList中重復元素,保持順序 public static List<Integer> hashSetWithOrder(List<Integer> list) { Set<Integer> set = new HashSet<Integer>(); List<Integer> newList = new ArrayList<Integer>(); 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<Integer>(list); return new ArrayList<>(set); } }
執行結果
源數據: [1, 3, 2, 3]
stream: [1, 3, 2]
hashSetWithoutOrder:[1, 3, 2]
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'}] } }
到此這篇關於Java實現List去重的方法詳解的文章就介紹到這瞭,更多相關Java List去重內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- Java關於List集合去重方案詳細介紹
- 關於List、Map、Stream初始化方式
- java8實現List中對象屬性的去重方法
- java8如何用Stream查List對象某屬性是否有重復
- 手把手帶你瞭解Java-Stream流方法學習及總結