I am wondering what API or collection would be best to use for using scanner to search through a document, count the number of times a word appears and create an alphabetical list of both that and for each word a sublist of how many times it is followed by another word.
This is for a class, so please just point me in the right direction as I am completely new to Java and packages, but I don’t want any actual coding tips, thank you.
I imagine you could do something like that with
Map<String, Map<String, Integer>>. Essentially what you’ll have a word, which is associated with a map that contains all the successive words along with their frequency (i.e., the number of times they appear). So what you’d have is:For sorting, you could create a class that holds a word and its frequency. Then you can use a
TreeSetwith a comparator (or implementcompareToon your class) to enforce ordering. Then your map would look like this:Assuming
Frequencyis the class that holds information about the string and the number of times it appears. The only difficulty here is looking up the word each time you need to update its frequency because you will have to iterate over the set.