[英]Java 8 Stream: Defining collectors based on other collectors
我是Java 8 Stream API的新手,但我希望将其用于以下问题。 假设我有一个名为InputRecord
的POJO, InputRecord
包含name
, fieldA
和fieldB
属性,它们可以表示以下各项的每条记录:
name | fieldA | fieldB
----------------------
A | 100 | 1.1
A | 150 | 2.0
B | 200 | 1.5
A | 120 | 1.3
InputRecord
看起来像:
public class InputRecord {
private String name;
private Integer fieldA;
private BigDecimal fieldB;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getFieldA() {
return fieldA;
}
public void setFieldA(Integer fieldA) {
this.fieldA = fieldA;
}
public BigDecimal getFieldB() {
return fieldB;
}
public void setFieldB(BigDecimal fieldB) {
this.fieldB = fieldB;
}
}
上面的这四个记录需要合并为两个按名称分组的记录,其中:
fieldA
相加 fieldB
相加 fieldC
属性,该属性是fieldA
和fieldB
的累加总和的结果。 因此,上面的结果将是:
name | sumOfFieldA | sumOfFieldB | fieldC (sumOfFieldA*sumOfFieldB)
-------------------------------------------------------------------
A | 370 | 4.4 | 1628
B | 200 | 1.5 | 300
一个称为OutputRecord
的不同POJO将代表组合记录的每一行记录:
public class OutputRecord {
private String name;
private Integer sumOfFieldA;
private BigDecimal sumOfFieldB;
private BigDecimal fieldC;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getSumOfFieldA() {
return sumOfFieldA;
}
public void setSumOfFieldA(Integer sumOfFieldA) {
this.sumOfFieldA = sumOfFieldA;
}
public BigDecimal getSumOfFieldB() {
return sumOfFieldB;
}
public void setSumOfFieldB(BigDecimal sumOfFieldB) {
this.sumOfFieldB = sumOfFieldB;
}
public BigDecimal getFieldC() {
return fieldC;
}
public void setFieldC(BigDecimal fieldC) {
this.fieldC = fieldC;
}
}
有哪些将InputRecords列表转换为OutputRecords列表的好的方法/解决方案?
我正在查看以下链接是否有帮助,但是我试图将fieldA
和fieldB
收集器fieldB
一起以形成fieldC
的新收集器时fieldC
: Java 8流:groupingBy具有多个收集器
Collector<InputRecord, ?, Integer> fieldACollector = Collectors.summingInt(InputRecord::getFieldA);
Collector<InputRecord, ?, BigDecimal> fieldBCollector = Collectors.reducing(BigDecimal.ZERO, InputRecord::getFieldB, BigDecimal::add);
List<Collector<InputRecord, ?, ?>> collectors = Arrays.asList(fieldACollector, fieldBCollector); // need a fieldCCollector object in the list
然后,将使用collectors
对象创建一个complexCollector
对象(根据上面链接中Tagir Valeev接受的答案)。
对我而言,最干净的方法是为此构建一个自定义收集器。 这里有多行代码,但是您可以将其隐藏在一个方法下,因此您的最终操作将如下所示:
Collection<OutputRecord> output = List.of(first, second, thrid, fourth)
.stream()
.parallel()
.collect(toOutputRecords());
实际的toOutputRecords
将是:
private static Collector<InputRecord, ?, Collection<OutputRecord>> toOutputRecords() {
class Acc {
Map<String, OutputRecord> map = new HashMap<>();
void add(InputRecord elem) {
String value = elem.getName();
// constructor without fieldC since you compute it at the end
OutputRecord record = new OutputRecord(value, elem.getFieldA(), elem.getFieldB());
mergeIntoMap(map, value, record);
}
Acc merge(Acc right) {
Map<String, OutputRecord> leftMap = map;
Map<String, OutputRecord> rightMap = right.map;
for (Entry<String, OutputRecord> entry : rightMap.entrySet()) {
mergeIntoMap(leftMap, entry.getKey(), entry.getValue());
}
return this;
}
private void mergeIntoMap(Map<String, OutputRecord> map, String value, OutputRecord record) {
map.merge(value, record, (left, right) -> {
left.setSumOfFieldA(left.getSumOfFieldA() + right.getSumOfFieldA());
left.setSumOfFieldB(left.getSumOfFieldB().add(right.getSumOfFieldB()));
return left;
});
}
public Collection<OutputRecord> finisher() {
for (Entry<String, OutputRecord> e : map.entrySet()) {
OutputRecord output = e.getValue();
output.setFieldC(output.getSumOfFieldB().multiply(BigDecimal.valueOf(output.getSumOfFieldA())));
}
return map.values();
}
}
return Collector.of(Acc::new, Acc::add, Acc::merge, Acc::finisher);
}
您可以使用Stream.reduce(..)将两个记录转换为一个记录。 它创建了一堆临时对象,需要由JVM进行垃圾回收。
Collection<InputRecord> input = Arrays.asList(
new InputRecord("A", 100, new BigDecimal(1.1)),
new InputRecord("A", 150, new BigDecimal(2.0)),
new InputRecord("B", 200, new BigDecimal(1.5)),
new InputRecord("A", 120, new BigDecimal(1.3)));
Collection<OutputRecord> output = input.stream()
// group records for particular Name into a List
.collect(Collectors.groupingBy(InputRecord::getName))
.values().stream()
// Reduce every List to a single records, performing summing
.map(records -> records.stream()
.reduce((a, b) ->
new InputRecord(a.getName(),
a.getFieldA() + b.getFieldA(),
a.getFieldB().add(b.getFieldB()))))
.filter(Optional::isPresent)
.map(Optional::get)
// Finally transform the InputRecord to OutputRecord
.map(record -> new OutputRecord(record.getName(),
record.getFieldA(),
record.getFieldB(),
record.getFieldB().multiply(new BigDecimal(record.getFieldA()))))
.collect(Collectors.toList());
您可以使用合并和聚合函数从InputRecords列表生成OutputRecords列表。
Map<String, OutputRecord> result = inputRecords.stream().collect(() -> new HashMap<>(),
(HashMap<String, OutputRecord> map, InputRecord inObj) -> {
OutputRecord out = map.get(inObj.getName());
if (out == null) {
out = new OutputRecord();
out.setName(inObj.getName());
out.setSumOfFieldA(inObj.getFieldA());
out.setSumOfFieldB(inObj.getFieldB());
} else {
Integer s = out.getSumOfFieldA();
out.setSumOfFieldA(s + inObj.getFieldA());
BigDecimal bd = out.getSumOfFieldB();
out.setSumOfFieldB(bd.add(inObj.getFieldB()));
}
out.setFieldC(out.getSumOfFieldB().multiply(new BigDecimal(out.getSumOfFieldA())));
map.put(out.getName(), out);
}, (HashMap<String, OutputRecord> out1, HashMap<String, OutputRecord> out2) -> {
out1.putAll(out2);
});
System.out.println(result);
与定义自定义的收集器(我认为这很复杂且难以维护)相比,我认为将多个收集器组合在一起的通用实用方法会更好。 例如:
public static <T, A1, A2, R1, R2> Collector<T, Tuple2<A1, A2>, Tuple2<R1, R2>> combine(final Collector<? super T, A1, R1> collector1,
final Collector<? super T, A2, R2> collector2) {
...
}
使用Combine方法,解决方案将是:
Collector<InputRecord, ?, Integer> fieldACollector = MoreCollectors.summingInt(InputRecord::getFieldA);
Collector<InputRecord, ?, BigDecimal> fieldBCollector = MoreCollectors.reducing(BigDecimal.ZERO, InputRecord::getFieldB, BigDecimal::add);
inputRecords.stream().collect(MoreCollectors.groupingBy(InputRecord::getName,
MoreCollectors.combine(fieldACollector, fieldBCollector)))
.entrySet().stream()
.map(e -> new OutputRecord(e.getKey(), e.getValue()._1, e.getValue()._2))
.collect(Collectors.toList());
这是AbacusUtil中的combine
的示例实现
StreamEx.of(inputRecords)
.groupBy(InputRecord::getName, MoreCollectors.combine(fieldACollector, fieldBCollector))
.map(e -> new OutputRecord(e.getKey(), e.getValue()._1, e.getValue()._2)).toList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.