0

I've a doubt about how ArrayList contains method works. Let's take an example:

List<String> lstStr = new ArrayList<String>();
String tempStr1 = new String("1");
String tempStr2 = new String("1");

lstStr.add(tempStr1);

if (lst.contains(tempStr2))
    System.out.println("contains");
else
    System.out.println("not contains");

it returns 'not contains'.

Another example:

List<LinkProfileGeo> lst = new ArrayList<LinkProfileGeo>();
LinkProfileGeo temp1 = new LinkProfileGeo();
temp1.setGeoCode("1");
LinkProfileGeo temp2 = new LinkProfileGeo();
temp2.setGeoCode("1");

lst.add(temp1);

if (lst.contains(temp2))
    System.out.println("contains");
else
   System.out.println("not contains");

It returns contains. So how does contains method works ?

Thanks

Sachin Thapa
  • 3,559
  • 4
  • 24
  • 42
AlessioG
  • 576
  • 5
  • 13
  • 32
  • 6
    If you fix your example to call `lstStr.contains`, it *does* work. (There's no variable called `lst`.) I suggest you delete this question, and edit it then undelete it when you've got a *real* short but complete program demonstrating the problem. – Jon Skeet Sep 20 '13 at 15:37
  • 1
    the second part is a duplicate of: http://stackoverflow.com/questions/2642589/how-does-a-java-arraylist-contains-method-evaluate-objects – Derek Sep 20 '13 at 15:39
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – jb. Sep 20 '13 at 18:12

4 Answers4

4

You are adding your string to the list lstStr

lstStr.add(tempStr1);

but you are using contains method on lst

if (lst.contains(tempStr2))

Your idea of testing is correct, as contains internally uses equals to find the element, so if the string is matched using equals then it should return true. But it seems you are using two different lists, one for adding and another one for checking contains.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
2

Here is the relevant source code from ArrayList if you're interested. As @user2777005 noted, you had a typo in your code. You should use lstStr.contains(), NOT lst.contains().

     public int indexOf(Object o) {
        if (o==null) {
            for (int i=0; i<a.length; i++)
                if (a[i]==null)
                    return i;
        } else {
            for (int i=0; i<a.length; i++)
                if (o.equals(a[i]))
                    return i;
        }
        return -1;
    }

    public boolean contains(Object o) {
        return indexOf(o) != -1;
    }
jtravaglini
  • 1,676
  • 11
  • 19
0

the second part is a duplicate of: How does a ArrayList's contains() method evaluate objects?

You need to override the equals method to make it work as you desire.

Community
  • 1
  • 1
Derek
  • 7,615
  • 5
  • 33
  • 58
0

in first section of code :

String tempStr1 = new String("1");
String tempStr2 = new String("1");

both tempStr1 and tempStr2 refer two different-2 object of string. after that String object that is refered by tempStr1 is added to the List by the codelstStr.add(tempStr1); .so the List have only one String object that is reffered by tempStr1 not tempStr2.but contains(); method work on equals() method.that is lstStr.contains(temp2); return true if content of String object which is refered by temp2 is same as the one of content of String object which is added to the List and return false when matching not found.here lstStr.contains(temp2);return true because content of String object temp2 is equal to content of String object temp1 which is added to List.but in your code insteed of lstStr.contains(temp2); it is mentioned as:

lst.contains(temp2);

Here you are using different List reference variable (lst) instead of (lstStr).thats why it return false and executed else part.

in 2nd section of code setGeoCode() is not defined.

Mukund Kumar
  • 21,413
  • 18
  • 59
  • 79