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

Java8?Stream之groupingBy分組使用解讀

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

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)計(jì)每個(gè)分組的count

功能代碼:

?? ?/**
?? ? * 使用java8 stream groupingBy操作,按城市分組list統(tǒng)計(jì)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)計(jì)分組平均值

功能代碼:

?? ?/**
?? ? * 使用java8 stream groupingBy操作,按城市分組list并計(jì)算分組年齡平均值
?? ? */
?? ?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)計(jì)分組總值

功能代碼:

/**
?? ? * 使用java8 stream groupingBy操作,按城市分組list并計(jì)算分組銷售總值
?? ? */
?? ?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);
?? ??? ?});
?
?? ??? ?// 對(duì)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中的對(duì)象的name 屬性使用逗號(hào)分隔
?? ? */
?? ?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() 分組之使用對(duì)象分組List

功能代碼:

?? ?/**
?? ? * 使用java8 stream groupingBy操作,通過Object對(duì)象的成員分組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() 分組之使用兩個(gè)成員分組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對(duì)結(jié)果去重

功能代碼

/**
?? ? * 使用java8 stream groupingBy操作, 基于Distinct 去重?cái)?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)計(jì)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并計(jì)算分組年齡平均值
?? ? */
?? ?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并計(jì)算分組銷售總值
?? ? */
?? ?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);
?? ??? ?});
?
?? ??? ?// 對(duì)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中的對(duì)象的name 屬性使用逗號(hào)分隔
?? ? */
?? ?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對(duì)象的成員分組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 去重?cái)?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é)

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

相關(guān)文章

  • Java中的@PostConstruct注解的使用

    Java中的@PostConstruct注解的使用

    本文主要介紹了Java中的@PostConstruct注解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • springboot?整合mysql實(shí)現(xiàn)版本管理通用最新解決方案

    springboot?整合mysql實(shí)現(xiàn)版本管理通用最新解決方案

    當(dāng)springboot微服務(wù)項(xiàng)目完成從開發(fā)到測(cè)試全流程后,通常來說,最終交付產(chǎn)物是一個(gè)完整的安裝包,這篇文章主要介紹了springboot?整合mysql實(shí)現(xiàn)版本管理通用解決方案,需要的朋友可以參考下
    2023-08-08
  • SpringBoot整合Redis實(shí)現(xiàn)常用功能超詳細(xì)過程

    SpringBoot整合Redis實(shí)現(xiàn)常用功能超詳細(xì)過程

    這篇文章主要介紹了SpringBoot整合Redis實(shí)現(xiàn)常用功能,登陸功能是每個(gè)項(xiàng)目必備的功能吧,但是想設(shè)計(jì)好,卻是很難,下面介紹兩種登陸功能的解決方式,需要的朋友可以參考下
    2022-08-08
  • mybatis-plus 使用Condition拼接Sql語(yǔ)句各方法的用法

    mybatis-plus 使用Condition拼接Sql語(yǔ)句各方法的用法

    這篇文章主要介紹了mybatis-plus 使用Condition拼接Sql語(yǔ)句各方法的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java中文亂碼解決方案全解析,讓你的程序“說人話”!

    Java中文亂碼解決方案全解析,讓你的程序“說人話”!

    探索Java中文亂碼解決方案全解析,讓你的程序終于能“說人話”!厭倦了看著一串串的問號(hào)或者奇怪符號(hào)嗎?跟著我們的指南,一步步輕松解鎖中文亂碼的秘密,讓你的代碼清晰表達(dá)每一個(gè)字,需要的朋友可以參考下
    2024-02-02
  • java正則表達(dá)式的簡(jiǎn)單運(yùn)用

    java正則表達(dá)式的簡(jiǎn)單運(yùn)用

    這篇文章主要為大家詳細(xì)介紹了java正則表達(dá)式的簡(jiǎn)單運(yùn)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • hibernate 命名查詢?nèi)绾螌?shí)現(xiàn)

    hibernate 命名查詢?nèi)绾螌?shí)現(xiàn)

    Hibernate允許在映射文件中定義字符串形式的查詢語(yǔ)句,這種查詢方式成為命名查詢,需要的朋友可以參考下
    2012-11-11
  • 聊聊Java三種常見的分布式鎖

    聊聊Java三種常見的分布式鎖

    目前分布式鎖的實(shí)現(xiàn)方案主要包括三種,本文就來介紹一下這三種常見的分布式鎖以及這三種鎖的性能等,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-06-06
  • SpringMVC中日期格式的轉(zhuǎn)換

    SpringMVC中日期格式的轉(zhuǎn)換

    本文主要介紹了SpringMVC中日期格式轉(zhuǎn)換的相關(guān)知識(shí):用來解決日期提交轉(zhuǎn)換異常的問題。具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-03-03
  • mybatis實(shí)現(xiàn)特殊字段加密方式

    mybatis實(shí)現(xiàn)特殊字段加密方式

    這篇文章主要介紹了mybatis實(shí)現(xiàn)特殊字段加密,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03

最新評(píng)論