javareduce分组_使⽤JAVA8stream中三个参数的reduce⽅法
对List。。。
背景
平时在编写前端代码时,习惯使⽤lodash来编写‘野⽣’的JavaScript;
lodash提供来⼀套完整的API对JS对象(Array,Object,Collection等)进⾏操作,这其中就包括_.groupBy 和 _.reduce,即分组和'聚
合'(reduce不知道该怎么翻译合适)。
使⽤这些‘野⽣’的API能够极⼤的提⾼我本⼈编写JS代码的效率。⽽JAVA8开始⽀持stream和lambda表达式,这些和lodash的API有很多类似的功能。因此我在熟悉lodash的前提下尝试使⽤JAVA8的新特性减少冗余代码的编写。
需求
在开发后端某功能接⼝的过程中,需要对⼀个从数据库中取出的数据List进⾏按照ID进⾏聚合统计
JAVA8 reduce API
API个⼈理解
U reduce(U u,BiFunctionaccumulator,BinaryOperatorcombiner)
#第⼀个参数返回实例u,传递你要返回的U类型对象的初始化实例u
#第⼆个参数累加器accumulator,可以使⽤⼆元ℷ表达式(即⼆元lambda表达式),声明你在u上累加你的数据来源t的逻辑
#例如(u,t)->u.sum(t),此时lambda表达式的⾏参列表是返回实例u和遍历的集合元素t,函数体是在u上累加t
#第三个参数组合器combiner,同样是⼆元ℷ表达式,(u,t)->u
#lambda表达式⾏参列表同样是(u,t),函数体返回的类型则要和第⼀个参数的类型保持⼀致
伪代码
#1.声明⼀个返回结果U
#2.对List进⾏遍历,在U和每个T实例上应⽤⼀个累加器进⾏累加操作
#3.返回最终结果U
U result = identity;
for (T element : this stream)
result = accumulator.apply(result, element)
return result;
数据准备
var source =
[
{"name": "A","type": "san","typeValue": 1.0,"count": 2},
{"name": "A","type": "nas","typeValue": 13.0,"count": 1},
{"name": "B","type": "san","typeValue": 112.0,"count": 3},
{"name": "C","type": "san","typeValue": 43.0,"count": 5},
{"name": "B","type": "nas","typeValue": 77.0,"count": 7}
];
var target =
[
{
groupby分组"name": "A",
"count": 3, "totalTypeValue": 14.0, "bazList": [
{
"type": "san", "typeValue": 1.0
},
{
"type": "nas" "typeValue": 13.0
}
]
},
{
"name": "B",
"count": 10, "totalTypeValue": 189.0, "bazList": [
{
"type": "san", "typeValue": 112.0
}, {
"type": "nas" "typeValue": 77.0
}
]
},
{
"name": "C",
"count": 5,
"totalTypeValue": 43.0,
"bazList": [
{
"type": "san",
"typeValue": 43.0
}
]
}
];
Code
讲了那么多废话,这个才是最直接的
代码执⾏⼤意
对 List 按照name分组统计得到 List
ReduceTest.java
llect.Lists;
import Bar;
import Foo;
import java.util.List;
import java.util.stream.Collectors;
public class ReduceTest {
public static void main(String[] args) throws Exception{ ListfooList = wArrayList(
new Foo("A","san",1.0,2),
new Foo("A","nas",13.0,1),
new Foo("B","san",112.0,3),
new Foo("C","san",43.0,5),
new Foo("B","nas",77.0,7)
);
ListbarList = wArrayList();
fooList
.stream()
.upingBy(Foo::List())) .forEach((name,fooListByName)->{
Bar bar = new Bar();
bar = fooListByName
.stream()
.reduce(bar,(u,t)->u.sum(t),(u,t)->u); System.out.String()); barList.add(bar);
});
}
/*
输出结果
name:A
count:3
totalTypeValue:14.0
bazList:
type:san
typeValue:1.0
type:nas
typeValue:13.0
name:B
count:10
totalTypeValue:189.0
bazList:
type:san
typeValue:112.0
type:nas
typeValue:77.0
name:C
count:5
totalTypeValue:43.0
bazList:
type:san
typeValue:43.0
*/
}
Foo.java
public class Foo{
private String name;
private String type;
private Double typeValue;
private Integer count;
public Foo(String name, String type, Double typeValue, Integer count) { this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
}
public Double getTypeValue() {
return typeValue;
}
public void setTypeValue(Double typeValue) {
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论