Java 8 Streams - Count Frequency Of Words In a List

Example 1 

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Main {

public static void main(String[] args) {

List<String> words = Arrays
.asList("cat", "rat", "bat", "cow", "cat", "bat");

// For Long values
Map<String, Long> result = words.stream()
.collect(Collectors.groupingBy(Function.identity(),
Collectors.counting()));
System.out.println(result);
}
}

  1. The List interface in Java provides a way to store the ordered collection. It is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored.
  2. The asList() method of java.util.Arrays class is used to return a fixed-size list backed by the specified array. This method acts as a bridge between array-based and collection-based APIs, in combination with Collection.toArray(). 
  3. A Map contains values on the basis of key, i.e. key and value pair. Each key and value pair is known as an entry. A Map contains unique keys.
  4. A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result.  A stream is not a data structure instead it takes input from the Collections, Arrays or I/O channels.
  5. Java Stream collect() performs a mutable reduction operation on the elements of the stream. This is a terminal operation.
  6. The groupingBy() method of Collectors class in Java are used for grouping objects by some property and storing results in a Map instance. In order to use it, we always need to specify a property by which the grouping would be performed.
  7. identity is a static method of the Function interface that always returns the input argument.
  8. Collectors counting() method is used to count the number of elements passed in the stream as the parameter. It returns a Collector accepting elements of type T that count the number of input elements. If no elements are present, the result is 0. It is a terminal operation.

Output:

{bat=2, rat=1, cat=2, cow=1}



Example 2

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Main {

public static void main(String[] args) {

List<String> words = Arrays
.asList("cat", "rat", "bat", "cow", "cat", "bat");

// For Integer values
Map<String, Integer> result = words.stream()
.collect(Collectors.groupingBy(Function.identity(),
Collectors.summingInt(e -> 1)));
System.out.println(result);
}
}
  1. The List interface in Java provides a way to store the ordered collection. It is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored.
  2. The asList() method of java.util.Arrays class is used to return a fixed-size list backed by the specified array. This method acts as a bridge between array-based and collection-based APIs, in combination with Collection.toArray().
  3. Map contains values on the basis of key, i.e. key and value pair. Each key and value pair is known as an entry. A Map contains unique keys.
  4. stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. A stream is not a data structure instead it takes input from the Collections, Arrays or I/O channels.
  5. Java Stream collect() performs a mutable reduction operation on the elements of the stream. This is a terminal operation.
  6. The groupingBy() method of Collectors class in Java are used for grouping objects by some property and storing results in a Map instance. In order to use it, we always need to specify a property by which the grouping would be performed.
  7. identity is a static method of the Function interface that always returns the input argument.
  8. The summingInt() method returns a Collector that produces the sum of an integer-valued function applied to the input elements. In other words - it sums the integers in the collection and returns the result. In the case of no input elements, the return value is 0.

Output:

{bat=2, rat=1, cat=2, cow=1}

Comments

Popular posts from this blog

Spring Boot OpenAI Integration: Step-by-Step Guide

Orchestration-Based Saga Architecture and Spring Boot Microservices Implementation Guide

Spring Boot 3 + Angular 15 + Material - Full Stack CRUD Application Example