Stream是Java 8引入的一款新特性,它支持函數式編程,可以使用Lambda表達式來對集合進行操作,使代碼更簡潔、優雅。而去重是Stream中常見的操作之一,那麼Stream如何去重呢?本文將從多個方面對Stream去重做詳細的闡述。
一、Stream去重字段
如果我們要對一個對象的某個字段進行去重,可以使用Stream的distinct()方法來實現。例如我們有一個Person對象,包含id和name兩個屬性,我們要根據name來去重:
List<Person> list = getList();
list.stream().map(Person::getName).distinct().forEach(System.out::println);
在上述代碼中,我們首先將List轉換為Stream,然後使用map方法獲取每個Person對象的name屬性,使用distinct()方法來去重,最後使用forEach打印每個不同的name。
二、Stream去重list元素方法
如果我們需要對一個List對象的元素進行去重,可以使用Stream的distinct()方法結合Collector的toCollection()方法來實現。例如我們有一個List<Integer>對象,要對其中的元素進行去重:
List<Integer> list = Arrays.asList(1, 2, 3, 1, 2, 4);
List<Integer> distinctList = list.stream().distinct().collect(Collectors.toCollection(ArrayList::new));
System.out.println(distinctList);
在上述代碼中,我們首先將List轉換為Stream,然後使用distinct()方法來去重,最後使用Collector的toCollection()方法將結果轉換為ArrayList。
三、Stream去重源碼
如果我們需要查看Stream去重的源碼,可以查看Java API文檔,或者在IDE中進入Stream的distinct()方法進行查看。以下是官方API對distinct()方法的定義:
default Stream<T> distinct() {
return this.<T, T, HashSet<T>>collect(Collectors.toSet()).stream();
}
可以看到,stream的distinct()方法實際上是調用了Collectors.toSet()方法來生成一個HashSet對象,然後再將HashSet轉換為Stream對象。
四、Stream去重的四種方法
除了使用distinct()方法來進行去重外,我們還有其他幾種方法可以實現Stream的去重:
1.使用HashSet去重
我們可以使用HashSet對象來進行Stream的去重,其實現邏輯與distinct()方法類似:
List<Person> list = getList();
List<Person> distinctList = new ArrayList<>(new HashSet<>(list));
System.out.println(distinctList);
2.使用TreeSet去重
我們還可以使用TreeSet對象來進行Stream的去重,其可以根據元素的自然排序進行去重:
List<Integer> list = Arrays.asList(1, 2, 3, 1, 2, 4);
List<Integer> distinctList = new ArrayList<>(new TreeSet<>(list));
System.out.println(distinctList);
3.使用HashMap去重
我們可以使用HashMap來進行Stream的去重,其利用Object的hashCode()和equals()方法來判斷元素是否重複:
List<String> list = Arrays.asList("apple", "banana", "orange", "banana", "pear");
List<String> distinctList = new ArrayList<>(new HashMap<>().values());
System.out.println(distinctList);
4.使用Stream.collect(Collectors.groupingBy())方法去重
我們還可以使用groupingBy()方法來進行Stream的去重,其利用Map的鍵的唯一性來去重:
List<String> list = Arrays.asList("apple", "banana", "orange", "banana", "pear");
List<String> distinctList = list.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet().stream().filter(entry -> entry.getValue() == 1L).map(Map.Entry::getKey)
.collect(Collectors.toList());
System.out.println(distinctList);
五、Stream去重的字段獲取
在使用Stream進行去重時,我們需要注意獲取去重字段。如果是List對象,則可以直接獲取每個對象,如果是Map對象,則需要獲取Map中的value,並轉換為List對象;如果是對象集合,則需要獲取對象屬性,例如:
List<Person> list = getList();
List<String> nameList = list.stream().map(Person::getName).distinct().collect(Collectors.toList());
在上述代碼中,我們首先將List轉換為Stream,然後使用map方法獲取每個Person對象的name屬性,使用distinct()方法來去重,最後使用Collectors.toList()方法將去重結果轉換為List對象。
六、Stream去重保留最後一條
有時候我們需要對重複元素進行去重,但是保留最後一條元素,可以使用Stream的collect()方法來實現:
List<Person> list = getList();
List<Person> distinctList = list.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Person::getName).reversed())),
ArrayList::new));
System.out.println(distinctList);
在上述代碼中,我們使用Comparator.comparing()方法來根據name屬性逆序排序,然後使用collectingAndThen()方法來將去重結果轉換為ArrayList。
七、Stream去重後分組排序
如果我們需要對去重後的元素進行分組排序,可以先進行去重,然後再使用Stream的sorted()方法來實現分組排序:
List<Person> list = getList();
List<Person> distinctList = list.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Person::getName).reversed())),
ArrayList::new));
Map<String, List<Person>> groupMap = distinctList.stream().sorted((p1, p2) -> p2.getAge() - p1.getAge())
.collect(Collectors.groupingBy(Person::getName));
System.out.println(groupMap);
在上述代碼中,我們首先使用collectingAndThen()方法將去重結果轉換為ArrayList,然後使用sorted()方法按照age屬性對去重後的元素進行逆序排序,最後使用groupingBy()方法進行分組。
八、Stream去重複後limit返回
如果我們只需要去重後的前幾個元素,可以使用limit()方法來限制返回數量:
List<Person> list = getList();
List<Person> distinctList = list.stream().distinct().limit(3).collect(Collectors.toList());
System.out.println(distinctList);
在上述代碼中,我們使用limit()方法限制去重後的元素數為3,並使用toList()方法將結果轉換為List對象。
九、Stream去重根據多個條件
如果我們是需要根據多個屬性進行去重,可以使用distinct()方法結合Comparator來實現。例如我們有一個Person對象,包含id、name和age三個屬性,我們要根據name和age來去重:
List<Person> list = getList();
List<Person> distinctList = list.stream().distinct().sorted(
Comparator.comparing(Person::getName).thenComparingInt(Person::getAge))
.collect(Collectors.toList());
System.out.println(distinctList);
在上述代碼中,我們首先使用distinct()方法去重,然後使用sorted()方法按照name和age屬性進行排序,最後使用Collectors.toList()方法將結果轉換為List對象。
結語
通過本文的介紹,我們已經掌握了Stream的去重操作,包括使用distinct()方法、HashSet、TreeSet、HashMap、groupingBy()方法、collect()方法等多種方式,同時還介紹了多屬性排序、限制返回數量等高級用法。希望本文能幫助大家更好地使用Stream進行開發。
原創文章,作者:RCPXA,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/369148.html