I am a software engineer at Google on the Android project and the creator of the Java testing framework TestNG. When I'm not updating this weblog with various software-related posts or speaking at conferences, I am busy snowboarding, playing squash, tennis, golf or volleyball or scuba diving. Cedric is a DZone MVB and is not an employee of DZone and has posted 90 posts at DZone. You can read more from them at their website. View Full User Profile

Coding Challenge ("Light" Edition)

02.21.2013
| 3140 views |
  • submit to reddit

This coding challenge is a bit different from the previous one ([1] [2]) because it is shorter and also a bit more closely tied to Java.

A School has either a name (“Stanford”) or a nickname (“UCSD”) or both.

Your task is to write equals() and hashCode() methods for the School class that satisfy the following requirement: two Schools are identical if they either have the same name or the same nickname.

 

Published at DZone with permission of Cedric Beust, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

sun east replied on Thu, 2013/02/21 - 10:50pm

Let  


School a = School(name = "Stanford", nickname = null);

School b = School(name = null, nickname = "UCSD");

School c = School(name = "Stanford", nickname = "UCSD");

 According to your description, the following relationship holds:


a.equals(c) == true
b.equals(c) == true
a.equals(b) == false

David Whatever replied on Fri, 2013/02/22 - 1:37am

Seems the hashCode part is not possible to do well, since you have to have equal hashes between two arbitrary strings (a name and a nickname). So I went with:

public class School {
   public String name, nickname;
   public boolean equals(Object other) {
       if (super.equals(other)) return true; if (!(other instanceof School) return false; School otherSchool = (School) other; return (name != null && name.equals(otherSchool.name) || (nickname != null && nickname.equals(otherSchool.nickame));    }
   public int hashCode() {
       return 0;
   }
}

Nils Rudolph replied on Fri, 2013/02/22 - 3:16am

This is a tricky question, if would say that such a equal function is not possible, or all school must be equal. I try to explain why:

The equal method must be an equivalence relation, so it must be:

transitive: if a==b and b==c then must a==c
symetric: if a==b then must b==a
reflexiv: a==a

So if you take two Schools which should not be equal: (assuming that there is a constructor which takes the name and the nickname as parameter)

s1 = School("a", "b")
s2 = School("c", "d")

and you create a third School:

s3 = School("a", "d")

now you have:

s1 == s3 and s3 == s2 

from the transitive property it follows:

s1 == s2

David Whatever replied on Fri, 2013/02/22 - 9:45am in response to: Nils Rudolph

I considered making all School objects equivalent in addition to making them all hash to zero; the requirement only said they had to be equal in the situation where the name/nickname matches, not that they had to be unequal in other cases ;-)

Farrukh Ijaz replied on Mon, 2013/02/25 - 2:02pm

static class School {
        public String name, nickname;
        
        @Override
        public boolean equals(Object source) {
        	if(!(source instanceof School)) return false;
            School s = (School) source;
			boolean flag = name != null && s.name != null && name.equals(s.name);
            return flag || (nickname != null && s.nickname != null && nickname.equals(s.nickname));
        }
        
        @Override
        public int hashCode() {
            if(name != null && nickname != null)
                return name.concat(nickname).hashCode();
            if(name != null)
                return name.hashCode();
            if(nickname != null)
                return nickname.hashCode();
            return 0;
        }
    }

I believe this the maximum we can implement. hashCode() method must return none-zero value in order to use the object with hash tables.

Somnath Shantveer replied on Thu, 2013/02/28 - 6:03am in response to: Farrukh Ijaz

 +1 to Farrukh... I ended up writing similar code.Want to know if some one has better implementation for hashcode.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.