使用流基于另一个字段计算属性

时间:2018-12-10 17:39:39

标签: java lambda java-8 java-stream

我下面有要转换为流的代码,在parallelStream或流中编写这种复杂的逻辑可能没有意义,但仍然想了解我们如何使用lambda表达式编写此代码。

for(PositionModel position :positionList)
    {           
        StringBuffer line = new StringBuffer();
        line.append(position.getInstrument());
        line.append(CSV_SEPARATOR);
        line.append(position.getAccount());
        line.append(CSV_SEPARATOR);
        line.append(position.getAccountType());
        line.append(CSV_SEPARATOR);

        List<TransactionModel> tranList = transactionMap.get(position.getInstrument());
        int delta = 0;
        if(tranList != null )
        {

            tranList.parallelStream().collect(Collectors.gr)
            for(TransactionModel tran: tranList)
            {
                if(PositionCalcContant.BUY_INDICATOR.equals(tran.getTransactionType()))
                {
                    if(PositionCalcContant.EXTERNAL.equals(position.getAccountType()))
                    {
                        delta += tran.getTransactionQuantity();
                    }
                    else
                    {
                        delta -= tran.getTransactionQuantity();
                    }                       
                }
                if(PositionCalcContant.SELL_INDICATOR.equals(tran.getTransactionType()))
                {
                    if(PositionCalcContant.EXTERNAL.equals(position.getAccountType()))
                    {
                        delta -= tran.getTransactionQuantity();
                    }
                    else
                    {
                        delta += tran.getTransactionQuantity();
                    }

                }
            }
        }
        line.append(position.getQuantity()+delta);
        line.append(CSV_SEPARATOR);
        line.append(delta);
        bw.write(line.toString());
        bw.newLine();

        position.setDelta(delta);

        if(Math.abs(delta) > maxChange)
        {
            maxChange = delta;
            maxChangeInstrument = position;
            maxChangeInstrument.setDelta(delta);
        }
        if(Math.abs(delta) < minChange)
        {
            minChange = delta;
            minChangeInstrument = position;
            minChangeInstrument.setDelta(delta);
        }
    }

这里有2个循环,我想将其都转换为lambda或将内部循环转换为lambda

1 个答案:

答案 0 :(得分:0)

我正在尝试一些问题,并在下面提出,无论如何,这比原始代码更糟糕/更好。

delta = tranList.parallelStream().collect
                    (Collectors.summingInt(model -> getDelta(model, position.getAccountType()))).intValue();

private int getDelta(TransactionModel tran, String accountType) {

    int delta = 0;
    if(PositionCalcContant.BUY_INDICATOR.equals(tran.getTransactionType()))
    {
        if(PositionCalcContant.EXTERNAL.equals(accountType))
        {
            delta += tran.getTransactionQuantity();
        }
        else
        {
            delta -= tran.getTransactionQuantity();
        }                       
    }
    if(PositionCalcContant.SELL_INDICATOR.equals(tran.getTransactionType()))
    {
        if(PositionCalcContant.EXTERNAL.equals(accountType))
        {
            delta -= tran.getTransactionQuantity();
        }
        else
        {
            delta += tran.getTransactionQuantity();
        }
    }
    return delta;
}

此外,这仅适用于我们正在计算增量的内循环,就整体而言,即使我同意,这样做也会过于复杂,可能不值得。