While count occurrences of a char finds how many times a single char is present, this example will group all characters of a string finding how many times a char is repeated. Each snippet calls toLowerCase
and will include the whitespace in the numbers.
Straight up Java
A pre java 8 solution is to use a HashMap using the char as a key and value is the number of times the char is present. If there is the char contained in the map the key or the count will be incremented else initialized.
Output
Java 8
Using a common regular expression to split on each char and converting a string to a stream we collect using a reduction operation to group chars. The Collectors.counting()
is a collector that accepts a char and counts the number of times it occurs. In the instance no elements are present, the result is 0. Finally outputting the char and the number of times it was present using a java 8 foreach.
Output
Google Guava
Before showing the code snippet, it is important to understand what a multiset is. 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.
The guava splitter class has the ability to split a string by a fixed length and in this case split it by 1 returning an Iterable. Passing the Iterable
into HashMultiset constructor which in concept will create a key of the char and a count as the value. Finally outputing the contents of the key and count.
Output