如何只用一个循环比较两个List中的对象?

时间:2019-05-24 16:26:53

标签: java loops compare

我有两个具有相同类型(事务)的对象列表,我需要对其进行比较。 我的目标是首先比较“ id”,其次,如果匹配,则检查是否匹配“ amount”。

如何在不使用双循环的情况下做到这一点?

列表如下:

列表1-[交易{id = 123,金额= 100.05},交易{id = 124,金额= 150.75},交易{id = 125,金额= 1010.0},交易{id = 126,金额= 15.5} ]

列表2-[交易{id = 123,金额= 94.7},交易{id = 124,金额= 150.75},交易{id = 125,金额= 1020.2},交易{id = 126,金额= 15.5} ,交易{id = 127,金额= 120.74]

public class Transaction {

    private Integer id;

    private Double amount;

    public Transaction(Integer id, Double amount) {
        this.id = id;
        this.amount = amount;
    }

    public Integer getId() {
        return id;
    }
    public String getData() {
        return data;
    }
  }

试过这样的东西:

        for (Transaction transactionSql : service.findAll()) {
        for (Transaction transactionTxt : scraper.read()) {
            if(transactionSql.getId().equals(transactionTxt.getId()) {
                // do smth
            }

        }
    }

0 个答案:

没有答案