1

I have a list in that list I created an object. By using the contains() method, I want to check whether the object already exists or not. For that, I override the equals() method. Everything is perfect upto this. But when I try to do the same thing for String and int the equals() override doesn't not work. Why is it like this? I just posted some sample code for reference.

public class Test 
{
private int x;

public Test(int n) 
{ 
x = n;
}

public boolean equals(Object o) 
{
return false; 
}

public static void main(String[] args) 
{
List<Test> list = new ArrayList<Test>();
list.add(new Test(3));
System.out.println("Test Contains Object : " + list.contains(new Test(3))); // Prints always false (Equals override)
List<String> list1 = new ArrayList<String>();
list1.add("Testing");
String a = "Testing";
System.out.println("List1 Contains String : " + list1.contains(a)); // Prints true (Equals override not working)
}
}
Lion
  • 18,729
  • 22
  • 80
  • 110
Miko
  • 2,615
  • 9
  • 33
  • 58
  • You're not overriding the equals method for the String object. You're overriding equals explicitly for the Test object. – The Real Baumann Jan 27 '12 at 18:08
  • I don't understand the question. You have a list of strings, the list contains the string you added, and `contains` indicates as much. You can't do the same thing for `String`, and it wouldn't compile if you tried to extend String. – Dave Newton Jan 27 '12 at 18:09

3 Answers3

11

String and Integer are both final classes, so you cannot subclass them. Therefore you cannot override their equals methods.

You can, however, subclass ArrayList and create your own contains implementation builds on the existing one.

Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83
  • Even i implement a contains method i can't override the String and Integer then what's the need to Implement them?? – Miko Jan 27 '12 at 18:25
3

There is no need for overriding the equals method of Integer or String as they are already implemented and work well.

However, if you want to do it anyways, this would be one way of doing it (Delegation Pattern):

public class MyString {
    private String myString;

    @Override
    public boolean equals(Object o) 
        return false;
    }

    // add getter and setter for myString 
    // or delegate needed methods to myString object.
}

Of course, then you must be using this class, not the String class in your lists.

tim
  • 1,999
  • 17
  • 32
0

Regarding Tim's answer you can do something like this:

import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone{
    public static void main (String[] args) throws java.lang.Exception
    {
        MyString my = new MyString();
        String testString = "bb";
        my.setMyString(testString);
        System.out.println(my.equals(testString));
    }
}

class MyString {
    private String myString;

    @Override
    public boolean equals(Object o){ 
        return o.equals(myString);
    }

    public String getMyString(){
        return myString;
    }

    public void setMyString(String newString){
        myString = newString;
    }
}

The output is true.

user229044
  • 232,980
  • 40
  • 330
  • 338
tryingHard
  • 1,794
  • 4
  • 35
  • 74