0

I have a class School:

public class School{
 private int noOfTeachers;
 private int noOfStudents;
 //setters and getters....
 public boolean equals(Object that){
 //instance of check..
 return (this.noOfTeachers == ((School)that).noOfTeachers || 
 this.noOfStudents== ((School)that).noOfStudents);
 }
 public int hashCode(){
 //What goes in here??? o.O
 }
}

How should I proceed with implementation of hashCode for this class? I am not able to think of a strategy which will involve both noOfTeachers and noOfStudents for calculation of hash. Rather noOfTeachers and noOfStudents combination seems to be breaching contract between equals and hashCode.

asked May 11, 2013 at 18:19
4
  • 7
    You're right - any equality check with "or" is going to breach transitivity. Commented May 11, 2013 at 18:21
  • 1
    I don't see how you can. You could, of course, return 1 - but that is next to useless as your class wouldn't work in HashMaps and HashSets. What are you using this equals method for? Commented May 11, 2013 at 18:24
  • I think your best bet is to leave equals and hashCode alone and create a method that checks this. The reason to override those methods is so that Collections respect equality using your equals method rather than the default which uses ==. Commented May 11, 2013 at 18:28
  • What is the criteria for a School object to be equal to another one? I see you're returning true if either his number of Teachers or number of Students are the same, is this correct? I would think of an object to be equal to another if all of their fields are the same, not only one of them. The easiest way I see to solve this is to implement an own version of equals(School that), which do the comparision between them and forget about overriding the equals and hashCode methods. Commented May 11, 2013 at 18:51

1 Answer 1

2

You will not find any proper hashCode() implementation for your class, That would serve your need! Because your equals method uses OR to its properties of your class. Using OR in the equals method returns true for more than one state of any object.

So using unique return value of the hashCode() you certainly cannot represent multiple state! And more over you should know, hashCode() must return unique value for an object. As returning multiple values by it, leads to an ambiguity (and harms certainty) to put an object into any map. Because that is a violation of the assumption of "Uniqueness of keys of a map".

answered May 11, 2013 at 18:24
3
  • 2
    This certainly doesn't work. The OP wants an equals method with an or. Commented May 11, 2013 at 18:26
  • if two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same result Commented May 11, 2013 at 18:33
  • @BoristheSpider, I request you to see the answer now, again, thanks! Commented May 11, 2013 at 18:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.