JAVA8List去重方法


幾種列表去重的方法

在這里我來分享幾種列表去重的方法,算是一次整理吧,如有紕漏,請不吝賜教。

1. Stream 的distinct()方法

distinct()是Java 8 中 Stream 提供的方法,返回的是由該流中不同元素組成的流。distinct()使用 hashCode() 和 eqauls() 方法來獲取不同的元素。因此,需要去重的類必須實現 hashCode() 和 equals() 方法。換句話講,我們可以通過重寫定制的 hashCode() 和 equals() 方法來達到某些特殊需求的去重。

distinct() 方法聲明如下:

  1.  
    Stream<T> distinct();
  2.  
    復制代碼

1.1 對於 String 列表的去重

因為 String 類已經覆寫了 equals() 和 hashCode() 方法,所以可以去重成功。

  1.  
    @Test
  2.  
    public void listDistinctByStreamDistinct() {
  3.  
    // 1. 對於 String 列表去重
  4.  
    List<String> stringList = new ArrayList<String>() {{
  5.  
    add( "A");
  6.  
    add( "A");
  7.  
    add( "B");
  8.  
    add( "B");
  9.  
    add( "C");
  10.  
    }};
  11.  
    out.print( "去重前:");
  12.  
    for (String s : stringList) {
  13.  
    out.print(s);
  14.  
    }
  15.  
    out.println();
  16.  
    stringList = stringList.stream().distinct().collect(Collectors.toList());
  17.  
    out.print( "去重后:");
  18.  
    for (String s : stringList) {
  19.  
    out.print(s);
  20.  
    }
  21.  
    out.println();
  22.  
    }
  23.  
    復制代碼

結果如下:

  1.  
    去重前:AABBC
  2.  
    去重后:ABC
  3.  
    復制代碼

1.2 對於實體類列表的去重

注:代碼中我們使用了 Lombok 插件的 @Data注解,可自動覆寫 equals() 以及 hashCode() 方法。

  1.  
    /**
  2.  
    * 定義一個實體類
  3.  
    */
  4.  
    @Data
  5.  
    public class Student {
  6.  
    private String stuNo;
  7.  
    private String name;
  8.  
    }
  9.  
    復制代碼
  1.  
    @Test
  2.  
    public void listDistinctByStreamDistinct() throws JsonProcessingException {
  3.  
    ObjectMapper objectMapper = new ObjectMapper();
  4.  
    // 1. 對於 Student 列表去重
  5.  
    List<Student> studentList = getStudentList();
  6.  
    out.print( "去重前:");
  7.  
    out.println(objectMapper.writeValueAsString(studentList));
  8.  
    studentList = studentList.stream().distinct().collect(Collectors.toList());
  9.  
    out.print( "去重后:");
  10.  
    out.println(objectMapper.writeValueAsString(studentList));
  11.  
    }
  12.  
    復制代碼

結果如下:

  1.  
    去重前:[{ "stuNo":"001","name":"Tom"},{"stuNo":"002","name":"Mike"},{"stuNo":"001","name":"Tom"}]
  2.  
    去重后:[{ "stuNo":"001","name":"Tom"},{"stuNo":"002","name":"Mike"}]
  3.  
    復制代碼

2. 根據 List<Object> 中 Object 某個屬性去重

2.1 新建一個列表出來

  1.  
    @Test
  2.  
    public void distinctByProperty1() throws JsonProcessingException {
  3.  
    // 這里第一種方法我們通過新創建一個只有不同元素列表來實現根據對象某個屬性去重
  4.  
    ObjectMapper objectMapper = new ObjectMapper();
  5.  
    List<Student> studentList = getStudentList();
  6.  
     
  7.  
    out.print( "去重前 :");
  8.  
    out.println(objectMapper.writeValueAsString(studentList));
  9.  
    studentList = studentList.stream().distinct().collect(Collectors.toList());
  10.  
    out.print( "distinct去重后:");
  11.  
    out.println(objectMapper.writeValueAsString(studentList));
  12.  
    // 這里我們引入了兩個靜態方法,以及通過 TreeSet<> 來達到獲取不同元素的效果
  13.  
    // 1. import static java.util.stream.Collectors.collectingAndThen;
  14.  
    // 2. import static java.util.stream.Collectors.toCollection;
  15.  
    studentList = studentList.stream().collect(
  16.  
    collectingAndThen(
  17.  
    toCollection(() -> new TreeSet<>(Comparator.comparing(Student::getName))), ArrayList::new)
  18.  
    );
  19.  
    out.print( "根據名字去重后 :");
  20.  
    out.println(objectMapper.writeValueAsString(studentList));
  21.  
    }
  22.  
    復制代碼

結果如下:

  1.  
    去重前 :[{ "stuNo":"001","name":"Tom"},{"stuNo":"001","name":"Tom"},{"stuNo":"003","name":"Tom"}]
  2.  
    distinct去重后:[{ "stuNo":"001","name":"Tom"},{"stuNo":"003","name":"Tom"}]
  3.  
    根據名字去重后 :[{ "stuNo":"001","name":"Tom"}]
  4.  
    復制代碼

2.2 通過 filter() 方法

我們首先創建一個方法作為 Stream.filter() 的參數,其返回類型為 Predicate,原理就是判斷一個元素能否加入到 Set 中去,代碼如下:

  1.  
    private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
  2.  
    Set<Object> seen = ConcurrentHashMap.newKeySet();
  3.  
    return t -> seen.add(keyExtractor.apply(t));
  4.  
    }
  5.  
    復制代碼

使用如下:

  1.  
    @Test
  2.  
    public void distinctByProperty2() throws JsonProcessingException {
  3.  
    // 這里第二種方法我們通過過濾來實現根據對象某個屬性去重
  4.  
    ObjectMapper objectMapper = new ObjectMapper();
  5.  
    List<Student> studentList = getStudentList();
  6.  
     
  7.  
    out.print( "去重前 :");
  8.  
    out.println(objectMapper.writeValueAsString(studentList));
  9.  
    studentList = studentList.stream().distinct().collect(Collectors.toList());
  10.  
    out.print( "distinct去重后:");
  11.  
    out.println(objectMapper.writeValueAsString(studentList));
  12.  
    // 這里我們將 distinctByKey() 方法作為 filter() 的參數,過濾掉那些不能加入到 set 的元素
  13.  
    studentList = studentList.stream().filter(distinctByKey(Student::getName)).collect(Collectors.toList());
  14.  
    out.print( "根據名字去重后 :");
  15.  
    out.println(objectMapper.writeValueAsString(studentList));
  16.  
    }
  17.  
    復制代碼

結果如下:

  1.  
    去重前 :[{ "stuNo":"001","name":"Tom"},{"stuNo":"001","name":"Tom"},{"stuNo":"003","name":"Tom"}]
  2.  
    distinct去重后:[{ "stuNo":"001","name":"Tom"},{"stuNo":"003","name":"Tom"}]
  3.  
    根據名字去重后 :[{ "stuNo":"001","name":"Tom"}]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM