Intermediate Methods of Stream in Java


In Java, the streams allow us to perform the functional operations on the specified element. It simply channelizes the elements of source such as arrays, files and classes of collection framework, through various built-in methods to return the result. These built-in methods could be intermediate or terminal. In this article, we are going to explore some intermediate methods of Stream, such as map, filter, reduce, and collect. These methods help us to manipulate and process data.

Intermediate Methods of Java Streams

The methods of Java Streams are collectively called as a higher-order function, which further classified as −

  • Intermediate Methods − They process the elements of an input stream.

  • Terminal Methods − They trigger the intermediate operation to produce a non-stream result such as printing, counting and storing.

In this section, we will talk about only intermediate methods of stream with the help of various examples.

List of intermediate methods of stream in Java −

  • filter()

  • map()

  • peek()

  • limit()

  • skip()

  • sorted()

  • distinct()

Let's discuss them one by one.

filter() method

The Java filter() method allows us to strain elements of the stream based on the specified condition. It is used to apply a certain behavior on stream items. This method takes a predicate as an argument and returns a list of elements that match the predicate.

Syntax

filter(predicate);

Example 

The following example illustrates the use of filter() method.

  • Create a list using Arrays.asList() method to store a fixed-size list.

  • Now, use the filter() method along with stream() and forEach() to filter out the odd numbers only. Here, stream() specifies input in the form of stream and we will use the terminal method forEach() to iterate and print the odd numbers.

import java.util.*;
public class Fltr {
   public static void main(String[] args) {
      // creating a list of numbers
      List<Integer> numbers = Arrays.asList(5, 21, 32, 14, 63, 19, 10);
      // printing all numbers in the list
      System.out.println("List of all numbers: " + numbers);
      System.out.println("List of odd numbers: ");
      // filtering only odd numbers from list
      numbers.stream()
         .filter(nums -> nums % 2 == 1)
         .forEach(System.out::println);
   }
}

Output

List of all numbers: [5, 21, 32, 14, 63, 19, 10]
List of odd numbers: 
5
21
63
19

map() method

This method takes a Function as an argument and applies it to each element of the Stream, which will produce a new Stream of the mapped elements. For instance, we can use the map() to convert a Stream of Strings to a Stream of Integers.

Example 

In this example, we will copy elements of one list to another list by using the map() method.

import java.util.*;
import java.util.stream.Collectors;
public class MapExample {
   public static void main(String[] args) {
      // creating a list of integers
      List<Integer> AList1 = Arrays.asList(11, 22, 33, 44, 55);
      // to copy elements of first list to new list 
      List<Integer> AList2 = AList1.stream()
         // copying the elements
         .map(i -> i) 
         // collecting the result
         .collect(Collectors.toList()); 
      // Printing the result
      System.out.println("The elements of the list: " + AList2);
   }
}

Output

The elements of the list: [11, 22, 33, 44, 55]

peek() method

We can use the peek() method to print the result of each intermediate operation.

Example 

In the following example, we will use filter() method along with peek() to print odd numbers from a list of numbers.

import java.util.*;
import java.util.stream.Collectors;
public class PeekExample {
   public static void main(String[] args) {
      // creating a list of numbers
      List<Integer> numbers = Arrays.asList(5, 21, 32, 14, 63, 19, 10);
      // printing all numbers in the list
      System.out.println("List of all numbers: " + numbers);
      System.out.println("List of odd numbers: ");
      // filtering only odd numbers from list
      numbers.stream()
         .filter(nums -> nums % 2 == 1)
         // using peek to get the result
         .peek(nums -> System.out.println(nums))
         // collecting the result
         .collect(Collectors.toList()); 
   }
}

Output

List of all numbers: [5, 21, 32, 14, 63, 19, 10]
List of odd numbers: 
5
21
63
19

limit() method

The purpose of using limit() method is to restrict the size of output to the specified limit.

Example 

In this example, we will restrict the result of intermediate operation to the specified size with the help of limit() method.

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

Output

List of all numbers: [5, 21, 32, 14, 63, 19, 10]
List of odd numbers: 
5
21

skip() method

We can discard the first n elements from the result using skip() method. It takes an integer as an argument that specifies those elements which will get discarded.

Example 

The following example demonstrates how to use skip() method to discard the first two elements from the filtered result.

import java.util.*;
public class SkipExample {
   public static void main(String[] args) {
      // creating a list of numbers
      List<Integer> numbers = Arrays.asList(5, 21, 32, 14, 63, 19, 10);
      // printing all numbers in the list
      System.out.println("List of all numbers: " + numbers);
      System.out.println("List of odd numbers: ");
      // filtering only odd numbers from list
      numbers.stream()
         .filter(nums -> nums % 2 == 1)
         // skipping first 2 elements from the result
         .skip(2)
         .forEach(System.out::println);
   }
}

Output

List of all numbers: [5, 21, 32, 14, 63, 19, 10]
List of odd numbers: 
63
19

sorted() method

This method can be used to sort the elements of a stream in ascending order.

Example 

The following example illustrates how we can sort the elements of stream in ascending order with the help of sorted() method.

import java.util.*;
public class SortedExample {
   public static void main(String[] args) {
      // creating a list of numbers
      List<Integer> numbers = Arrays.asList(5, 21, 32, 14, 63, 19, 10);
      // printing all numbers from the list in ascending order 
      System.out.println("List of numbers: ");
      // Sorting the elements of list
      numbers.stream()
         .sorted()
         .forEach(System.out::println);
   }
}

Output

List of numbers: 
5
10
14
19
21
32
63

distinct() method

To remove the duplicacy from the elements of a stream, we can use the distinct() method.

Example 

In this example, we will create a stream with duplicate elements and apply the distinct() method to print only distinct elements.

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

Output

Distinct list of numbers: 
21
32
14
19
10

Conclusion

We started this article by defining the stream and its intermediate methods that are a part of higher-order functions in Java. In the next section, we have discussed intermediate methods in detail with the help of example programs.

Updated on: 17-Aug-2023

362 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements