这种情况下其实不太适合用reduce,如果非要用,那就是这样写的。
import lombok.Data;
import lombok.ToString;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* @author dushixiang
* @date 2020/11/3 20:00 下午
*/
public class Reducing {
@ToString
@Data
public static class Item {
/**
* 月份
*/
private int month;
/**
* 值
*/
private int count;
/**
* 计算后的值
*/
private int result;
}
public static void main(String[] args) {
// 假设已经计算出每个月的数据
List<Item> items = IntStream.rangeClosed(1, 12)
.mapToObj(month -> {
Item item = new Item();
item.setMonth(month);
item.setCount((int) (Math.random() * 100));
return item;
})
.collect(Collectors.toList());
// 新建一个列表用于存储累加计算后的结果
List<Item> results = new ArrayList<>(items.size());
items.stream()
.reduce(new Item(), (acc, n) -> {
Item result = new Item();
result.setMonth(n.getMonth());
result.setCount(n.getCount());
result.setResult(acc.getResult() + n.getCount());
System.out.println("acc : " + acc);
System.out.println("n : " + n);
System.out.println("result: " + result);
results.add(result);
System.out.println("-------------------");
return result;
});
System.out.println("=====================");
System.out.println(results);
}
}
输出结果
acc : Reducing.Item(month=0, count=0, result=0)
n : Reducing.Item(month=1, count=42, result=0)
result: Reducing.Item(month=1, count=42, result=42)
-------------------
acc : Reducing.Item(month=1, count=42, result=42)
n : Reducing.Item(month=2, count=45, result=0)
result: Reducing.Item(month=2, count=45, result=87)
-------------------
acc : Reducing.Item(month=2, count=45, result=87)
n : Reducing.Item(month=3, count=43, result=0)
result: Reducing.Item(month=3, count=43, result=130)
-------------------
acc : Reducing.Item(month=3, count=43, result=130)
n : Reducing.Item(month=4, count=53, result=0)
result: Reducing.Item(month=4, count=53, result=183)
-------------------
acc : Reducing.Item(month=4, count=53, result=183)
n : Reducing.Item(month=5, count=80, result=0)
result: Reducing.Item(month=5, count=80, result=263)
-------------------
acc : Reducing.Item(month=5, count=80, result=263)
n : Reducing.Item(month=6, count=51, result=0)
result: Reducing.Item(month=6, count=51, result=314)
-------------------
acc : Reducing.Item(month=6, count=51, result=314)
n : Reducing.Item(month=7, count=2, result=0)
result: Reducing.Item(month=7, count=2, result=316)
-------------------
acc : Reducing.Item(month=7, count=2, result=316)
n : Reducing.Item(month=8, count=23, result=0)
result: Reducing.Item(month=8, count=23, result=339)
-------------------
acc : Reducing.Item(month=8, count=23, result=339)
n : Reducing.Item(month=9, count=36, result=0)
result: Reducing.Item(month=9, count=36, result=375)
-------------------
acc : Reducing.Item(month=9, count=36, result=375)
n : Reducing.Item(month=10, count=60, result=0)
result: Reducing.Item(month=10, count=60, result=435)
-------------------
acc : Reducing.Item(month=10, count=60, result=435)
n : Reducing.Item(month=11, count=60, result=0)
result: Reducing.Item(month=11, count=60, result=495)
-------------------
acc : Reducing.Item(month=11, count=60, result=495)
n : Reducing.Item(month=12, count=16, result=0)
result: Reducing.Item(month=12, count=16, result=511)
-------------------
=====================
[Reducing.Item(month=1, count=42, result=42), Reducing.Item(month=2, count=45, result=87), Reducing.Item(month=3, count=43, result=130), Reducing.Item(month=4, count=53, result=183), Reducing.Item(month=5, count=80, result=263), Reducing.Item(month=6, count=51, result=314), Reducing.Item(month=7, count=2, result=316), Reducing.Item(month=8, count=23, result=339), Reducing.Item(month=9, count=36, result=375), Reducing.Item(month=10, count=60, result=435), Reducing.Item(month=11, count=60, result=495), Reducing.Item(month=12, count=16, result=511)]