This snippet will show how to count the number of words from a text file using java 7 syntax. You could read the file into an arraylist using another library for backwards compatibility.
First we will read the lines of the text file by calling Files.readAllLines and storing the results in an array list. Next we will create a HashMap that will store the word found as the key and the value will represent the number of times it was found. Iterating over each line in the file and splitting the string by a space we will check if the word exists in the map, if so we will increment the count otherwise we will put it to the map with an intial value.
Output
Java 8
Using java 8 syntax we will find the unique words contained within a text file. Java 7 file api NIO introduced Files, a static utility that contains methods for working with files, we will read all lines from a file as a Stream. Then calling Stream.flatmap we will break the line into words elements. If we had a line made up of "she skipped while she was chewing bubble gum", this line would be broken into ["she", "skipped", "while", "she", "was", "chewing", "bubble", "gum"]. Calling the the Stream.distinct method will find all unique occurrences of words.
Google Guava
Wikipedia defines a multiset, in mathematics, as "a generalization of the notion of set in which members are allowed to appear more than once... In multisets, as in sets and in contrast to tuples, the order of elements is irrelevant: The multisets {a, a, b} and {a, b, a} are equal." A guava multiset is like a set but can contain multiple items and has methods has useful summary methods like count. Below we will use guava Splitter to split a string and load a HashMultiset.