4

I have two ArrayList as :

ArrayOne

ClassPojo [Name ="XXX", FilterID = 4]
ClassPojo [Name = "ZZZ", FilterID = 5]

ArrayTwo

ClassPojo [Name = "XXX", FilterID = 4]

but when try to use:

 Arrays.asList(ArrayOne).contains(ArrayTwo )

or

ArrayOne().containsAll(ArrayTwo)

it returns false, i know the comparing is based on Refrences not Values equlaty but how i can do it by values note that: i want to use conatinsAll , so if the content of arrayTwo is found in some part of ArrayOne return true .

Omore
  • 614
  • 6
  • 18
  • you are using very different method signatures. contains: https://docs.oracle.com/javase/8/docs/api/java/util/List.html#contains-java.lang.Object- Is for a ClassPojo element, not the List. containsAll -https://docs.oracle.com/javase/8/docs/api/java/util/List.html#containsAll-java.util.Collection - does take a list, but as mentioned in lots of comments below you need to implement equals method. – Bojan Petkovic Apr 10 '17 at 18:57

2 Answers2

7

i know the comparing is based on references not Values equality

No, it is based on value equality. You need to override equals() to properly compare 2 objects of your ClassPojo

RamPrakash
  • 1,687
  • 3
  • 20
  • 25
jmj
  • 237,923
  • 42
  • 401
  • 438
3

In order for the contains or containsall to work you need to override the equals() function in your class ClassPojo.

It could look like this :

@Override
public boolean equals(Object obj){
    if (obj.Name.equals(this.Name) && obj.FilterID == this.FilterID)
        return true;
    else
        return false;
}

(written using phone so be careful with just copy paste)

Niki van Stein
  • 10,564
  • 3
  • 29
  • 62