Skip to content

Instantly share code, notes, and snippets.

@wfwei
Created June 25, 2013 06:29
Show Gist options
  • Select an option

  • Save wfwei/5856407 to your computer and use it in GitHub Desktop.

Select an option

Save wfwei/5856407 to your computer and use it in GitHub Desktop.
Hashable & Comparable
static class WordToName implements Comparable<WordToName> {
private String word;
private List<String> docNames;
public WordToName(String word) {
this.word = word;
this.docNames = new ArrayList<String>();
}
@Override
public int compareTo(WordToName wtn) {
Integer wtnSize = wtn.getDocNames().size();
Integer thisSize = this.getDocNames().size();
return wtnSize.compareTo(thisSize);
}
@Override
public int hashCode() {
return this.word.hashCode();
}
public boolean equals(Object obj) {
if (!(obj instanceof WordToName))
return false;
WordToName wtn = (WordToName) obj;
return this.word.equals(wtn.getWord());
}
public String getWord() {
return word;
}
public List<String> getDocNames() {
return docNames;
}
public void addDocName(String docName) {
this.docNames.add(docName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment