您好,登錄后才能下訂單哦!
這篇文章主要介紹“Java8 Stream之groupingBy分組如何使用”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“Java8 Stream之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); }); }
功能代碼:
/** * 使用java8 stream groupingBy操作,按城市分組list統計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轉化為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轉化為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 實現多次分組 */ 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 去重數據 */ 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()); }); } /** * 自定義重復key 規則 * @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>(); /** * 數據初始化 */ 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統計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轉化為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轉化為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 實現多次分組 */ 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 去重數據 */ 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()); }); } /** * 自定義重復key 規則 * @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(); } } }
關于“Java8 Stream之groupingBy分組如何使用”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。