一、Stream分组求和
/**
* 求和
*/
public void sum(){
List dataList = getDataList();
Map<String, Double> map = dataList.stream().collect(Collectors.groupingBy(Data::getType, Collectors.summingDouble(Data::getValue)));
}
Stream分组求和可以用来对List进行分组后求和操作,例如对于以下数据:
List<Data> dataList = Arrays.asList(
new Data("A", 1),
new Data("B", 2),
new Data("A", 3),
new Data("B", 4),
new Data("C", 5)
);
按照Data对象的type属性进行分组,再对value属性求和,可以得到如下结果:
Map<String, Double> result = {
"A": 4.0,
"B": 6.0,
"C": 5.0
}
二、Stream分组前十
/**
* 分组前十
*/
public void top(){
List dataList = getDataList();
Map<String, List<Data>> groups = dataList.stream().collect(Collectors.groupingBy(Data::getType));
Map<String, List<Data>> result = new HashMap<>();
groups.forEach((k, v) -> {
List<Data> top = v.stream()
.sorted(Comparator.comparing(Data::getValue).reversed())
.limit(10)
.collect(Collectors.toList());
result.put(k, top);
});
}
Stream分组前十可以用来对List进行分组后取出每组的前十个数据,例如对于以下数据:
List<Data> dataList = Arrays.asList(
new Data("A", 1),
new Data("B", 2),
new Data("A", 3),
new Data("B", 4),
new Data("C", 5)
);
按照Data对象的type属性进行分组,再对value属性进行降序排序,取出前十个数据,可以得到如下结果:
Map<String, List<Data>> result = {
"A": [{type: "A", value: 3}, {type: "A", value: 1}],
"B": [{type: "B", value: 4}, {type: "B", value: 2}],
"C": [{type: "C", value: 5}]
}
三、Stream分组拼接
/**
* 字符串拼接
*/
public void join(){
List<Data> dataList = getDataList();
Map<String, String> result = dataList.stream().collect(Collectors.groupingBy(Data::getType, Collectors.mapping(Data::getName, Collectors.joining(","))));
}
Stream分组拼接可以用来对List进行分组后将某个属性值拼接成一个字符串,例如对于以下数据:
List<Data> dataList = Arrays.asList(
new Data("A", 1, "A1"),
new Data("B", 2, "B1"),
new Data("A", 3, "A2"),
new Data("B", 4, "B2"),
new Data("C", 5, "C1")
);
按照Data对象的type属性进行分组,再对name属性进行拼接,可以得到如下结果:
Map<String, String> result = {
"A": "A1,A2",
"B": "B1,B2",
"C": "C1"
}
四、Map转List Stream
/**
* Map转List
*/
public void mapToList(){
Map<String, List<Data>> map = getMap();
List<Data> result = map.entrySet()
.stream()
.flatMap(entry -> entry.getValue().stream().peek(data -> data.setType(entry.getKey())))
.collect(Collectors.toList());
}
Map转List Stream可以用来将Map转换为List,同时给List添加一些额外的属性,例如对于以下数据:
Map<String, List<Data>> map = {
"A": [{value: 1}, {value: 2}],
"B": [{value: 3}, {value: 4}]
}
将Map转换为List,并且在List中添加type属性,可以得到如下结果:
List<Data> result = [{type: "A", value: 1}, {type: "A", value: 2}, {type: "B", value: 3}, {type: "B", value: 4}]
五、Stream分组List
/**
* 分组List
*/
public void groupList(){
List<Data> dataList = getDataList();
Map<String, List<Data>> result = dataList.stream().collect(Collectors.groupingBy(Data::getType));
}
Stream分组List可以用来对List进行分组操作,例如对于以下数据:
List<Data> dataList = Arrays.asList(
new Data("A", 1),
new Data("B", 2),
new Data("A", 3),
new Data("B", 4),
new Data("C", 5)
);
按照Data对象的type属性进行分组,可以得到如下结果:
Map<String, List<Data>> result = {
"A": [{type: "A", value: 1}, {type: "A", value: 3}],
"B": [{type: "B", value: 2}, {type: "B", value: 4}],
"C": [{type: "C", value: 5}]
}
六、Stream分组并排序
/**
* 分组排序
*/
public void sort(){
List<Data> dataList = getDataList();
Map<String, List<Data>> result = dataList.stream()
.collect(Collectors.groupingBy(Data::getType, Collectors.collectingAndThen(Collectors.toList(), list -> {
list.sort(Comparator.comparingDouble(Data::getValue));
return list;
})));
}
Stream分组并排序可以用来对List进行分组后再按照某个属性进行排序,例如对于以下数据:
List<Data> dataList = Arrays.asList(
new Data("A", 1),
new Data("B", 2),
new Data("A", 3),
new Data("B", 4),
new Data("C", 5)
);
按照Data对象的type属性进行分组,再按照value属性进行升序排序,可以得到如下结果:
Map<String, List<Data>> result = {
"A": [{type: "A", value: 1}, {type: "A", value: 3}],
"B": [{type: "B", value: 2}, {type: "B", value: 4}],
"C": [{type: "C", value: 5}]
}
七、Stream分组计数
/**
* 统计数量
*/
public void count(){
List<Data> dataList = getDataList();
Map<String, Long> result = dataList.stream().collect(Collectors.groupingBy(Data::getType, Collectors.counting()));
}
Stream分组计数可以用来对List进行分组后统计每组数量,例如对于以下数据:
List<Data> dataList = Arrays.asList(
new Data("A", 1),
new Data("B", 2),
new Data("A", 3),
new Data("B", 4),
new Data("C", 5)
);
按照Data对象的type属性进行分组,统计每组数据的数量,可以得到如下结果:
Map<String, Long> result = {
"A": 2,
"B": 2,
"C": 1
}
八、Stream分组统计数量
/**
* 统计数量
*/
public void count(){
List<Data> dataList = getDataList();
Map<String, Integer> result = dataList.stream()
.collect(Collectors.groupingBy(Data::getType, Collectors.summingInt(Data::getValue)));
}
Stream分组统计数量可以用来对List进行分组后统计每组数据的数量并且可以针对每个数量属性进行求和,例如对于以下数据:
List<Data> dataList = Arrays.asList(
new Data("A", 1),
new Data("B", 2),
new Data("A", 3),
new Data("B", 4),
new Data("C", 5)
);
按照Data对象的type属性进行分组,统计每组数据的数量并且对value属性求和,可以得到如下结果:
Map<String, Integer> result = {
"A": 4,
"B": 6,
"C": 5
}
九、Stream分组求和BigDecimal
/**
* BigDecimal求和
*/
public void bigDecimalSumming(){
List<Data> dataList = getDataList();
Map<String, BigDecimal> result = dataList.stream()
.collect(Collectors.groupingBy(Data::getType, Collectors.mapping(Data::getValue, Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))));
}
Stream分组求和BigDecimal可以用来对List进行分组后对某个BigDecimal属性进行求和,例如对于以下数据:
List<Data> dataList = Arrays.asList(
new Data("A", new BigDecimal(1)),
new Data("B", new BigDecimal(2)),
new Data("A", new BigDecimal(3)),
new Data("B", new BigDecimal(4)),
new Data("C", new BigDecimal(5))
);
按照Data对象的type属性进行分组,再对value属性进行BigDecimal求和,可以得到如下结果:
Map<String, BigDecimal> result = {
"A": new BigDecimal(4),
"B": new BigDecimal(6),
"C": new BigDecimal(5)
}
十、Stream分组后对List处理选取
/**
* 分组取值
*/
public void groupSample(){
List<Data> dataList = getDataList();
int size = 2;
Map<String, List<String>> result = dataList.stream().collect(Collectors.groupingBy(Data::getType, Collectors.collectingAndThen(Collectors.toList(), list -> {
Collections.shuffle(list);
return list.stream().limit(size).map(Data::getName).collect(Collectors.toList());
})));
}
Stream分组后对List处理选取可以用来对List进行分组后从每组中随机选取一些数据,例如对于以下数据:
List<Data> dataList = Arrays.asList(
new Data("A", 1, "A1"),
new Data("B", 2, "B1"),
new Data("A", 3, "A2"),
new Data("B", 4, "B2"),
new Data("C", 5)
);
按照Data对象的type属性进行分组,再对List进行随机排序后选取前2个,可以得到如下结果:
Map<String, List<String>> result = {
"A": ["A2", "A1"],
"B": ["B2", "B1"],
"C": ["C1"]
}
原创文章,作者:VWVW,如若转载,请注明出处:https://www.506064.com/n/135937.html