Paris Karagiannopoulos
Paris Karagiannopoulos

Reputation: 591

Overriding hashcode and equals for custom class

I have created a class with 2 fields. A Double and a Line2D. I want to override the equals method so that the following code return true

public class Main {

    public static void main(String[] args) {
        StatusLinePair slp1 = new StatusLinePair(25.0, new Line2D.Double(123.0, 32.0, 342.0, 54.0));  
        StatusLinePair slp2 = new StatusLinePair(25.0, new Line2D.Double(123.0, 32.0, 342.0, 54.0));

        System.out.println(slp1.equals(slp2));
    }

}

This is what I've tried but I am still not getting the desired results

public class StatusLinePair {
    public Double yAxisOrder;
    public Line2D line;

    public StatusLinePair(Double yAxisOrder, Line2D line) {
        this.yAxisOrder = yAxisOrder;
        this.line = line;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((line == null) ? 0 : line.hashCode());
        result = prime * result + ((yAxisOrder == null) ? 0 : yAxisOrder.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        StatusLinePair other = (StatusLinePair) obj;

        if (this.yAxisOrder == other.yAxisOrder && this.line.getX1() == other.line.getX1()
                && this.line.getX2() == other.line.getX2() && this.line.getY1() == other.line.getY1()
                && this.line.getY2() == other.line.getY2())
            return true;        

        return false;
    }
}

Please help me. Thanks in advance!

Upvotes: 0

Views: 262

Answers (4)

Sumanth Jois
Sumanth Jois

Reputation: 3234

By using ==, you are not comparing the actual value of the Object but the reference.

For example:
     Object a = new Object();
     Object b = a;              // if you compare these two objects with == it will return true

But
    Object a =new Object();
    Object b = new Object();    // if you compare these two objects with == then it will return false as they are pointing to two different objects

By overriding equals() you can compare two objects based on their values. Check this link out for more on equals.

Upvotes: 2

Yaron V.
Yaron V.

Reputation: 29

Look Here:

Guidelines for Overloading Equals() and Operator == : https://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx

AND

the Guidelines for Overloading GetHashCode: https://msdn.microsoft.com/en-us/library/system.object.gethashcode(v=vs.80).aspx

it's in C# but is almost the same!

Upvotes: -1

JB Nizet
JB Nizet

Reputation: 691685

There are several problems with your code:

  • it should return false if the other object is not a StatusLinePair, instead of throwing a ClassCastException
  • it should return false if the other object is null, instead of throwing a NullPointerException
  • it should not compare Double instances with ==, but with equals()(or the class should contain a double rather than a Double, since it doesn't seem to be nullable): that's the cause of your specific problem.
  • it should not use Line2D.hashCode(), since it doesn't use Line2D.equals()

Upvotes: 3

Peter Lawrey
Peter Lawrey

Reputation: 533492

You are not using Line2D.equals, so I assume this is not implemented the way you need, or you would use it. If this is the case, you shouldn't be using Line2D.hashCode() either.

In short, either use Line2D.hashCode() and Line2D.equals(), or not, don't use a mix.

Upvotes: 2

Related Questions