Home > Java > javaTutorial > How can Java 8\'s Stream API be used to efficiently count word frequencies in a list?

How can Java 8\'s Stream API be used to efficiently count word frequencies in a list?

Susan Sarandon
Release: 2024-11-03 14:51:02
Original
996 people have browsed it

How can Java 8's Stream API be used to efficiently count word frequencies in a list?

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template