1

From what i know, Every equals object must have same hash code. However what if in equals method have multiple if that need to be followed ?

Location is an object, Junction is an object, length is an integer, offset is an integer, section is an object.

I already solved the one when atAJunction method, the hashcode i only used junction as the additional hashcode. And when Section is equal, its section's length is equal to the both location's offset. The hashcode that i used for this is only using section as the hashcode.

The main problem is when they have different section, but same offset and endPoint. its equal but the hashcode is different.

Is there anyone can help with my problem ? Thanks Before. :)

This is my Equals method :

@Override
public boolean equals(Object object) {
    if(object == null){
        return false;
    }
    if(!(object instanceof Location)){
        return false;
    }else{
        Location otherLocation = (Location) object;
        if(atAJunction() && otherLocation.atAJunction()){
            return this.endPoint.getJunction().equals(otherLocation.getEndPoint().getJunction());
        }else{
            // The Problem Here 
            if(this.endPoint.equals(otherLocation.endPoint)){
                return this.offset == otherLocation.getOffset();
            }else{
                return this.section.equals(otherLocation.getSection()) && 
                        this.section.getLength() == (this.offset + otherLocation.getOffset());
            }
        }
    }
}

And this is my Hash Code :

@Override
public int hashCode() {
    // creates a polynomial hash-code based on the fields of the class.
    final int prime = 13; // an odd base prime
    int result = 1; // the hash code under construction
    if(atAJunction()){
        result = prime * result + this.endPoint.getJunction().hashCode();
    }else{
        result = prime * result + this.section.hashCode();
    }
    return result;
}
Rifqi Ryan
  • 19
  • 1
  • Are you sure the equals method is correct? Remember that equality must be reflexive: if a.equals(b) and b.equals(c) then a.equals(c) - I don't see how you can guarantee that – Joni Apr 07 '16 at 09:18
  • As a sidenote, when an if statement contains only a return statement, do not use an "else" afterwards because it is redundant and adds alot of clutter to the code – niilzon Apr 07 '16 at 11:10

2 Answers2

0

Firstly what I dont understand is why you need this additional check

 return this.section.equals(otherLocation.getSection()) && 
                    **this.section.getLength() == (this.offset + otherLocation.getOffset());**

Ideally if sections are equal, then length and all should be already covered in it. Comparing internal value of one object with some derived value of other class is error prone, and should be avoided in methods like equals().

You may want to take a look at this book, http://www.amazon.com/Effective-Java-Edition-Joshua-Bloch/dp/0321356683. The auther here explains the relation in hash-code and equals in a decent way. This would be definitely helpful to you.

In your case, what I would suggest is, if you are not very clear about right way to generate hashcode() and equals(), try using codegenerators inbuilt in IDE's like Eclipse or netbeans.

Anand Vaidya
  • 1,374
  • 11
  • 26
  • thanks for your answer first of all, I use the additional check because the requirement wants me to do it. one of requirement, location to be equal is : the length of the section must be the same as the total of location offset + the otherlocation's offset (another object that to be compare to) – Rifqi Ryan Apr 07 '16 at 12:08
  • but in that case, this.offset + otherLocation.getOffset()); and this.section.hashCode(); are contradictory. hashcode is not considering this.offset + otherLocation.getOffset());, which is used by equals. – Anand Vaidya Apr 07 '16 at 13:42
-1

Please visit another resourse For Ex. below.

public class Point {

        private final int x;
        private final int y;

        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public int getX() {
            return x;
        }

        public int getY() {
            return y;
        }

        // ...
    }

Than you create Equals and HashCode as in Effective Java

// A better definition, but still not perfect
@Override public boolean equals(Object other) {
    boolean result = false;
    if (other instanceof Point) {
        Point that = (Point) other;
        result = (this.getX() == that.getX() && this.getY() == that.getY());
    }
    return result;
}


@Override public int hashCode() {
                return (41 * (41 + getX()) + getY());
    }
GensaGames
  • 5,538
  • 4
  • 24
  • 53