Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 基于当前元素的值更新列表中的下一个元素_Java_Java 8_Java Stream - Fatal编程技术网

Java 基于当前元素的值更新列表中的下一个元素

Java 基于当前元素的值更新列表中的下一个元素,java,java-8,java-stream,Java,Java 8,Java Stream,我有一个列表list需要使用stream在一次过程中更新相同的list 我们有两个数量字段,如果firstQty小于secondQty,则应在下一条记录中将剩余的设置为secondQty。只有当currentmount指标为真时,我们才进行此计算 输入: none [CT(currentMonth=tue, firstQty=600, secondQty=620,..), CT(currentMonth=false, firstQty=0, secondQty=0,..)

我有一个列表
list
需要使用stream在一次过程中更新相同的
list

我们有两个数量字段,如果
firstQty
小于
secondQty
,则应在下一条记录中将剩余的设置为
secondQty
。只有当
currentmount
指标为真时,我们才进行此计算

输入:

none
[CT(currentMonth=tue, firstQty=600, secondQty=620,..),           
 CT(currentMonth=false, firstQty=0, secondQty=0,..),        
 CT(currentMonth=false, firstQty=0, secondQty=0,..)]       
输出:

none
[CT(currentMonth=tue, firstQty=600, secondQty=620,..),           
 CT(currentMonth=false, firstQty=0, secondQty=20,..),        
 CT(currentMonth=false, firstQty=0, secondQty=0,..)]       
List lotsDetailsTpm=deals.stream()
.map(dcl->{
BigDecimal差异=BigDecimal.0;
if(dcl.getCurrentMonth()){
BigDecimal qtyFirst=deals.getFirstQty();
BigDecimal qtySecond=deals.getSecondQty();
BigDecimal diff=qtySecond.减法(qtyFirst);
dcl.qtySecond(qtySecond.sbtract(diff));
如果(差异比较到(BigDecimal.ZERO)>1){
//需要将差异更新到下一个元素
}
}
返回dcl;
}).collect(Collectors.toList());

这里的困难在于如何保持数量上的差异,并使用该值来更新下一个元素。

您可以使用其行为来处理两个后续值

  • U reduce(U标识、双功能累加器、二进制运算符组合器)
守则:

AList<CT> newList = list.stream().reduce(
    new ArrayList<>(),                             // ArrayList as identity, the storage
    (l, ct) -> {                                   // List and the next CT
        if (l.isEmpty()) {                         // ... if the list is empty, insert CT
            l.add(ct);
        } else if (ct.getCurrentMonth()) {         // .. or else do you calculation
            final CT other = l.get(l.size() - 1);
            final BigDecimal diff = other.getSecondQty().subtract(other.getFirstQty());
            if (diff.compareTo(BigDecimal.ZERO) > 0) {
                ct.setSecondQty(diff);
            }
            l.add(ct);                             // .. add the new item
        }
        return l;                                  // .. and return the whole list
    },
    (l, r) -> l                                    // Finally, the operator returns the list
);

可以将其行为与两个后续值一起使用

  • U reduce(U标识、双功能累加器、二进制运算符组合器)
守则:

AList<CT> newList = list.stream().reduce(
    new ArrayList<>(),                             // ArrayList as identity, the storage
    (l, ct) -> {                                   // List and the next CT
        if (l.isEmpty()) {                         // ... if the list is empty, insert CT
            l.add(ct);
        } else if (ct.getCurrentMonth()) {         // .. or else do you calculation
            final CT other = l.get(l.size() - 1);
            final BigDecimal diff = other.getSecondQty().subtract(other.getFirstQty());
            if (diff.compareTo(BigDecimal.ZERO) > 0) {
                ct.setSecondQty(diff);
            }
            l.add(ct);                             // .. add the new item
        }
        return l;                                  // .. and return the whole list
    },
    (l, r) -> l                                    // Finally, the operator returns the list
);

不要为此使用流。它们不适用于涉及元素之间此类操作的任务。请添加您当前的解决方案尝试。解决方案是使用
IntStream
并在集合中循环,就像您在更经典的“for循环”(类似
IntStream.range(0,collection.size())中所做的那样。forEach(i->\*在这里执行操作*\);
)。但是,我看不出这样做有什么意义:只需执行“for循环”,它会更容易阅读。@Abrikot:这只是一种滥用。我会坚持使用for循环。不要为此使用流。它们不适合涉及元素之间此类操作的任务。请添加您当前的解决方案尝试。解决方案是使用
IntStream
并像在中一样循环您的集合一个更经典的“for循环”(类似于
IntStream.range(0,collection.size()).forEach(i->\*在这里做你的操作*\);
)。但是,我看不出这样做有什么意义:只要做一个“for循环”,它就会更容易阅读。@Abrikot:这只是滥用了。我会坚持使用for循环。
AList<CT> newList = list.stream().reduce(
    new ArrayList<>(),                             // ArrayList as identity, the storage
    (l, ct) -> {                                   // List and the next CT
        if (l.isEmpty()) {                         // ... if the list is empty, insert CT
            l.add(ct);
        } else if (ct.getCurrentMonth()) {         // .. or else do you calculation
            final CT other = l.get(l.size() - 1);
            final BigDecimal diff = other.getSecondQty().subtract(other.getFirstQty());
            if (diff.compareTo(BigDecimal.ZERO) > 0) {
                ct.setSecondQty(diff);
            }
            l.add(ct);                             // .. add the new item
        }
        return l;                                  // .. and return the whole list
    },
    (l, r) -> l                                    // Finally, the operator returns the list
);
CT(currentMonth=false, firstQty=0, secondQty=20,..)