Something like HashMap but sorted?

hashmap, java

Solution

The Manual way to do it is as follows:

- Create a composite WordCount class with `word` and `count` fields.

- Create a Comparator for that class that sorts by count.

- When you're done filling your HashMap, create a new List of WordCount objects created from values in the HashMap.

- Sort the List using your comparator.

Problem

I'm writing a Java program that parses all the words from a text file and then adds them to a HashMap. I need to count how many distinct words are contained in the file. I also need to figure out the highest counted words. The HashMap is comprised of each word mapped to an integer which represents how many times the word occurs. Is there something like HashMap that will help me sort this?

Original source