Java 8 Streams and Its Operations



In Java, stream was introduced in Java 8. It represents a collection of elements and supports functional operations on those collections. Here, we are talking about collections like Array, List and Set.

The Stream API simply channelizes the elements of sources through various built-in methods to return the desired result. In this article, we are going to discuss different operations that can be performed on the Java stream.

Operations on Java 8 Streams

There are two types of operations, we can perform on the Java streams ?

  • Intermediate Operations ? They process the elements of an input stream with the help of methods like filter(), map(), limit(), skip() and so on.

  • Terminal Operations ? They trigger the intermediate operation to produce a non-stream result such as printing, counting and storing. The terminal operations are performed with the help of forEach(), collect(), toArray() and so forth.

The methods of Java Streams are collectively called as a higher-order function. And, the main difference between intermediate and terminal operations is that the Terminal operation can't be chained together whereas the Intermediate operations can be chained.

Filter Operation on Java Stream

In the following Java program, we will demonstrate the filter operation of Java stream. For this purpose, we are going to use the filter() method to strain the number greater than 10, limit() method to restrict the size of output and forEach() method to print the result.

import java.util.*;
public class Example1 {
   public static void main(String[] args) {
      // creating a list of numbers
      List<Integer> numbers = Arrays.asList(5, 21, 32, 4, 13, 41, 10);
      // printing all numbers in the list
      System.out.println("Numbers in the given List : " + numbers);
      System.out.println("List of numbers greater than 10: ");
      // filtering the numbers greater than 10 from list
      numbers.stream().filter(nums -> nums > 10)
         // limiting the result to 2 only
         .limit(2)
         // printing the result
         .forEach(System.out::println);
   }
}

When you run this code, it produce following result ?

Numbers in the given List : [5, 21, 32, 4, 13, 41, 10]
List of numbers greater than 10: 
21
32

Sorting operation on Java Stream

The following Java program shows how to perform the sorting of streams. For this purpose, we will use the sorted() method to sort the elements in ascending order and then, forEach() method to print the result.

import java.util.*;
public class Example2 {
   public static void main(String []args) {
      // creating a list of numbers
      List<String> terms = Arrays.asList("Objects", "Classes", "Methods", "Constructors", "Abstract");
      System.out.println("List after Sorting: ");
      // Sorting the elements of list
      terms.stream()
         .sorted()
         // printing the result
         .forEach(System.out::println);
   }
}

On executing, following output will be displayed ?

List after Sorting: 
Abstract
Classes
Constructors
Methods
Objects

Use of startsWith() method

In this Java program, we will print the elements of streams that start with the character ?C'. We will make use of the filter(), startsWith() and collect() methods for this operation.

import java.util.*;
import java.util.stream.*;
public class Example3 {
   public static void main(String []args) {
      // creating a list of numbers
      List<String> terms = Arrays.asList("Objects", "Classes", "Methods", "Constructors", "Abstract");
      System.out.println("Original List: " + terms);
      System.out.println("List after filtering: ");
      // Filtering the elements that start with 'C'
      List<String> output = terms.stream()
                     .filter(x -> x.startsWith("C"))
                     .collect(Collectors.toList());
      // printing the result                        
      System.out.println(output);
   }
}

Output of the above code is as follows ?

Original List: [Objects, Classes, Methods, Constructors, Abstract]
List after filtering: 
[Classes, Constructors]

Use of distinct() method

The following Java program illustrates how to print the distinct elements only from the stream of elements using the distinct() method.

import java.util.*;
public class Example4 {
   public static void main(String[] args) {
      // creating a list of numbers
      List<Integer> numbers = Arrays.asList(55, 55, 44, 34, 89, 89, 10, 10);
      System.out.println("Distinct elements from the list: ");
      // removing duplicate numbers from the list 
      numbers.stream()
        .distinct()
        .forEach(System.out::println);
   }
}

Output will be obtained as ?

Distinct elements from the list: 
55
44
34
89
10
Updated on: 2024-08-01T11:44:01+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements