Saturday, May 2, 2020

Stream API in Java

Stream in Java:

Stream API is used to process collection of objects. It is a sequence of objects that supports various methods which can be pipelined to produce desired result.

Following are the characteristic of the Stream -
  • Sequence of elements: A stream provides set of elements of specific type in a sequential manner. It get/commutes elements in demand. It never stores the elements
  • Source: Stream takes Arrays, Collections or I/O resource as input resource. 
  • Aggregate Operations: Stream supports aggregate operations like filter, map, limit, reduce, find, match etc.
  • Pipelining: Most of the stream operation returns stream itself so that their results can be pipelined. This operations are called as Intermediate Operations and their function is to take input, process them and return output to the target. collect() method is the terminal operation which is normally present at the end of the pipelining operation to mark the end of the Stream.
  • Automatic iteration: Stream operations does the iteration internally over the source elements provided.
In Java 8 Collection interface has two methods to generate the Stream:
  1. stream(): Returns a sequential stream.
  2. parallelStream(): Returns the parallel stream.
Example: 


The output will be:

No comments:

Post a Comment

Terraform Cheat Sheet [WIP]

Installing Terraform