Home >Java >javaTutorial >How can I efficiently count word frequencies in Java 8 using streams?

How can I efficiently count word frequencies in Java 8 using streams?

DDD
DDDOriginal
2024-11-03 12:33:02613browse

How can I efficiently count word frequencies in Java 8 using streams?

Word Frequency Counting in Java 8: A Streamlined Approach

In Java 8, counting the frequency of words in a list can be achieved elegantly using streams.

Consider the following example list: List wordsList = Lists.newArrayList("hello", "bye", "ciao", "bye", "ciao");. To count word frequencies, we utilize the Collectors.groupingBy and Collectors.counting collectors.

<code class="java">Map<String, Long> wordFrequencies = wordsList.stream()
    .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));</code>

This code first groups the list elements by their string identity, effectively creating a map where keys are unique words and values are their counts. The Collectors.counting collector automatically increments the count for each word.

The resulting map, wordFrequencies, will resemble {ciao=2, hello=1, bye=2} when printed.

Alternatively, for integer-valued counts:

<code class="java">Map<String, Integer> wordFrequencies = wordsList.stream()
     .collect(Collectors.groupingBy(Function.identity(), Collectors.summingInt(e -> 1)));</code>

This variant uses Collectors.summingInt to accumulate integer counts.

To sort the resulting map by value, we can chain additional streams and collectors:

<code class="java">LinkedHashMap<String, Long> countByWordSorted = wordFrequencies.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 sorting ensures that the most frequently occurring words appear first in the map.

The above is the detailed content of How can I efficiently count word frequencies in Java 8 using streams?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn