如何使用结转前一个对象计算值转换地图对象

我有一堂这样的课

class Transaction{
    long id;
    String type;
    long quantity;
    double amount;
}

我想将其转换为如下所示

class TransactionDTO{
    long id;
    String type;
    long quantity;
    double amount;
    double total;
}

假设输入如下

Transaction( id=1,type=buy,quantity=5,amount=5)
Transaction( id=1,quantity=2,amount=3)
Transaction( id=1,quantity=3,amount=6)

应该输出如下(quantity*amount adds when buy subtract when sell with previous amount)

Transaction( id=1,amount=5,total=25)
Transaction( id=1,type=sell,amount=3,total=19)
Transaction( id=1,amount=6,total=37)

我可以使用 for 循环轻松完成此操作。但我想知道这是否可以通过 lambda 完成。

特别希望根据 Transaction type 使用先前计算的总数和下一个元素。

这能做到吗?如果是,那么如何?

wangyande108 回答:如何使用结转前一个对象计算值转换地图对象

你可以这样做:

public List<TransactionDTO> getTransaction() {
    double[] temporaryTotal = new double[1];
    List<Transaction> transactionList = Arrays.asList(
            new Transaction(1,"buy",5,5),new Transaction(1,"sell",2,3),3,6)
    );

    return transactionList.stream()
            .map(
                    transaction -> new TransactionDTO(
                            transaction.getId(),transaction.getType(),transaction.quantity,transaction.getAmount(),calculate(transaction,temporaryTotal[0])
                    )
            )
            .peek(transaction -> temporaryTotal[0] = transaction.getTotal())
            .collect(Collectors.toList());
}

private double calculate(final Transaction transaction,final double total){
    if (transaction.getType().equals("buy")){
        return total + (transaction.getQuantity() * transaction.getAmount());
    }

    return total - (transaction.getQuantity() * transaction.getAmount());
}

由于变量 total 不是 final 或有效 final,我们可以使用带有一个名为 temporaryTotal 的元素的数组。

输出为:

[TransactionDTO(id=1,type=buy,quantity=5,amount=5.0,total=25.0),TransactionDTO(id=1,type=sell,quantity=2,amount=3.0,total=19.0),quantity=3,amount=6.0,total=37.0)]
,

似乎 ('ret','AAPL') 的流应该按 public A(Object ... args) { } List<Object> objects = new ArrayList<>(); objects.add(new Object()); objects.add(new Object()); A a = new A(objects); 分组,Transaction 应该提供合并功能来根据 {{1} }:

id

然后可以使用 TransactionDTO 检索 type 的列表,然后转换地图值:

class TransactionDTO {
    long id;
    String type;
    long quantity;
    double amount;
    double total;

public TransactionDTO (Transaction t) {
        this.id = t.id;
        this.type = t.type;

        int sign = "buy".equals(type) ? 1 : "sell".equals(type) ? -1 : 0;
        
        this.amount = sign * t.amount;
        this.quantity = sign * t.quantity;
        this.total = sign * t.amount * t.quantity;
    }

    public TransactionDTO merge(TransactionDTO t) {
        this.type = t.type; // store last type
        
        int sign = "buy".equals(type) ? 1 : "sell".equals(type) ? -1 : 0;
        
        this.amount += t.amount;
        this.quantity += t.quantity;
        this.total += sign * t.amount * t.quantity;

        return this;
    }
}

输出:

TransactionDTO
本文链接:https://www.f2er.com/40543.html

大家都在问