Advertisement

java 8 tutorial : Introduction to streams and types of streams?

java 8 tutorial : Introduction to streams and types of  streams? Streams are an update to the Java API that lets you manipulate collections of data in a declarative way. ((you express a query rather than code an ad hoc implementation for it)).


Streams allow us to write collections-processing code at a higher level of abstraction.



A Stream is a tool for building up complex operations on collections using a functional approach. You can think of them as fancy iterators over a collection of data. In addition, streams can be processed in parallel transparently, without you having to write any multithreaded code.

A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result.

what exactly is a stream?

A short definition is “a sequence of elements from a source that supports data processing operations.”

Sequence of elements— Like a collection, a stream provides an interface to a sequenced set of values of a specific element type. Because collections are data structures, they’re mostly about storing and accessing elements with specific time/space complexities (for example, an ArrayList vs. a LinkedList). But streams are about expressing computations such as filter, sorted, and map that you saw earlier.

Source— Streams consume from a data-providing source such as collections, arrays, or I/O resources.

Note that generating a stream from an ordered collection preserves the ordering. The elements of a stream coming from a list will have the same order as the list.

Data processing operations— Streams support database-like operations and common operations from functional programming languages to manipulate data, such as filter, map, reduce, find,match, sort, and so on. Stream operations can be executed either sequentially or in parallel.

The features of Java stream are –

A stream is not a data structure instead it takes input from the Collections, Arrays or I/O channels. Streams don’t change the original data structure; they only provide the result as per the pipelined methods.

Types of Operations in Streams:

Intermediate Operations:
Stream operations that can be connected are called intermediate operations.
Intermediate operations don’t perform any processing until a terminal operation is invoked on the stream pipeline—they’re lazy.

Ex : filter,distinct,limit,map,flatmap,distinct

Terminal Operations:

Operations that close a stream are called terminal operations.

Ex: count(),collect(),foreach()

Benefits of Streams:

a) The code is written in a declarative way: you specify what you want to achieve (that is, filter dishes that are low in calories) as opposed to specifying how to implement an operation (using control-flow blocks such as loops and if conditions).



b) You chain together several building-block operations to express a complicated data processing pipeline.



To summarize, the Streams API in Java 8 lets you write code that’s

• Declarative— More concise and readable

• Composable— Greater flexibility

• Parallelizable— Better performance

java 8 streams,java 8 streams examples,java 8 streams api,java 8 streams tutorial,java 8 streams filter,java 8 streams interview questions,java 8 streams api tutorial,java 8 streams and filters,java 8 streams api interview questions,java 8 streams benefits,java 8 streams best practices,java 8 streams basics,java 8 streams explained,java 8 streams for beginners,java 8 streams filter map,

Post a Comment

0 Comments