How does contains() method work in evaluating objects?

0 votes

Say I create one object and add it to my ArrayList. If I then create another object with exactly the same constructor input, will the contains() method evaluate the two objects to be the same? Assume the constructor doesn't do anything funny with the input, and the variables stored in both objects are identical.

ArrayList<Thing> basket = new ArrayList<Thing>();  
Thing thing = new Thing(100);  
basket.add(thing);  
Thing another = new Thing(100);  
basket.contains(another); // true or false?

class Thing {  
    public int value;  

    public Thing (int x) {
        value = x;
    }

    equals (Thing x) {
        if (x.value == value) return true;
        return false;
    }
}

Is this how the class should be implemented to have contains() return true?

May 25, 2018 in Java by developer_1
• 3,280 points
19 views

1 answer to this question.

0 votes

Generally you should also override hashCode() each time you override equals(), even if just for the performance boost. HashCode() decides which 'bucket' your object gets sorted into when doing a comparison, so any two objects which equal() evaluates to true should return the same hashCode value(). I cannot remember the default behavior of hashCode() (if it returns 0 then your code should work but slowly, but if it returns the address then your code will fail). I do remember a bunch of times when my code failed because I forgot to override hashCode() though. :)

answered May 25, 2018 by Rishabh
• 3,540 points

Related Questions In Java

0 votes
2 answers

How does the “final” keyword in Java work?

A final variable that is created inside ...READ MORE

answered Mar 23 in Java by anonymous
28 views
0 votes
1 answer

How can I invoke a method when the method name is in the form of a given string?

I tried doing it this way - java.lang.reflect.Method ...READ MORE

answered Apr 27, 2018 in Java by developer_1
• 3,280 points
42 views
0 votes
1 answer

How does omitting curly braces in Java program, effect the code ?

Using braces makes the code more maintainable ...READ MORE

answered May 14, 2018 in Java by Rishabh
• 3,540 points
33 views
0 votes
1 answer

How to calculate method execution time in Java ?

Use the following code : new Timer(""){{ ...READ MORE

answered May 24, 2018 in Java by Rishabh
• 3,540 points
86 views
0 votes
2 answers

One line initialization of an ArrayList object in Java

In Java 8 or earlier: List<String> string = ...READ MORE

answered Jul 26, 2018 in Java by samarth295
• 2,190 points
109 views
0 votes
3 answers

Check if a String is numeric in Java

Java 8 Lambda Expression is used: String someString ...READ MORE

answered Sep 3, 2018 in Java by Daisy
• 8,040 points
558 views
0 votes
2 answers

Ho do I Iterate through a HashMap which contains duplicate values

for (Map.Entry<String, String> item : params.entrySet()) { ...READ MORE

answered Jul 24, 2018 in Java by samarth295
• 2,190 points
111 views
0 votes
2 answers

Performing Iteration over each entry in a 'Map'

In Java 8 you can do it ...READ MORE

answered Oct 24, 2018 in Java by Sushmita
• 6,840 points
38 views
0 votes
2 answers

What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,040 points
307 views
0 votes
2 answers

How does '&&' (AND) and '||' (OR) statements work inside an IF condition ?

Short circuit here means that the second ...READ MORE

answered Sep 19, 2018 in Java by Sushmita
• 6,840 points
24 views