Java8 Stream之groupingBy分组使用解读
Java8 Stream之groupingBy分组
本文主要讲解:Java 8 Stream之Collectors.groupingBy()分组示例
Collectors.groupingBy()分组之常见用法
功能代码:
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() 分组之统计每个分组的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() 分组之统计分组平均值
功能代码:
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() 分组之统计分组总值
功能代码:
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
功能代码:
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() 分组之转换分组结果List -> 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() 分组之转换分组结果List -> 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
功能代码:
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
功能代码:
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对结果去重
功能代码
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());
});
}
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;
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);
}
}
public void groupingByCity() {
Map<String, List<Employee>> map = employees.stream().collect(Collectors.groupingBy(Employee::getCity));
map.forEach((k, v) -> {
System.out.println(k + " = " + v);
});
}
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);
});
}
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);
});
}
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);
});
}
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);
});
}
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);
});
});
}
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);
});
});
}
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);
});
}
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);
});
});
}
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());
});
}
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();
}
}
}
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
相关文章