Java 8 Implementation for Word Frequency Count
Identifying the frequency of words within a given list is an essential task in natural language processing. Java 8 offers a versatile approach to efficiently count the occurrences of words using its robust stream API and built-in collectors.
Problem:
Consider the following list of words:
<code class="java">List<String> wordsList = Lists.newArrayList("hello", "bye", "ciao", "bye", "ciao");</code>
The objective is to determine the frequency count of each word, resulting in an output like:
<code class="java">{ciao=2, hello=1, bye=2}</code>
Java 8 Solution:
Unlike conventional methods, Java 8 employs a different approach:
<code class="java">Map<String, Long> collect = wordsList.stream() .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));</code>
In this line of code, the stream() method is used to initiate a stream of elements from the wordsList. Subsequently, Collectors.groupingBy() gathers elements based on their identity (the words themselves) and Collectors.counting() calculates the frequency of each occurrence.
For integers, the code can be slightly modified:
<code class="java">Map<String, Integer> collect = wordsList.stream() .collect(Collectors.groupingBy(Function.identity(), Collectors.summingInt(e -> 1)));</code>
Sorting by Value:
Furthermore, the resulting map can be sorted in descending order of word frequency:
<code class="java">LinkedHashMap<String, Long> countByWordSorted = collect.entrySet() .stream() .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) .collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> { throw new IllegalStateException(); }, LinkedHashMap::new ));</code>
This additional code snippet appends an entry set to the stream, sorts it in descending order based on the values, and collects the entries into a LinkedHashMap to maintain the sorted order.
The above is the detailed content of How can Java 8\'s Stream API be used to efficiently count word frequencies in a list?. For more information, please follow other related articles on the PHP Chinese website!