Skip to content

Instantly share code, notes, and snippets.

@jimwhite jimwhite/School
Last active Dec 14, 2015

What would you like to do?
A tweak to the API gets the School challenge right.
import java.util.HashSet;
import java.util.Set;
public class School {
private final String name;
private final String nickname; // Nicks are not unique. How many Aggies are there?
public School(String name, String nickname) { = name;
this.nickname = nickname;
public int hashCode() {
return name.hashCode();
public boolean equals(Object obj) {
if (!(obj instanceof School)) return false;
School other = (School) obj;
return name.equals(;
public boolean equalsFuzzily(Object obj) {
if (!(obj instanceof School)) return false;
School other = (School) obj;
return name.equalsIgnoreCase( || nickname.equalsIgnoreCase(other.nickname);
public String toString() {
return name + " (aka " + nickname + ")";
public static void main(String[] args)
School school1 = new School("University of Pennsylvania", "upenn");
School school2 = new School("University of Pennsylvania", "Penn State");
System.out.println("school1: " + school1);
System.out.println("school2: " + school2);
System.out.println("Equals: " + school1.equals(school2));
System.out.println("EqualsFuzzily: " + school1.equalsFuzzily(school2));
Set<School> schools = new HashSet<School>();
System.out.println("Set: " + schools);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.