欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java8利用Stream實現(xiàn)列表去重的方法詳解

 更新時間:2022年04月14日 16:52:04   作者:Java知音  
這篇文章主要為大家介紹了Java利用Stream實現(xiàn)列表去重的幾種方法詳解,文中的示例代碼講解詳細,需要的小伙伴可以參考一下

一. Stream 的distinct()方法

distinct()是Java 8 中 Stream 提供的方法,返回的是由該流中不同元素組成的流。distinct()使用 hashCode() 和 eqauls() 方法來獲取不同的元素。

因此,需要去重的類必須實現(xiàn) hashCode() 和 equals() 方法。換句話講,我們可以通過重寫定制的 hashCode() 和 equals() 方法來達到某些特殊需求的去重。

distinct() 方法聲明如下:

Stream<T> distinct();

1.1 對于 String 列表的去重

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

@Test
public void listDistinctByStreamDistinct() {
  // 1. 對于 String 列表去重
  List<String> stringList = new ArrayList<String>() {{
    add("A");
    add("A");
    add("B");
    add("B");
    add("C");
  }};
  out.print("去重前:");
  for (String s : stringList) {
    out.print(s);
  }
  out.println();
  stringList = stringList.stream().distinct().collect(Collectors.toList());
  out.print("去重后:");
  for (String s : stringList) {
    out.print(s);
  }
  out.println();

}

結(jié)果如下:

去重前:AABBC
去重后:ABC

1.2 對于實體類列表的去重

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

/**
* 定義一個實體類
*/  
@Data
public class Student {
  private String stuNo;
  private String name;
}
@Test
public void listDistinctByStreamDistinct() throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper();
    // 1. 對于 Student 列表去重
    List<Student> studentList = getStudentList();
    out.print("去重前:");
    out.println(objectMapper.writeValueAsString(studentList));
    studentList = studentList.stream().distinct().collect(Collectors.toList());
    out.print("去重后:");
    out.println(objectMapper.writeValueAsString(studentList));
  }

結(jié)果如下:

去重前:[{"stuNo":"001","name":"Tom"},{"stuNo":"002","name":"Mike"},{"stuNo":"001","name":"Tom"}]
去重后:[{"stuNo":"001","name":"Tom"},{"stuNo":"002","name":"Mike"}]

二. 根據(jù) List<Object> 中 Object 某個屬性去重

2.1 新建一個列表出來

@Test
  public void distinctByProperty1() throws JsonProcessingException {
    // 這里第一種方法我們通過新創(chuàng)建一個只有不同元素列表來實現(xiàn)根據(jù)對象某個屬性去重
    ObjectMapper objectMapper = new ObjectMapper();
    List<Student> studentList = getStudentList();
    out.print("去重前        :");
    out.println(objectMapper.writeValueAsString(studentList));
    studentList = studentList.stream().distinct().collect(Collectors.toList());
    out.print("distinct去重后:");
    out.println(objectMapper.writeValueAsString(studentList));
    // 這里我們引入了兩個靜態(tài)方法,以及通過 TreeSet<> 來達到獲取不同元素的效果
    // 1. import static java.util.stream.Collectors.collectingAndThen;
    // 2. import static java.util.stream.Collectors.toCollection;
    studentList = studentList.stream().collect(
      collectingAndThen(
        toCollection(() -> new TreeSet<>(Comparator.comparing(Student::getName))), ArrayList::new)
    );
    out.print("根據(jù)名字去重后 :");
    out.println(objectMapper.writeValueAsString(studentList));
  }

結(jié)果如下:

去重前        :[{"stuNo":"001","name":"Tom"},{"stuNo":"001","name":"Tom"},{"stuNo":"003","name":"Tom"}]
distinct去重后:[{"stuNo":"001","name":"Tom"},{"stuNo":"003","name":"Tom"}]
根據(jù)名字去重后 :[{"stuNo":"001","name":"Tom"}]

2.2 通過 filter() 方法

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

private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
    Set<Object> seen = ConcurrentHashMap.newKeySet();
    return t -> seen.add(keyExtractor.apply(t));
}

使用如下:

@Test
  public void distinctByProperty2() throws JsonProcessingException {
    // 這里第二種方法我們通過過濾來實現(xiàn)根據(jù)對象某個屬性去重
    ObjectMapper objectMapper = new ObjectMapper();
    List<Student> studentList = getStudentList();
    out.print("去重前        :");
    out.println(objectMapper.writeValueAsString(studentList));
    studentList = studentList.stream().distinct().collect(Collectors.toList());
    out.print("distinct去重后:");
    out.println(objectMapper.writeValueAsString(studentList));
    // 這里我們將 distinctByKey() 方法作為 filter() 的參數(shù),過濾掉那些不能加入到 set 的元素
    studentList = studentList.stream().filter(distinctByKey(Student::getName)).collect(Collectors.toList());
    out.print("根據(jù)名字去重后 :");
    out.println(objectMapper.writeValueAsString(studentList));
  }

結(jié)果如下:

去重前        :[{"stuNo":"001","name":"Tom"},{"stuNo":"001","name":"Tom"},{"stuNo":"003","name":"Tom"}]
distinct去重后:[{"stuNo":"001","name":"Tom"},{"stuNo":"003","name":"Tom"}]
根據(jù)名字去重后 :[{"stuNo":"001","name":"Tom"}]

到此這篇關(guān)于Java8利用Stream實現(xiàn)列表去重的方法詳解的文章就介紹到這了,更多相關(guān)Java8 列表去重內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Hibernate命名策略詳解

    Hibernate命名策略詳解

    本文主要介紹了Hibernate命名策略。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-01-01
  • 淺談SpringBoot在使用測試的時候是否需要@RunWith

    淺談SpringBoot在使用測試的時候是否需要@RunWith

    本文主要介紹了淺談SpringBoot在使用測試的時候是否需要@RunWith,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • Springboot發(fā)送post請求的幾種方式總結(jié)

    Springboot發(fā)送post請求的幾種方式總結(jié)

    這篇文章主要為大家詳細介紹了Springboot發(fā)送post請求的幾種方式,文中的示例代碼講解詳細,對我們學(xué)習(xí)或工作有一定的幫助,感興趣的小伙伴可以了解一下
    2024-01-01
  • Java 數(shù)據(jù)結(jié)構(gòu)之堆的概念與應(yīng)用

    Java 數(shù)據(jù)結(jié)構(gòu)之堆的概念與應(yīng)用

    堆是一顆完全二叉樹,在這棵樹中,所有父節(jié)點都滿足大于等于其子節(jié)點的堆叫大根堆,所有父節(jié)點都滿足小于等于其子節(jié)點的堆叫小根堆,堆雖然是一顆樹,但是通常存放在一個數(shù)組中,父節(jié)點和孩子節(jié)點的父子關(guān)系通過數(shù)組下標來確定
    2021-10-10
  • SpringMvc后臺接收json數(shù)據(jù)中文亂碼問題詳解

    SpringMvc后臺接收json數(shù)據(jù)中文亂碼問題詳解

    這篇文章主要介紹了SpringMvc后臺接收json數(shù)據(jù)中文亂碼問題詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-09-09
  • 詳解Java I/O流中的字符流有哪些

    詳解Java I/O流中的字符流有哪些

    字節(jié)流的功能已經(jīng)十分強大,幾乎可以直接或間接地處理任何類型的輸入/輸出操作,但它卻不能直接操作16位的Unicode字符,這就需要使用字符流,所以在今天的內(nèi)容中,小編會給大家講解IO流中的字符流,希望各位能夠繼續(xù)耐心學(xué)習(xí)
    2023-10-10
  • Java中Comparable與Comparator的區(qū)別解析

    Java中Comparable與Comparator的區(qū)別解析

    這篇文章主要介紹了Java中Comparable與Comparator的區(qū)別解析,實現(xiàn)Comparable接口,重寫compareTo方法,一般在實體類定義的時候就可以選擇實現(xiàn)該接口,提供一個默認的排序方式,供Arrays.sort和Collections.sort使用,需要的朋友可以參考下
    2024-01-01
  • SpringBoot+Hibernate實現(xiàn)自定義數(shù)據(jù)驗證及異常處理

    SpringBoot+Hibernate實現(xiàn)自定義數(shù)據(jù)驗證及異常處理

    這篇文章主要為大家介紹了SpringBoot如何整合Hibernate自定義數(shù)據(jù)驗證及多種方式異常處理,文中的示例代碼講解詳細,感興趣的可以了解一下
    2022-04-04
  • java藍橋杯歷年真題及答案整理(小結(jié))

    java藍橋杯歷年真題及答案整理(小結(jié))

    這篇文章主要介紹了java藍橋杯歷年真題及答案整理(小結(jié)),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • SpringBoot中配置AOP詳解

    SpringBoot中配置AOP詳解

    這篇文章主要介紹了SpringBoot中配置AOP詳解,Spring Boot 在Spring 的基礎(chǔ)上對AOP的配置提供了自動化配置解決方案spring-boot-starter-aop,使開發(fā)者能夠更加便捷地在Spring Boot項目中使用AOP,需要的朋友可以參考下
    2024-01-01

最新評論