Intermediate and Terminal operations in java

Intermediate and Terminal operations in java

In the Java programming language, the Java Stream API provides several built-in Stream API operation methods to process and manage user-defined program data type elements in a clear and easily ordered, readable format.

Intermediate and Terminal operations in java

Stream operations in Java are primarily divided into two categories.

  • Intermediate Stream API data operations.
  • Terminal Stream API data operations.

A typical stream pipeline in Java looks like this.

Program Data Type Source

↓

Intermediate Data Operation

↓

Intermediate Data Operation

↓

Terminal Data Operation

↓

Final Output

Example of Stream operations.

List<Integer> integers =

import java.util.Arrays;

import java.util.List;

public class Main {

public static void main(String[] args) {

List<Integer> integers = Arrays.asList(2, 7, 9, 11, 17, 23, 77);

integers.stream()

.filter(integer -> integer > 7)

.map(integer -> integer + 4)

.forEach(System.out::println);

}

}

So let’s explore the Stream operations in Java programming in detail.

  • filter() → Intermediate Stream data operations.
  • map() → Intermediate Stream data operations.
  • forEach() → Terminal Stream data operations.

Intermediate Stream Data Operations with Java.

Intermediate data operations in Java streams are user-defined program operations that process, filter, or transform data value elements of a stream into user-defined condition expressions.

Special features of intermediate stream data.

  • Intermediate data operations return other stream values.
  • These data operations are lazy in nature.
  • Java users can chain intermediate data operations together.
  • They do not automatically display the final result.
  • Intermediate data operations are executed only when a terminal operation is called in the current program.

Some common intermediate stream data operation methods in Java.

#Common OperationIntermediate Operations Description
1.filter() functionThis function used to filter elements data base on a specific user define condition
2.map()functionThis function used to convert each element into another data value element
3.flatMap()functionThis function used to convert and flattens multiple streams data element
4.sorted()functionThis function used to sort a give particular data elements value in specific order or sequence
5.distinct()functionThis function used to remove duplicate double data elements values
6.limit()functionThis function used to limit the number of data elements values
7.skip()functionThis function used to skip a specified number of data elements values
8.peek()functionThis function used to perform an action during data element values processing

filter() intermediate stream data operation.

The filter() function method in Java stream programs is used to select data elements that satisfy a user-defined custom condition.

List<Integer> integers =

import java.util.Arrays;

import java.util.List;

public class Main {

public static void main(String[] args) {

List<Integer> integers = Arrays.asList(4, 9, 29, 48, 59, 69, 78);

integers.stream()

.filter(integer -> integer > 30)

.forEach(System.out::println);

}

}

Here, in this example, only integer values ​​greater than 30 are selected and displayed in the output.

map() Intermediate stream data operation.

In Java stream programs, the map() function method is used to modify the value of each data program element.

List<Integer> integers =

import java.util.Arrays;

import java.util.List;

public class Main {

public static void main(String[] args) {

List<Integer> integers = Arrays.asList(5, 15, 35, 50, 60, 70, 89, 99);

integers.stream()

.map(integer -> integer / 5)

.forEach(System.out::println);

}

}

The original stream data values ​​​​are modified.

5 → 1

15 → 3

35 → 7

50 → 10

60 → 12

70 → 14

89 → 17

99 → 19

distinct() Intermediate stream data operation.

The distinct() function method in Java stream programs is used to remove duplicate data element values ​​in the current program.

List<Integer> integers =

import java.util.Arrays;

import java.util.List;

public class Main {

public static void main(String[] args) {

List<Integer> integers = Arrays.asList(7, 9, 8, 9, 21, 7, 33, 44, 7);

integers.stream()

distinct()

forEach(System.out::println);

}

}

sorted() intermediate stream data operation.

The sorted() function method in a Java stream program arranges or arranges data elements in ascending order by sorting them in natural order.

List<Integer> integers =

import java.util.Arrays;

import java.util.List;

public class Main {

public static void main(String[] args) {

List<Integer> integers = Arrays.asList(99, 1, 33, 3, 77, 55, 66, 2, 7);

integers.stream()

.sorted()

.forEach(System.out::println);

}

}

Intermediate stream data operation in descending order.

import java.util.Arrays;

import java.util.List;

import java.util.Comparator;

public class Main {

public static void main(String[] args) {

List<Integer> integers = Arrays.asList(99, 1, 33, 3, 77, 55, 66, 2, 7);

integers.stream()

.sorted(Comparator.reverseOrder())

.forEach(System.out::println);

}

}

limit() Intermediate stream data operation.

In a Java stream program, the limit() function method limits the number of program data elements and displays them.

List<Integer> integers =

import java.util.Arrays;

import java.util.List;

public class Main {

public static void main(String[] args) {

List<Integer> integers = Arrays.asList(99, 1, 33, 3, 77, 55, 66, 2, 7, 11, 17);

integers.stream()

.limit(5)

.forEach(System.out::println);

}

}

skip() Intermediate stream data operation.

In a Java stream program, the skip() function method skips a fixed number of data element values.

List<Integer> integers =

import java.util.Arrays;

import java.util.List;

public class Main {

public static void main(String[] args) {

List<Integer> integers = Arrays.asList(99, 1, 33, 3, 77, 55, 66, 2, 7, 11, 17);

integers.stream()

.skip(7)

.forEach(System.out::println);

}

}

Terminal Operations with Java.

Terminal operations in the Java programming language are operations that display a final result as output or perform a final action in the current program.

Essential features of Terminal Operations with Java.

  • Terminal operations display a final result or side effect.
  • Terminal operations initiate intermediate operations.
  • After a terminal operation in a Java program, stream data cannot normally be reused.
  • A stream pipeline in Java normally terminates with a terminal operation.

Common Terminal stream data Operations

#Terminal OperationCommon Terminal stream data Description
1.forEach() functionThis method used to perform an action for each data element values
2.collect()functionThis method used to collect data elements into a collection/result output
3.count()functionThis method used to return the number of data elements
4.reduce()functionThis method used to combine elements data into one value
5.min()functionThis method used to find a minimum stream data element
6.max()functionThis method used to find maximum stream data element
7.findFirst()functionThis method used to return the first of data element
8.findAny()functionThis method used to return any data element
9.anyMatch()functionThis method used to check whether any element matches or not n group data element
10.allMatch()functionThis method used to check whether all data elements match or not
11noneMatch()functionThis method used to check whether no elements match

forEach() Terminal Operations with Java.

The forEach() function method in a Java stream program performs an operation on each data element.

List<String> aeroplane =

import java.util.Arrays;

import java.util.List;

public class Main {

public static void main(String[] args) {

List<String> aeroplane = Arrays.asList(“Airbus A380”, “Boeing 747”, “Boeing 777”, “Airbus A350”);

aeroplane.stream()

.forEach(System.out::println);

}

}

In the above program, forEach() is a terminal method that applies stream data operations.

collect() Terminal Operations with Java.

In Java stream programs, the collect() function method is used to collect stream data elements into a collection or some other result.

List<Integer> integers =

import java.util.Arrays;

import java.util.List;

import java.util.stream.Collectors;

public class Main {

public static void main(String[] args) {

List<Integer> integers = Arrays.asList(99, 1, 33, 3, 77, 55, 66, 2, 7, 11, 17);

List<Integer> output = integers.stream()

.filter(integer -> integer > 33)

.collect(Collectors.toList());

System.out.println(output);

}

}

In newer Java versions, Java users can also create it like this.

List<Integer> output =

integers.stream()

.filter(integer -> integer > 37)

.toList();

count() Terminal Operations with Java.

The count() function method in a Java stream program displays the number of elements.

List<Integer> integers =

import java.util.Arrays;

import java.util.List;

public class Main {

public static void main(String[] args) {

List<Integer> integers = Arrays.asList(3, 7, 10, 18, 21, 33, 77);

long count = integers.stream()

.filter(integer -> integer > 9)

.count();

System.out.println(count);

}

}

reduce() Terminal Operations with Java.

In a Java stream program, the reduce() function method combines all data elements into a single result and displays it.

reduce() Terminal Operations Example: Finding the sum.

List<Integer> integers =

import java.util.Arrays;

import java.util.List;

public class Main {

public static void main(String[] args) {

List<Integer> integers = Arrays.asList(3, 7, 10, 18, 21, 33, 77);

int total = integers.stream()

.reduce(0, (p, q) -> p + q);

System.out.println(total);

}

}

Its output.

169

Calculation in Java is conceptually as follows.

3 + 7 + 10 + 18 + 21 + 33 + 77 = 169

min() and max() are terminal operations in Java.

These Java stream program operations extract and display the minimum and maximum values.

List<Integer> integers =

import java.util.Arrays;

import java.util.List;

import java.util.Optional;

public class Main {

public static void main(String[] args) {

List<Integer> integers = Arrays.asList(3, 7, 10, 18, 21, 33, 77);

Optional<Integer> min =

integers.stream().min(Integer::compareTo);

Optional<Integer> max =

integers.stream().max(Integer::compareTo);

System.out.println(min.get());

System.out.println(max.get());

}

}

Its output.

3

77

In this example, the min() and max() functions return an Optional value, as this could be an empty stream.

findFirst() terminal operations in Java.

In a Java stream program, the findFirst() function method returns the first data element value.

List<Integer> integers =

import java.util.Arrays;

import java.util.List;

import java.util.Optional;

public class Main {

public static void main(String[] args) {

List<Integer> integers = Arrays.asList(3, 7, 10, 18, 21, 33, 77);

Optional<Integer> output = integers.stream().findFirst();

System.out.println(output.get());

}

}

Its output.

3

findAny() terminal operations in Java.

In a Java stream program, the findAny() function method returns the data value of any element from the stream.

Optional<Integer> output =

integers.stream().findAny();

System.out.println(output.get());

This is especially useful with parallel streams, where the returned data element is not necessarily the first.

Matching terminal operations in Java.

Java provides three useful terminal operations for checking user-defined conditions in a program.

anyMatch() terminal operations in Java.

It checks whether at least one data element satisfies the condition in the current program.

boolean output =

integers.stream()

.anyMatch(integer -> integer > 11);

System.out.println(output);

Its output.

true

allMatch() is a terminal operation in Java.

It checks whether all data elements satisfy a value condition in a Java program.

boolean output =

integers.stream()

.allMatch(integer -> integer > 0);

System.out.println(output);

Its output.

true

noneMatch()

It checks whether any data element satisfies a value condition in the current Java program.

boolean output =

integers.stream()

.noneMatch(integer -> integer < 0);

System.out.println(output);

Its output.

true

Difference Between Intermediate and Terminal Operations

#Intermediate Data OperationsTerminal Data Operations
1.Intermediate data operations return a stream data outputTerminal stream data operations return a final result or void value
2.Intermediate data operations are lazy in nature to workTerminal stream data operations trigger stream data for processing
3.Intermediate data operations can be chained as natureTerminal stream data operations usually ends the pipeline system
4.Intermediate data used for transformation/filtering data or values elementTerminal stream data used to produce final output or result
5.We used filter() with intermediate data operationsWe can used foreach() with terminal data operations
6.We used map() with intermediate data operationsWe can used collect() with terminal data operations
7.We used sorted() with intermediate data operationsWe can used count() with terminal data operations
8.We used distinct() with intermediate data operationsWe can used reduce() with terminal data operations
9.We used limit() with intermediate data operationsWe can used min() / max() with terminal data operations
10.We used skip() with intermediate data operationsWe can used findfirst() with terminal data operations

A complete example.

import java.util.*;

import java.util.stream.Collectors;

public class Main {

public static void main(String[] args) {

List<Integer> integers =

Arrays.asList(3, 7, 10, 18, 21, 33, 77);

List<Integer> result =

integers.stream()

.filter(integer -> integer > 21)

.map(integer -> integer * 3)

.sorted()

.collect(Collectors.toList());

System.out.println(result);

}

}

Its output.

[99, 231]

Execution Flow with Stream Data Operations.

Original list data elements

↓

3, 7, 10, 18, 21, 33, 77

↓

filter(integer > 21)

↓

33, 77

↓

map(integer * 3)

↓

99, 231

↓

sorted()

↓

99, 231

↓

collect()

↓

Final list

Here in this example.

  • filter() → Intermediate
  • map() → Intermediate
  • sorted() → Intermediate
  • collect() → Terminal

Conclusion of Intermediate and Terminal operations.

In a Java program, intermediate operations create the processing pipeline, while terminal operations start the processing and produce it.

As the final result.

Data Element Source

↓

filter() ← Function intermediate operation

↓

map() ← Function intermediate operation

↓

sorted() ← Function intermediate operation

↓

collect() ← Terminal operation

↓

Final result

Leave a Reply