0

Hi guys i got 2 ArrayList based on same class. I have to add from arraylist1 to arraylist2 non exist(s)ing rows. I tried to use contains but its always returning false. What i m doing wrong ? Ty

MyClass

public class HataKoduBean {
private String Oid;
private String Name;
private String Surname; 

}

How i define Arraylists

Arraylist<MyClass> array1 = new Arraylist<>(); Arraylist<MyClass> array2 = new Arraylist<>();

How i tried to compare ?

 for (int ii = 0; ii < array1.size(); ii++) {
                            if (!array2.contains(array1.get(ii)))
                                array2.add(array1.get(ii));  
                        }
1
  • show the equals() and hashcode() implementations for the HataKoduBean class. Commented Apr 26, 2016 at 13:04

2 Answers 2

2

First you have to implement equals() and hashCode() in your custom class.

Consider to use Sets, so you can merge collections in one line with addAll().

Sign up to request clarification or add additional context in comments.

Comments

0

First you have to implement equals() and hashCode() in your custom object and create loop and compare between object by using equals method like this.

implement equals and hashCode:

`@Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass()) 
           return false;
        HataKoduBean o = (HataKoduBean) o;
        return Oid != null?Oid.equals(HataKoduBean.Oid):HataKoduBean.Oid== null;
    }

    @Override
    public int hashCode() {
        return Oid != null ? Oid.hashCode() : 0;
    }`

compare Objects:

for (int ii = 0; ii < array1.size(); ii++) { if (!array2.get(ii).equals(array1.get(ii))) array2.add(array1.get(ii)); }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.