Home > Software engineering >  removing duplicates in arraylist java
removing duplicates in arraylist java

Time:01-31

I need to write a logic to check if a list has any duplicates and if it does it removes them and returns a new list with the items that weren't duplicated/removed.Below is the way i tried but didn't work.

CustomerResponse response = service.callGetDetails(id,name);
List <Customer> responseList = response.getCustomerList();
CustomerResponse response1 = service.callGetDetails(id,name);
responseList.addAll(response1.getCustomerList());

Customer.java:

public class Customer{

@JsonProperty("id")
private String id;

@JsonProperty("startDate")
private String startDate;

@JsonProperty("abcAccount")
private AbcAccount abcAccount;

@JsonProperty("accPaList")
private List<AccPaList> accPaList;

@JsonProperty("type")
private String  type;

//Added getter and setter

@Override
public int hashCode(){
    final int prime=31;
    int result = 1;
    result = prime * result   ((abcAccount ==null)? 0 : abcAccount.hashCode());
    result = prime * result   ((accPaList ==null)? 0 : accPaList.hashCode());
    result = prime * result   ((id ==null)? 0 : id.hashCode());
    result = prime * result   ((startDate ==null)? 0 : startDate.hashCode());
    result = prime * result   ((type ==null)? 0 : type.hashCode());
    return result;
}

@Override
public boolean equals(Object obj){
    if(this == obj)
        return true;
    if(obj == null)
        return false;
    if(getClass() != obj.getClass())
        return false;
    Customer other = (Customer) obj;
    if(abcAccount ==null){
        if(other.abcAccount !=null)
            return false;
    } else if (!abcAccount.equals(other.abcAccount))
        return false;
    if(accPaList ==null){
        if(other.accPaList !=null)
            return false;
    } else if (!accPaList.equals(other.accPaList))
        return false;
    if(id ==null){
        if(other.id !=null)
            return false;
    } else if (!id.equals(other.id))
        return false;
    if(startDate ==null){
        if(other.startDate !=null)
            return false;
    } else if (!startDate.equals(other.startDate))
        return false;
    if(type ==null){
        if(other.type !=null)
            return false;
    } else if (!type.equals(other.type))
        return false;
    return true;
}
}

AbcAccount.java:

public class AbcAccount extends GCAcc{
}

HeadAcc.java:

public class HeadAcc extends GCAcc{
}

SouAcc.java:

public class SouAcc extends GCAcc{
}

AccPaList.java:

public class AccPaList{

@JsonProperty("headAcc")
private HeadAcc headAcc;
@JsonProperty("souAcc")
private SouAcc souAcc;
@JsonProperty("accPaDetails")
private ConcId concId;

@Override
public int hashCode(){
    final int prime=31;
    int result = 1;
    result = prime * result   ((headAcc ==null)? 0 : headAcc.hashCode());
    result = prime * result   ((souAcc ==null)? 0 : souAcc.hashCode());
    result = prime * result   ((concId ==null)? 0 : concId.hashCode());
    
    return result;
}

@Override
public boolean equals(Object obj){
    if(this == obj)
        return true;
    if(obj == null)
        return false;
    if(getClass() != obj.getClass())
        return false;
    AccPaList other = (AccPaList) obj;
    if(headAcc ==null){
        if(other.headAcc !=null)
            return false;
    } else if (!headAcc.equals(other.headAcc))
        return false;
    if(souAcc ==null){
        if(other.souAcc !=null)
            return false;
    } else if (!souAcc.equals(other.souAcc))
        return false;
    if(concId ==null){
        if(other.concId !=null)
            return false;
    } else if (!concId.equals(other.concId))
        return false;
    return true;
}

}

ConcId.java:

public class ConcId{

@JsonProperty("id")
private String id;
@JsonProperty("version")
private String version;

@Override
public int hashCode(){
    final int prime=31;
    int result = 1;
    result = prime * result   ((id ==null)? 0 : id.hashCode());
    result = prime * result   ((version ==null)? 0 : version.hashCode());
    return result;
}

@Override
public boolean equals(Object obj){
    if(this == obj)
        return true;
    if(obj == null)
        return false;
    if(getClass() != obj.getClass())
        return false;
    ConcId other = (ConcId) obj;
    if(id ==null){
        if(other.id !=null)
            return false;
    } else if (!id.equals(other.id))
        return false;
    if(version ==null){
        if(other.version !=null)
            return false;
    } else if (!version.equals(other.version))
        return false;
    return true;
}

}

GCAcc.java:


public class GCAcc{

@JsonProperty("act")
private String act;
@JsonProperty("acctCurrency")
private String acctCurrency;


@Override
public int hashCode(){
    final int prime=31;
    int result = 1;
    result = prime * result   ((act ==null)? 0 : act.hashCode());
    result = prime * result   ((acctCurrency ==null)? 0 : acctCurrency.hashCode());
    return result;
}

@Override
public boolean equals(Object obj){
    if(this == obj)
        return true;
    if(obj == null)
        return false;
    if(getClass() != obj.getClass())
        return false;
    GCAcc other = (GCAcc) obj;
    if(act ==null){
        if(other.act !=null)
            return false;
    } else if (!act.equals(other.act))
        return false;
    if(acctCurrency ==null){
        if(other.acctCurrency !=null)
            return false;
    } else if (!acctCurrency.equals(other.acctCurrency))
        return false;
    return true;
}

}

Below is the responseList after adding, where duplicate object are in same order:

{
"customerList":[
        {
        "id":"123",
        "startDate":"2022-01-29",
        "abcAccount":{
            "acct":"123",
            "acctCurrency":"USD"
            },
            "accPaList":[
            
                {
                    "headAcc":{
                        "act":"67890",
                        "acctCurrency":"USD"
                    },
                    "souAcc":{
                        "act":"5678",
                        "acctCurrency":"USD"
                    },
                    "accPaDetails":{
                        "id":"C012",
                        "version":"12"
                    }   
                },
                {
                    "headAcc":{
                        "act":"000123",
                        "acctCurrency":"AUD"
                    },
                    "souAcc":{
                        "act":"5678",
                        "acctCurrency":"AUD"
                    },
                    "accPaDetails":{
                        "id":"C045",
                        "version":"12"
                    }
                }
            ],
            "type":"EDA"
        },
        {
        "id":"123",
        "startDate":"2022-01-29",
        "abcAccount":{
            "acct":"123",
            "acctCurrency":"USD"
            },
            "accPaList":[
                {
                    "headAcc":{
                        "act":"67890",
                        "acctCurrency":"USD"
                    },
                    "souAcc":{
                        "act":"5678",
                        "acctCurrency":"USD"
                    },
                    "accPaDetails":{
                        "id":"C012",
                        "version":"12"
                    }
                },
                {
                    "headAcc":{
                        "act":"000123",
                        "acctCurrency":"AUD"
                    },
                    "souAcc":{
                        "act":"5678",
                        "acctCurrency":"AUD"
                    },
                    "accPaDetails":{
                        "id":"C045",
                        "version":"12"
                    }
                }
            ],
            "type":"EDA"
        },
        {
        "id":"456",
        "startDate":"2022-01-29",
        "abcAccount":{
            "acct":"321",
            "acctCurrency":"CAD"
            },
            "accPaList":[
                {
                    "headAcc":{
                        "act":"76899",
                        "acctCurrency":"CAD"
                    },
                    "souAcc":{
                        "act":"8765",
                        "acctCurrency":"CAD"
                    },
                    "accPaDetails":{
                        "id":"C022",
                        "version":"15"
                    }
                }
            ],
            "type":"EDA"
        }
    ]
} 

where in above list two objects having same details,same order (i.e) ["id":123] is duplicate. I have added toString(), equals(), hashcode() in all objects and passing it to hashSet..Duplicates got removed after passing into Set.It works fine.

Where in same response list, i have another object and duplicate, in which data present inside the object are same, added it below, but order is different. duplicates not getting removed in this case. Below is the responseList after adding, where duplicate object are in different order:

{
"customerList":[
        {
        "id":"123",
        "startDate":"2022-01-29",
        "abcAccount":{
            "acct":"123",
            "acctCurrency":"USD"
            },
            "accPaList":[
            {
                    "headAcc":{
                        "act":"000123",
                        "acctCurrency":"AUD"
                    },
                    "souAcc":{
                        "act":"5678",
                        "acctCurrency":"AUD"
                    },
                    "accPaDetails":{
                        "id":"C045",
                        "version":"12"
                    }
                },
                {
                    "headAcc":{
                        "act":"67890",
                        "acctCurrency":"USD"
                    },
                    "souAcc":{
                        "act":"5678",
                        "acctCurrency":"USD"
                    },
                    "accPaDetails":{
                        "id":"C012",
                        "version":"12"
                    }   
                }
            ],
            "type":"EDA"
        },
        {
        "id":"123",
        "startDate":"2022-01-29",
        "abcAccount":{
            "acct":"123",
            "acctCurrency":"USD"
            },
            "accPaList":[
                {
                    "headAcc":{
                        "act":"67890",
                        "acctCurrency":"USD"
                    },
                    "souAcc":{
                        "act":"5678",
                        "acctCurrency":"USD"
                    },
                    "accPaDetails":{
                        "id":"C012",
                        "version":"12"
                    }
                },
                {
                    "headAcc":{
                        "act":"000123",
                        "acctCurrency":"AUD"
                    },
                    "souAcc":{
                        "act":"5678",
                        "acctCurrency":"AUD"
                    },
                    "accPaDetails":{
                        "id":"C045",
                        "version":"12"
                    }
                }
            ],
            "type":"EDA"
        },
        {
        "id":"456",
        "startDate":"2022-01-29",
        "abcAccount":{
            "acct":"321",
            "acctCurrency":"CAD"
            },
            "accPaList":[
                {
                    "headAcc":{
                        "act":"76899",
                        "acctCurrency":"CAD"
                    },
                    "souAcc":{
                        "act":"8765",
                        "acctCurrency":"CAD"
                    },
                    "accPaDetails":{
                        "id":"C022",
                        "version":"15"
                    }
                }
            ],
            "type":"EDA"
        }
    ]
} 
responseList= new ArrayList<>(new HashSet <Customer>(responseList));

Please help.I checked it in forum and tried many cases to have common logic which handle both the cases, nothing works.

CodePudding user response:

Your idea about passing it through a Set is correct as adding an object that is equal to one of the contained objects, results in no change. So the culprit is most likely your implementation of equals() method. Make sure that this method returns true for the objects that you define as equal and false otherwise. If that will be written correctly then your set will filter out all duplicates. Implementation of toString() method is not necessary though.

CodePudding user response:

Have you also implemented equals() and hashcode() for AbcAccount and AccPaList classes?

  •  Tags:  
  • Related