Home > Blockchain >  Update existing value in an element, Adding new element in array
Update existing value in an element, Adding new element in array

Time:11-09

I am comparing 2 Lists. If both list exists in one of the element, it updates the element values. If it is not found, then it will add into the array. But it seems that it adds every time it loops in even with conditions. It seems like i need to add/update the arrayList only after the 2nd loop ends. Not sure if there are any possibility in doing that

Below are my codes:

for(var bscsEntry : bscsChargeTypemap.entrySet()) {
    Report report = new Report();
    for(int i = 0; i < reportDataList.size(); i  ) {
        if(bscsEntry.getValue().getChargeTypeName().equalsIgnoreCase(reportDataList.get(i).getChargeTypeName())) {
            reportDataList.get(i).setBscsAmount(bscsEntry.getValue().getAmount());
            reportDataList.get(i).setBscsQuantity(bscsEntry.getValue().getQuantity());
        }else {
            report.setChargeTypeName(bscsEntry.getValue().getChargeTypeName());
            report.setBscsAmount(bscsEntry.getValue().getAmount());
            report.setBscsAmount(bscsEntry.getValue().getQuantity());
            reportDataList.add(report);
        }
    }
}

Example of the lists:

Existing Report Data List

Type : [Name : TypeA, Amount : 10.0 , Quantity : 1]
Type : [Name : TypeB, Amount : 5.0 , Quantity : 2]
Type : [Name : TypeC, Amount : 55.0 , Quantity : 1]
Type : [Name : TypeD, Amount : 10.0 , Quantity : 2]

Map Entry Sets

Type : [Name : TypeA, Amount : 10.0 , Quantity : 1]
Type : [Name : TypeB, Amount : 5.0 , Quantity : 2]
Type : [Name : TypeE, Amount : 65.0 , Quantity : 1]
Type : [Name : TypeF, Amount : 100.0 , Quantity : 2]

Expected outcome of existing Report Data List

Type : [Name : TypeA, Amount : 20.0 , Quantity : 2]
Type : [Name : TypeB, Amount : 10.0 , Quantity : 4]
Type : [Name : TypeC, Amount : 55.0 , Quantity : 1]
Type : [Name : TypeD, Amount : 10.0 , Quantity : 2]
Type : [Name : TypeE, Amount : 65.0 , Quantity : 1]
Type : [Name : TypeF, Amount : 100.0 , Quantity : 2]

Updated POJO:

public class ChargeType {

    private String chargeTypeName;
    private Double amount;
    private Integer quantity;
    
    public ChargeType() {
        chargeTypeName = "";
        amount = Double.parseDouble("0");
        quantity = 0;
    }
    
    public ChargeType(String chargeTypeName, Double amount, Integer quantity) {
        this.chargeTypeName = chargeTypeName;
        this.amount = amount;
        this.quantity = quantity;
    }

    public String getChargeTypeName() {
        return chargeTypeName;
    }

    public void setChargeTypeName(String chargeTypeName) {
        this.chargeTypeName = chargeTypeName;
    }

    public Double getAmount() {
        return amount;
    }

    public void setAmount(Double amount) {
        this.amount = amount;
    }

    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }
}



public class Report {
    private String chargeTypeName;
    private Double brmAmount;
    private Integer brmQuantity;
    private Double bscsAmount;
    private Integer bscsQuantity;
    private String remarks;
    
    public Report() {
        chargeTypeName = "";
        brmAmount = Double.parseDouble("0");
        brmQuantity = 0;
        bscsAmount = Double.parseDouble("0");
        bscsQuantity = 0;
        remarks = "";
    }

    public String getChargeTypeName() {
        return chargeTypeName;
    }

    public void setChargeTypeName(String chargeTypeName) {
        this.chargeTypeName = chargeTypeName;
    }

    public Double getBrmAmount() {
        return brmAmount;
    }

    public void setBrmAmount(Double brmAmount) {
        this.brmAmount = brmAmount;
    }

    public Integer getBrmQuantity() {
        return brmQuantity;
    }

    public void setBrmQuantity(Integer brmQuantity) {
        this.brmQuantity = brmQuantity;
    }

    public Double getBscsAmount() {
        return bscsAmount;
    }

    public void setBscsAmount(Double bscsAmount) {
        this.bscsAmount = bscsAmount;
    }

    public Integer getBscsQuantity() {
        return bscsQuantity;
    }

    public void setBscsQuantity(Integer bscsQuantity) {
        this.bscsQuantity = bscsQuantity;
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }
    
}

CodePudding user response:

A simple solution would be to create a new list and merge the two lists after iteration, something like this

List<Report> additional = new ArrayList<>();
for(var bscsEntry : bscsChargeTypemap.entrySet()) {
    Report report = new Report();
    for(int i = 0; i < reportDataList.size(); i  ) {
        if(bscsEntry.getValue().getChargeTypeName().equalsIgnoreCase(reportDataList.get(i).getChargeTypeName())) {
            reportDataList.get(i).setBscsAmount(bscsEntry.getValue().getAmount());
            reportDataList.get(i).setBscsQuantity(bscsEntry.getValue().getQuantity());
        }else {
            report.setChargeTypeName(bscsEntry.getValue().getChargeTypeName());
            report.setBscsAmount(bscsEntry.getValue().getAmount());
            report.setBscsAmount(bscsEntry.getValue().getQuantity());
            additional.add(report);
        }
    }
}
reportDataList.addAll(additional);

CodePudding user response:

I think i found a solution. This is how i write the logic.

The code to check if it exists in the List:

Boolean isExist = reportDataList.stream().anyMatch(i -> i.getChargeTypeName().equalsIgnoreCase(bscsEntry.getValue().getChargeTypeName()));

The code to find index if exists:

int dataIndex = IntStream.range(0, reportDataList.size())
                            .filter(i -> reportDataList.get(i).getChargeTypeName().equalsIgnoreCase(bscsEntry.getValue().getChargeTypeName()))
                            .findFirst().getAsInt();

This way, i dont have to do nested loop. Thank for the help everyone

  • Related