Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (71.8m points)

java List 相邻两个数据累加,可以用stream的collectors.reducing实现么

a0= a0,
a1= a0 + a1,
a2= a1 + a2...

有位兄台提醒我了, 我把业务说一下.
场景是我需要给echart的折线图赋数据.
LineDto是一个有两个字段的Dto.
Date(月份),
Count(值).
每月(Date)显示的是当月的数值(Count).

现在是需求变了, 每月显示的是累加的数值, 就是当月之前的数值也累加到当前Count上.

所以我想到的是先查询出来后,
1月, a0 = 1月的Count,
2月, a1 = a0 + 2月的Count,
3月, a2 = a1 + a2,
....

我是用for循环实现的, 但是我强迫症总想用stream的相关api来实现, 所以问问各位兄台有没有例子我学习一下.

if(CollectionUtils.isNotEmpty(result)){
 LineDto dto = new LineDto();
 dto.setGemBoardCount(0);
 
 for (LineDto i : result) {
    i.setGemBoardCount(i.getGemBoardCount() + dto.getGemBoardCount());
    dto = i;
 }
}

我想问下, 可以用stream的collectors.reducing实现么, 谢谢


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

这种情况下其实不太适合用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)]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...