Javatpoint Logo
Javatpoint Logo

Terminal Operations in Java 8

Java 8 introduced a significant enhancement to the Stream API, which revolutionized the way we process collections of data. Streams provide a streamlined and efficient way to perform operations on data, offering a functional programming approach to data manipulation. In this section, we will explore the concept of terminal operations in Java 8 and demonstrate their usage with complete programs and output.

Understanding Streams:

Before delving into terminal operations, let's briefly understand the concept of streams. In Java, a stream is a sequence of elements that can be processed in parallel or sequentially. Streams allow us to perform complex data operations, such as filtering, mapping, and reducing, in a declarative and concise manner.

Terminal Operations:

Terminal operations are the final operations performed on a stream, producing a result or a side effect. Unlike intermediate operations, which return a new stream, terminal operations are eager and consume the elements of the stream. Once a terminal operation is applied to a stream, the stream is considered consumed and cannot be reused.

Common Terminal Operations:

1. forEach:

The forEach operation allows us to perform an action on each element of a stream. It takes a Consumer as a parameter and executes it for each element of the stream. Here's an example:

Output:

John
Jane
Alice

2. count:

The count operation returns the number of elements in a stream as a long value. It is a terminal operation that terminates the stream after its execution. Here's an example:

Output:

Count: 5

3. collect:

The collect operation is used to accumulate elements of a stream into a collection or a single result. It takes a Collector as a parameter, which specifies the type of collection to be created. Here's an example:

Output:

Even Numbers: [2, 4]

4. reduce:

The reduce operation combines the elements of a stream into a single value. It takes a BinaryOperator as a parameter, which specifies the reduction operation. Here's an example:

Output:

Sum: 15

5. anyMatch and allMatch:

The anyMatch operation checks if any element of a stream matches a given condition, while the allMatch operation checks if all elements satisfy the given condition. They return a boolean value indicating the result. Here's an example:

Output:

Any Match: true
All Match: true

In summary, Terminal operations in Java 8 provide a convenient and powerful way to process data streams. They allow us to perform actions on stream elements, obtain results, or accumulate elements into collections or single values. By using terminal operations, we can write concise and expressive code for data manipulation. In this article, we explored several common terminal operations, including forEach, count, collect, reduce, anyMatch, and allMatch. We provided complete programs with their corresponding output, demonstrating their usage in real scenarios. By leveraging the power of terminal operations in Java 8, developers can simplify data processing tasks and write cleaner and more maintainable code. It's worth exploring the various terminal operations available in the Stream API to harness their full potential and enhance your Java programming skills.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA