本文主要讲解:Java 8 Stream之Collectors.groupingBy()分组示例
功能代码:
/**
* 使用java8 stream groupingBy操作,按城市分组list
*/
public void groupingByCity() {
Map> 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 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 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 map = employees.stream()
.collect(Collectors.groupingBy(Employee::getCity, Collectors.summingLong(Employee::getAmount)));
map.forEach((k, v) -> {
System.out.println(k + " = " + v);
});
// 对Map按照分组销售总值逆序排序
Map sortedMap = new LinkedHashMap<>();
map.entrySet().stream().sorted(Map.Entry. 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 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> 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> 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> 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>> 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 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 Predicate distinctByKey(Function super T, ?> keyExtractor) {
Set
完整源代码:
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 employees = new ArrayList();
/**
* 数据初始化
*/
public void init() {
List 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> 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 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 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 map = employees.stream()
.collect(Collectors.groupingBy(Employee::getCity, Collectors.summingLong(Employee::getAmount)));
map.forEach((k, v) -> {
System.out.println(k + " = " + v);
});
// 对Map按照分组销售总值逆序排序
Map sortedMap = new LinkedHashMap<>();
map.entrySet().stream().sorted(Map.Entry. 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 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> 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> 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> 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>> 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 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 Predicate distinctByKey(Function super T, ?> keyExtractor) {
Set
github 地址: 待补全
本文参考:
Java8 Stream groupingBy对List进行分组