0

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!

  • what is your question? – Andrew Tobilko Jun 26 '16 at 12:16
  • What is the problem? You didn't ask any question. I suggest reading the following threads and asking again if you run into problems: see [here](http://stackoverflow.com/questions/2265503/why-do-i-need-to-override-the-equals-and-hashcode-methods-in-java) and [here](http://stackoverflow.com/questions/2265503/why-do-i-need-to-override-the-equals-and-hashcode-methods-in-java). – RK1 Jun 26 '16 at 12:18

4 Answers4

3

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()
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
2

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Sumanth Jois
  • 3,146
  • 4
  • 27
  • 42
-1

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!

Yaron V.
  • 29
  • 3
  • That doesn't answer the question, and is irrelevant since the question is about Java, not C#. And BTW, your second link is broken. – JB Nizet Jun 26 '16 at 14:15