最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java8?Stream之groupingBy分組使用解讀

 更新時間:2023年04月26日 08:35:49   作者:Archie_java  
這篇文章主要介紹了Java8?Stream之groupingBy分組使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

Java8 Stream之groupingBy分組

本文主要講解:Java 8 Stream之Collectors.groupingBy()分組示例

Collectors.groupingBy()分組之常見用法

功能代碼:

?? ?/**
?? ? * 使用java8 stream groupingBy操作,按城市分組list
?? ? */
?? ?public void groupingByCity() {
?? ??? ?Map<String, List<Employee>> map = employees.stream().collect(Collectors.groupingBy(Employee::getCity));
?
?? ??? ?map.forEach((k, v) -> {
?? ??? ??? ?System.out.println(k + " = " + v);
?? ??? ?});
?? ?}

Collectors.groupingBy() 分組之統(tǒng)計每個分組的count

功能代碼:

?? ?/**
?? ? * 使用java8 stream groupingBy操作,按城市分組list統(tǒng)計count
?? ? */
?? ?public void groupingByCount() {
?? ??? ?Map<String, Long> map = employees.stream()
?? ??? ??? ??? ?.collect(Collectors.groupingBy(Employee::getCity, Collectors.counting()));
?
?? ??? ?map.forEach((k, v) -> {
?? ??? ??? ?System.out.println(k + " = " + v);
?? ??? ?});
?? ?}

Collectors.groupingBy() 分組之統(tǒng)計分組平均值

功能代碼:

?? ?/**
?? ? * 使用java8 stream groupingBy操作,按城市分組list并計算分組年齡平均值
?? ? */
?? ?public void groupingByAverage() {
?? ??? ?Map<String, Double> map = employees.stream()
?? ??? ??? ??? ?.collect(Collectors.groupingBy(Employee::getCity, Collectors.averagingInt(Employee::getAge)));
?
?? ??? ?map.forEach((k, v) -> {
?? ??? ??? ?System.out.println(k + " = " + v);
?? ??? ?});
?? ?}

Collectors.groupingBy() 分組之統(tǒng)計分組總值

功能代碼:

/**
?? ? * 使用java8 stream groupingBy操作,按城市分組list并計算分組銷售總值
?? ? */
?? ?public void groupingBySum() {
?? ??? ?Map<String, Long> map = employees.stream()
?? ??? ??? ??? ?.collect(Collectors.groupingBy(Employee::getCity, Collectors.summingLong(Employee::getAmount)));
?
?? ??? ?map.forEach((k, v) -> {
?? ??? ??? ?System.out.println(k + " = " + v);
?? ??? ?});
?
?? ??? ?// 對Map按照分組銷售總值逆序排序
?? ??? ?Map<String, Long> sortedMap = new LinkedHashMap<>();
?? ??? ?map.entrySet().stream().sorted(Map.Entry.<String, Long> comparingByValue().reversed())
?? ??? ??? ??? ?.forEachOrdered(e -> sortedMap.put(e.getKey(), e.getValue()));
?
?? ??? ?sortedMap.forEach((k, v) -> {
?? ??? ??? ?System.out.println(k + " = " + v);
?? ??? ?});
?? ?}

Collectors.groupingBy() 分組之Join分組List

功能代碼:

/**
?? ? * 使用java8 stream groupingBy操作,按城市分組list并通過join操作連接分組list中的對象的name 屬性使用逗號分隔
?? ? */
?? ?public void groupingByString() {
?? ??? ?Map<String, String> map = employees.stream().collect(Collectors.groupingBy(Employee::getCity,
?? ??? ??? ??? ?Collectors.mapping(Employee::getName, Collectors.joining(", ", "Names: [", "]"))));
?
?? ??? ?map.forEach((k, v) -> {
?? ??? ??? ?System.out.println(k + " = " + v);
?? ??? ?});
?? ?}

Collectors.groupingBy() 分組之轉(zhuǎn)換分組結(jié)果List -> List

功能代碼:

/**
?? ? * 使用java8 stream groupingBy操作,按城市分組list,將List轉(zhuǎn)化為name的List
?? ? */
?? ?public void groupingByList() {
?? ??? ?Map<String, List<String>> map = employees.stream().collect(
?? ??? ??? ??? ?Collectors.groupingBy(Employee::getCity, Collectors.mapping(Employee::getName, Collectors.toList())));
?
?? ??? ?map.forEach((k, v) -> {
?? ??? ??? ?System.out.println(k + " = " + v);
?? ??? ??? ?v.stream().forEach(item -> {
?? ??? ??? ??? ?System.out.println("item = " + item);
?? ??? ??? ?});
?? ??? ?});
?? ?}

Collectors.groupingBy() 分組之轉(zhuǎn)換分組結(jié)果List -> Set

功能代碼:

?? ?/**
?? ? * 使用java8 stream groupingBy操作,按城市分組list,將List轉(zhuǎn)化為name的Set
?? ? */
?? ?public void groupingBySet() {
?? ??? ?Map<String, Set<String>> map = employees.stream().collect(
?? ??? ??? ??? ?Collectors.groupingBy(Employee::getCity, Collectors.mapping(Employee::getName, Collectors.toSet())));
?
?? ??? ?map.forEach((k, v) -> {
?? ??? ??? ?System.out.println(k + " = " + v);
?? ??? ??? ?v.stream().forEach(item -> {
?? ??? ??? ??? ?System.out.println("item = " + item);
?? ??? ??? ?});
?? ??? ?});
?? ?}

Collectors.groupingBy() 分組之使用對象分組List

功能代碼:

?? ?/**
?? ? * 使用java8 stream groupingBy操作,通過Object對象的成員分組List
?? ? */
?? ?public void groupingByObject() {
?? ??? ?Map<Manage, List<Employee>> map = employees.stream().collect(Collectors.groupingBy(item -> {
?? ??? ??? ?return new Manage(item.getName());
?? ??? ?}));
?
?? ??? ?map.forEach((k, v) -> {
?? ??? ??? ?System.out.println(k + " = " + v);
?? ??? ?});
?? ?}

Collectors.groupingBy() 分組之使用兩個成員分組List

功能代碼:

?? ?/**
?? ? * 使用java8 stream groupingBy操作, 基于city 和name 實(shí)現(xiàn)多次分組
?? ? */
?? ?public void groupingBys() {
?? ??? ?Map<String, Map<String, List<Employee>>> map = employees.stream()
?? ??? ??? ??? ?.collect(Collectors.groupingBy(Employee::getCity, Collectors.groupingBy(Employee::getName)));
?
?? ??? ?map.forEach((k, v) -> {
?? ??? ??? ?System.out.println(k + " = " + v);
?? ??? ??? ?v.forEach((i, j) -> {
?? ??? ??? ??? ?System.out.println(i + " = " + j);
?? ??? ??? ?});
?? ??? ?});
?? ?}

自定義Distinct對結(jié)果去重

功能代碼

/**
?? ? * 使用java8 stream groupingBy操作, 基于Distinct 去重數(shù)據(jù)
?? ? */
?? ?public void groupingByDistinct() {
?? ??? ?List<Employee> list = employees.stream().filter(distinctByKey(Employee :: getCity))
?? ??? ??? ??? ?.collect(Collectors.toList());;
?
?? ??? ?list.stream().forEach(item->{
?? ??? ??? ?System.out.println("city = " + item.getCity());
?? ??? ?});
?? ??? ?
?? ??? ?
?? ?}
?
?? ?/**
?? ? * 自定義重復(fù)key 規(guī)則
?? ? * @param keyExtractor
?? ? * @return
?? ? */
?? ?private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
?? ??? ?Set<Object> seen = ConcurrentHashMap.newKeySet();
?? ??? ?return t -> seen.add(keyExtractor.apply(t));
?? ?}

完整源代碼:

package com.stream;
?
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
?
/**
?* Java 8 Stream 之groupingBy 分組講解
?*?
?* @author zzg
?*
?*/
public class Java8GroupBy {
?
?? ?List<Employee> employees = new ArrayList<Employee>();
?
?? ?/**
?? ? * 數(shù)據(jù)初始化
?? ? */
?? ?public void init() {
?? ??? ?List<String> citys = Arrays.asList("湖南", "湖北", "江西", "廣西 ");
?? ??? ?for (int i = 0; i < 10; i++) {
?? ??? ??? ?Random random = new Random();
?? ??? ??? ?Integer index = random.nextInt(4);
?? ??? ??? ?Employee employee = new Employee(citys.get(index), "姓名" + i, (random.nextInt(4) * 10 - random.nextInt(4)),
?? ??? ??? ??? ??? ?(random.nextInt(4) * 1000 - random.nextInt(4)));
?? ??? ??? ?employees.add(employee);
?? ??? ?}
?? ?}
?
?? ?/**
?? ? * 使用java8 stream groupingBy操作,按城市分組list
?? ? */
?? ?public void groupingByCity() {
?? ??? ?Map<String, List<Employee>> map = employees.stream().collect(Collectors.groupingBy(Employee::getCity));
?
?? ??? ?map.forEach((k, v) -> {
?? ??? ??? ?System.out.println(k + " = " + v);
?? ??? ?});
?? ?}
?
?? ?/**
?? ? * 使用java8 stream groupingBy操作,按城市分組list統(tǒng)計count
?? ? */
?? ?public void groupingByCount() {
?? ??? ?Map<String, Long> map = employees.stream()
?? ??? ??? ??? ?.collect(Collectors.groupingBy(Employee::getCity, Collectors.counting()));
?
?? ??? ?map.forEach((k, v) -> {
?? ??? ??? ?System.out.println(k + " = " + v);
?? ??? ?});
?? ?}
?
?? ?/**
?? ? * 使用java8 stream groupingBy操作,按城市分組list并計算分組年齡平均值
?? ? */
?? ?public void groupingByAverage() {
?? ??? ?Map<String, Double> map = employees.stream()
?? ??? ??? ??? ?.collect(Collectors.groupingBy(Employee::getCity, Collectors.averagingInt(Employee::getAge)));
?
?? ??? ?map.forEach((k, v) -> {
?? ??? ??? ?System.out.println(k + " = " + v);
?? ??? ?});
?? ?}
?
?? ?/**
?? ? * 使用java8 stream groupingBy操作,按城市分組list并計算分組銷售總值
?? ? */
?? ?public void groupingBySum() {
?? ??? ?Map<String, Long> map = employees.stream()
?? ??? ??? ??? ?.collect(Collectors.groupingBy(Employee::getCity, Collectors.summingLong(Employee::getAmount)));
?
?? ??? ?map.forEach((k, v) -> {
?? ??? ??? ?System.out.println(k + " = " + v);
?? ??? ?});
?
?? ??? ?// 對Map按照分組銷售總值逆序排序
?? ??? ?Map<String, Long> sortedMap = new LinkedHashMap<>();
?? ??? ?map.entrySet().stream().sorted(Map.Entry.<String, Long> comparingByValue().reversed())
?? ??? ??? ??? ?.forEachOrdered(e -> sortedMap.put(e.getKey(), e.getValue()));
?
?? ??? ?sortedMap.forEach((k, v) -> {
?? ??? ??? ?System.out.println(k + " = " + v);
?? ??? ?});
?? ?}
?
?? ?/**
?? ? * 使用java8 stream groupingBy操作,按城市分組list并通過join操作連接分組list中的對象的name 屬性使用逗號分隔
?? ? */
?? ?public void groupingByString() {
?? ??? ?Map<String, String> map = employees.stream().collect(Collectors.groupingBy(Employee::getCity,
?? ??? ??? ??? ?Collectors.mapping(Employee::getName, Collectors.joining(", ", "Names: [", "]"))));
?
?? ??? ?map.forEach((k, v) -> {
?? ??? ??? ?System.out.println(k + " = " + v);
?? ??? ?});
?? ?}
?
?? ?/**
?? ? * 使用java8 stream groupingBy操作,按城市分組list,將List轉(zhuǎn)化為name的List
?? ? */
?? ?public void groupingByList() {
?? ??? ?Map<String, List<String>> map = employees.stream().collect(
?? ??? ??? ??? ?Collectors.groupingBy(Employee::getCity, Collectors.mapping(Employee::getName, Collectors.toList())));
?
?? ??? ?map.forEach((k, v) -> {
?? ??? ??? ?System.out.println(k + " = " + v);
?? ??? ??? ?v.stream().forEach(item -> {
?? ??? ??? ??? ?System.out.println("item = " + item);
?? ??? ??? ?});
?? ??? ?});
?? ?}
?
?? ?/**
?? ? * 使用java8 stream groupingBy操作,按城市分組list,將List轉(zhuǎn)化為name的Set
?? ? */
?? ?public void groupingBySet() {
?? ??? ?Map<String, Set<String>> map = employees.stream().collect(
?? ??? ??? ??? ?Collectors.groupingBy(Employee::getCity, Collectors.mapping(Employee::getName, Collectors.toSet())));
?
?? ??? ?map.forEach((k, v) -> {
?? ??? ??? ?System.out.println(k + " = " + v);
?? ??? ??? ?v.stream().forEach(item -> {
?? ??? ??? ??? ?System.out.println("item = " + item);
?? ??? ??? ?});
?? ??? ?});
?? ?}
?
?? ?/**
?? ? * 使用java8 stream groupingBy操作,通過Object對象的成員分組List
?? ? */
?? ?public void groupingByObject() {
?? ??? ?Map<Manage, List<Employee>> map = employees.stream().collect(Collectors.groupingBy(item -> {
?? ??? ??? ?return new Manage(item.getName());
?? ??? ?}));
?
?? ??? ?map.forEach((k, v) -> {
?? ??? ??? ?System.out.println(k + " = " + v);
?? ??? ?});
?? ?}
?
?? ?/**
?? ? * 使用java8 stream groupingBy操作, 基于city 和name 實(shí)現(xiàn)多次分組
?? ? */
?? ?public void groupingBys() {
?? ??? ?Map<String, Map<String, List<Employee>>> map = employees.stream()
?? ??? ??? ??? ?.collect(Collectors.groupingBy(Employee::getCity, Collectors.groupingBy(Employee::getName)));
?
?? ??? ?map.forEach((k, v) -> {
?? ??? ??? ?System.out.println(k + " = " + v);
?? ??? ??? ?v.forEach((i, j) -> {
?? ??? ??? ??? ?System.out.println(i + " = " + j);
?? ??? ??? ?});
?? ??? ?});
?? ?}
?
?? ?/**
?? ? * 使用java8 stream groupingBy操作, 基于Distinct 去重數(shù)據(jù)
?? ? */
?? ?public void groupingByDistinct() {
?? ??? ?List<Employee> list = employees.stream().filter(distinctByKey(Employee :: getCity))
?? ??? ??? ??? ?.collect(Collectors.toList());;
?
?? ??? ?list.stream().forEach(item->{
?? ??? ??? ?System.out.println("city = " + item.getCity());
?? ??? ?});
?? ??? ?
?? ??? ?
?? ?}
?
?? ?/**
?? ? * 自定義重復(fù)key 規(guī)則
?? ? * @param keyExtractor
?? ? * @return
?? ? */
?? ?private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
?? ??? ?Set<Object> seen = ConcurrentHashMap.newKeySet();
?? ??? ?return t -> seen.add(keyExtractor.apply(t));
?? ?}
?
?? ?public static void main(String[] args) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?Java8GroupBy instance = new Java8GroupBy();
?? ??? ?instance.init();
?? ??? ?instance.groupingByCity();
?? ??? ?instance.groupingByCount();
?? ??? ?instance.groupingByAverage();
?? ??? ?instance.groupingBySum();
?? ??? ?instance.groupingByString();
?? ??? ?instance.groupingByList();
?? ??? ?instance.groupingBySet();
?? ??? ?instance.groupingByObject();
?? ??? ?instance.groupingBys();
?? ??? ?instance.groupingByDistinct();
?
?? ?}
?
?? ?class Employee {
?? ??? ?private String city;
?? ??? ?private String name;
?? ??? ?private Integer age;
?? ??? ?private Integer amount;
?
?? ??? ?public String getCity() {
?? ??? ??? ?return city;
?? ??? ?}
?
?? ??? ?public void setCity(String city) {
?? ??? ??? ?this.city = city;
?? ??? ?}
?
?? ??? ?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 Integer getAmount() {
?? ??? ??? ?return amount;
?? ??? ?}
?
?? ??? ?public void setAmount(Integer amount) {
?? ??? ??? ?this.amount = amount;
?? ??? ?}
?
?? ??? ?public Employee(String city, String name, Integer age, Integer amount) {
?? ??? ??? ?super();
?? ??? ??? ?this.city = city;
?? ??? ??? ?this.name = name;
?? ??? ??? ?this.age = age;
?? ??? ??? ?this.amount = amount;
?? ??? ?}
?
?? ??? ?public Employee() {
?? ??? ??? ?super();
?? ??? ?}
?? ?}
?
?? ?class Manage {
?? ??? ?private String name;
?
?? ??? ?public String getName() {
?? ??? ??? ?return name;
?? ??? ?}
?
?? ??? ?public void setName(String name) {
?? ??? ??? ?this.name = name;
?? ??? ?}
?
?? ??? ?public Manage(String name) {
?? ??? ??? ?super();
?? ??? ??? ?this.name = name;
?? ??? ?}
?
?? ??? ?public Manage() {
?? ??? ??? ?super();
?? ??? ?}
?? ?}
?
}

總結(jié)

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • springboot + vue 實(shí)現(xiàn)遞歸生成多級菜單(實(shí)例代碼)

    springboot + vue 實(shí)現(xiàn)遞歸生成多級菜單(實(shí)例代碼)

    這篇文章主要介紹了springboot + vue 實(shí)現(xiàn)遞歸生成多級菜單,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-12-12
  • 滴滴二面之Kafka如何讀寫副本消息的

    滴滴二面之Kafka如何讀寫副本消息的

    這篇文章主要給大家介紹了關(guān)于滴滴二面之Kafka如何讀寫副本消息的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2022-01-01
  • Java try-with-resource語法使用解析

    Java try-with-resource語法使用解析

    這篇文章主要介紹了Java try-with-resource語法使用解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • 詳解批處理框架之Spring Batch

    詳解批處理框架之Spring Batch

    Spring Batch是一個輕量級的、完善的批處理框架,作為Spring體系中的一員,它擁有靈活、方便、生產(chǎn)可用的特點(diǎn)。在應(yīng)對高效處理大量信息、定時處理大量數(shù)據(jù)等場景十分簡便。結(jié)合調(diào)度框架能更大地發(fā)揮Spring Batch的作用
    2021-06-06
  • 關(guān)于MyBatis Plus中使用or和and問題

    關(guān)于MyBatis Plus中使用or和and問題

    這篇文章主要介紹了關(guān)于MyBatis Plus中使用or和and問題,需要的朋友可以參考下
    2020-12-12
  • SpringCloud中的@RefreshScope注解與使用場景方式

    SpringCloud中的@RefreshScope注解與使用場景方式

    SpringCloud中的@RefreshScope注解用于動態(tài)刷新Bean配置,解決外部配置變化時的問題,避免重啟應(yīng)用,通過本文的詳細(xì)介紹,希望讀者能夠更好地掌握@RefreshScope的使用技巧,在實(shí)際項目中靈活應(yīng)用,提升微服務(wù)應(yīng)用的動態(tài)配置管理能力
    2024-12-12
  • 深入理解Java中的注解Annotation

    深入理解Java中的注解Annotation

    這篇文章主要介紹了深入理解Java中的注解Annotation,注解在Java中確實(shí)也很常見,但是人們常常不會自己定義一個注解拿來用,我們雖然很少去自定義注解,但是學(xué)會注解的寫法,注解的定義,學(xué)會利用反射解析注解中的信息,在開發(fā)中能夠使用到,這是很關(guān)鍵的,需要的朋友可以參考下
    2023-10-10
  • springboot集成nacos的配置方法

    springboot集成nacos的配置方法

    這篇文章主要介紹了springboot集成nacos的配置方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • Springboot無法注入service問題

    Springboot無法注入service問題

    這篇文章主要介紹了Springboot無法注入service的問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • 在CentOS系統(tǒng)上安裝Java?JDK?8簡單步驟

    在CentOS系統(tǒng)上安裝Java?JDK?8簡單步驟

    最近購買一臺新的云服務(wù)器,用于開發(fā)學(xué)習(xí)使用,因此需要安裝很多的組件,下面這篇文章主要給大家介紹了關(guān)于在CentOS系統(tǒng)上安裝Java?JDK8的簡單步驟,需要的朋友可以參考下
    2023-12-12

最新評論

万山特区| 家居| 临安市| 甘洛县| 凉城县| 临邑县| 垫江县| 大同县| 齐河县| 元阳县| 睢宁县| 阿拉善左旗| 青浦区| 平度市| 陆川县| 纳雍县| 晋州市| 东丰县| 铁岭市| 宁都县| 博爱县| 论坛| 辉南县| 云安县| 剑河县| 射阳县| 泌阳县| 留坝县| 景泰县| 丰原市| 平定县| 新建县| 江山市| 阿克苏市| 清远市| 梁平县| 滕州市| 卢龙县| 仁寿县| 绥棱县| 道孚县|