0

I read that .equals() compares the value(s) of objects whereas == compares the references (that is -- the memory location pointed to by the variable). See here: What is the difference between == vs equals() in Java?

But observe the following piece of code:

package main;

public class Playground {

    public static void main(String[] args) {
        Vertex v1 = new Vertex(1);
        Vertex v2 = new Vertex(1);

        if(v1==v2){
            System.out.println("1");
        }
        if(v1.equals(v2)){
            System.out.println("2");
        }
    }
}

class Vertex{
    public int id;

    public Vertex(int id){
        this.id = id;
    }
}

Output:
(Nothing)

Shouldn't it be printing 2?

Community
  • 1
  • 1
user2316667
  • 5,444
  • 13
  • 49
  • 71
  • 2
    You probably should pick a title that isn't going to be closed as a duplicate of another question. – Robert Harvey Jul 31 '13 at 17:02
  • I have a question: how does hashmap look up objects? Lets say I have a vertex with id=5. Can I create a new vertex with id=5 and pass that to the hashmap's get method? – user2316667 Jul 31 '13 at 17:04
  • @user2316667 it would be great if you first **read** about it and then **test** it. Then you will have your answer. – Luiggi Mendoza Jul 31 '13 at 17:06
  • Add a member function: `public boolean equals(Object obj){ return this.id == id; }` in `Vertex` class – Grijesh Chauhan Jul 31 '13 at 17:06
  • Thanks Luiggi. I was actually coding it now. But I'm sure we both are aware the difference between testing if one condition is true, and knowing theoretically how something is done. Does HashMap use .equals? I have no clue. It might work for the problem I described above and if it does, I might accidentally make a false assumption for something else. – user2316667 Jul 31 '13 at 17:09
  • **Missed the 5-minute mark. Disregard above** -> This post actually had sass to it. I legitimately appreciate all the help (Luiggi too). I tested it and it does not work. Here is my problem: I have a text file: 1 25\n 1 45\n 167\n 2 3\n 2 6... indicating a directed edge from 1 to 25, from 1 to 45 and so on. I can't create vertices as I go. So I pre-create them. But now I can't find the vertices in my map ;( – user2316667 Jul 31 '13 at 17:16

2 Answers2

6

You need to implement your own .equals() method for the Vertex class.

By default, you are using the Object.equals method. From the docs, this is what it does:

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

You can do something like this:

@Override
public boolean equals(Object obj) {
    if (obj == null) return false;
    if (obj.getClass() != getClass()) return false;
    Vertex other = (Vertex)obj;
    return (this.id == other.id);
}
jh314
  • 27,144
  • 16
  • 62
  • 82
3

You need to override the default implementation of equals(). The default implementation is Object#equals():

public boolean equals(Object obj) {
    return (this == obj);
}

The overridden version would be something like this:

@Override
public boolean equals(Object obj)
{
    if(obj == this) return true;
    if(obj == null) return false;
    if(obj.getClass() != getClass()) return false;
    return ((Vertex) obj).id == this.id;
}
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417