I am trying to get from TreeMap but it return null even the key exist. HashCode and eqauls is based on word only. Comparable is based on freqency.
public sta
In your compareTo
method it is comparing the frequency. So if the frequency is same it will be equal.
to compare the words you can use
return this.word.compareTo(o.word);
or to compare both word and frequency you can use
return this.word.compareTo((o.word)) * this.freq.compareTo(o.freq);
EDIT
Now as you need to sort using the frequency so instead of using comparable you can use Comparator
. Use the above comparators to create the Map. And use your previous comprator to sort.
while creating
TreeMap test = new TreeMap(
new Comparator() {
public int compare(Word word, Word o) {
return word.word.compareTo((o.word));
}
});
and while sorting
Collections.sort(new LinkedList(test.keySet()), new Comparator() {
public int compare(Word word, Word o) {
return word.freq.compareTo((o.freq));
}
});