Created
June 25, 2013 06:29
-
-
Save wfwei/5856407 to your computer and use it in GitHub Desktop.
Hashable & Comparable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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