A frequency map in Java 8 or above can be created concisely with the help of Stream
and Collectors.groupingBy()
API.
It provides a general method to count the frequency of elements in a collection:
import java.util.stream.*;
import java.util.*;
import java.util.function.*;
<T> Map<T, Long> frequencyMap(Stream<T> elements) {
return elements.collect(
Collectors.groupingBy(
Function.identity(),
HashMap::new, // can be skipped
Collectors.counting()
)
);
}
Any streamable and countable collection can utilise the above method to count the frequency of elements.
A list can be converted to a stream by calling stream()
:
import java.util.stream.*;import java.util.*;import java.util.function.*;class HelloWorld {public static <String> Map<String, Long> frequencyMap(Stream<String> elements) {return elements.collect(Collectors.groupingBy(Function.identity(),HashMap::new, // can be skippedCollectors.counting()));}public static void main( String args[] ) {List<String> words = Arrays.asList("hello", "hello", "mighty");System.out.println(frequencyMap(words.stream()));}}
An array of characters can be converted to a stream using Arrays.stream()
import java.util.stream.*;import java.util.*;import java.util.function.*;class HelloWorld {public static <Character> Map<Character, Long> frequencyMap(Stream<Character> elements) {return elements.collect(Collectors.groupingBy(Function.identity(),HashMap::new, // can be skippedCollectors.counting()));}public static void main( String args[] ) {Character[] letters = {'a', 'b', 'b', 'c', 'c', 'c'};System.out.println(frequencyMap(Arrays.stream(letters)));}}
String characters can be converted to a stream using chars()
. However, it returns an integer stream instead of a character stream; so, each integer needs to be converted back to a character using the maptoObj()
method:
import java.util.stream.*;import java.util.*;import java.util.function.*;class HelloWorld {public static <Character> Map<Character, Long> frequencyMap(Stream<Character> elements) {return elements.collect(Collectors.groupingBy(Function.identity(),HashMap::new, // can be skippedCollectors.counting()));}public static void main( String args[] ) {String aString = "abc";System.out.println(frequencyMap(aString.chars().mapToObj(c -> (char) c)));}}
An array of unboxed typed elements can be converted to a stream using Arrays.stream()
and calling boxed()
:
import java.util.stream.*;import java.util.*;import java.util.function.*;class HelloWorld {public static <Integer> Map<Integer, Long> frequencyMap(Stream<Integer> elements) {return elements.collect(Collectors.groupingBy(Function.identity(),HashMap::new, // can be skippedCollectors.counting()));}public static void main( String args[] ) {int [] numbers = {1, 2, 2, 2, 3, 3};System.out.println(frequencyMap(Arrays.stream(numbers).boxed()));}}
Free Resources