Stateless and Stateful Operations

Java-Stateless and Stateful Operations

Stateless and Stateful Operations

Intermediate operations in the stream are classified to two types: stateless and stateful. In a stateless operation, each element is processed independently of the others. In a stateful operation, the processing of an element may depend on aspects of the other elements. For example, consider the following program:

Stateless and Stateful Operations

The sorted() method produces a stream that contains the elements of the invoking stream sorted according to natural order. Sorting is a stateful operation because an element’s order depends on the values of the other elements. Thus, the sorted( ) method is stateful. However, filtering elements based on a stateless predicate is stateless because each element is handled individually. Thus, filter( ) can (and should be) stateless. The difference between stateless and stateful operations is especially important when parallel processing of a stream is desired because a stateful operation may require more than one pass to complete.


Stateless and Stateful Operations

Program Source

import java.util.ArrayList;

public class Javaapp {

    public static void main(String[] args) {

        ArrayList<Integer> arylist = new ArrayList<Integer>();
        arylist.add(10);>System.out.print(a+"  "));
        System.out.println();>a<200).forEach((a)->System.out.print(a+"  "));

Leave a Comment