Get Frequency of Words Using Lambda Expression in Java



In this article, we will learn how to calculate the frequency of words in a text using Java and Lambda Expressions. By leveraging concise syntax and functional programming techniques, we can efficiently process text data and determine word occurrences. This approach is particularly useful for applications like text analysis and natural language processing.

What is Lambda Expression?

A lambda expression in Java is a concise way to represent an anonymous function(a function without a name). Introduced in Java 8, it simplifies the implementation of functional interfaces (interfaces with a single abstract method), enabling cleaner and more readable code.

Syntax of Lambda Expressions

(parameters) -> expression_or_body

Using Stream API and Lambda Expressions

In the first approach, we will be using the Stream API combined with Lambda Expressions to group and count words efficiently.

Stream API

The Stream API in Java, introduced in Java 8, is a powerful tool for processing collections and streams of data in a functional style. It allows operations like filtering, mapping, and reducing with minimal code, enabling efficient and parallel processing.

Let's say the following is the List ?

List<String> list = Arrays.asList("Welcome", "to","the","club", "club", "the");

No, let us create a Map to get the frequency of words. Here, we are using Lambda Expression as well ?

Map<String, Integer> map = list
   .parallelStream()
   .flatMap(a -> Arrays.asList(a.split(" ")).stream())
   .collect(
   Collectors.toConcurrentMap(c ->c.toLowerCase(), c -> 1,
   Integer::sum));

Example

Below is an example to get frequency of words with Lambda Expression using Stream API and Lambda Expressions ?

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Demo {
   public static void main(String[] args) {
      List<String> list = Arrays.asList("Welcome", "to","the","club", "club", "the");
      Map<String, Integer> map = list
         .parallelStream()
         .flatMap(a -> Arrays.asList(a.split(" ")).stream())
         .collect(
         Collectors.toConcurrentMap(c ->c.toLowerCase(), c -> 1,
         Integer::sum));
      System.out.println("Word Frequency = "+map);
   }
}

Output

Word Frequency = {the=2, club=2, to=1, welcome=1}

Time Complexity

Splitting Text: O(n), where n is the length of the string.
Grouping and Counting: O(m), where m is the number of words.

Space Complexity

O(m) for storing words and frequencies in the map.

Using HashMap and Lambda Expressions

This approach uses a HashMap to manually count word frequencies with the help of Lambda Expressions.

HashMap

The HashMap class implements the Map interface using a hashtable, which allows basic operations like get() and put() to execute in constant time, even for large datasets.

Words are added to a HashMap, and their frequencies are updated using the merge() method.
The Integer::sum Lambda function adds 1 to the current frequency of the word.

Example

Below is an example to get frequency of words with Lambda Expression using HashMap and Lambda Expressions ?

import java.util.*;
public class WordFrequencyHashMap {
    public static void main(String[] args) {
        // Input string
        String text = "Lambda expressions in Java are concise. Java allows functional programming.";

        // Normalize and split text
        String[] words = text.toLowerCase().replaceAll("[^a-z ]", "").split("\s+");

        // Initialize HashMap to store word frequencies
        Map<String, Integer> wordFrequency = new HashMap<>();

        // Count word frequencies using Lambda
        for (String word : words) {
            wordFrequency.merge(word, 1, Integer::sum);
        }

        // Display word frequencies
        wordFrequency.forEach((word, frequency) -> 
            System.out.println("Word: " + word + ", Frequency: " + frequency));
    }
}

Output

Word: allows, Frequency: 1
Word: functional, Frequency: 1
Word: lambda, Frequency: 1
Word: java, Frequency: 2
Word: concise, Frequency: 1
Word: are, Frequency: 1
Word: in, Frequency: 1
Word: expressions, Frequency: 1
Word: programming, Frequency: 1

Time Complexity
O(n): Iterating through the words.
O(1): Average time for adding/updating in a HashMap.
Space Complexity
O(m): Space for storing the map.

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2024-12-13T13:55:29+05:30

538 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements