Search Bar

Java 8 Stream API with basic interview questions and solutions from basic to advance.

 

Java 8 Stream API with basic interview questions and solutions from basic to advance!


Hi Programmers, 

Today we are going to learn about Java 8 new feature called Stream API.  

Introduction to java 8:

Java8 was officially released into market in the year of March 18th, 2014 under Oracle Corporation.

Java8 Stream API Flow and working lifecycle
Java8 Stream API Flow and working lifecycle

Up to Java 7 version we call as "Object Oriented Programming" Java8 version Onwards it supports "Object Oriented Programming" & "Functional Programming".
Functional Programming is a new style of coding for java developers where we can pass functions as a parameters to java methods.
Functional Programming having some building blocks of i.e. "Functional Interface & Lambda Expressions".
The Advantage Functional Programming is to write very concise code and more readability.
Most of the projects in industry already migrated into Java8 and Java 11 versions.
Whenever we are using lambda expression it will be enabled functional programming in Java.


Java8 Features:

  • Functional Programming
  • DateTime API
  • Lambda Expressions
  • Stream API
  • ForEach() Method
  • Method Reference
  • String Joiner
  • Default Method
  • Nashone JavaScript Engine
  • Optional Class etc...


Functional Interface:

An Interface has only one abstract method then such type of interface called "Functional Interface".

Functional Interface is also called as SAM (Single Abstract Method).

All the Functional Interfaces are available in one package i.e. java.utol.funcation from java8 version and totally provided 43 Predefined Functional Interfaces.

Important Functional Interfaces are java.util.function.Consumer , java.util.function.Supplier , java.util.function.Function , java.util.function.Predicate etc,.

We can apply lambda Expression where ever the Functional Interfaces are available in java.



@FunctionalInterface

is the marker annotation to mark the interface as Functional Interface strictly.

Before Java8 version we have some couple of Functional Interfaces i.e. Runnable, Comparable, Comparator, ActionListener, Callable, etc.

Lambda Expression: 

Lambda Expression is an anonymous function or nameless function or nameless method that means function/method doesn't have the name, return type and access modifier and with a list of parameters.

Arrow Symbol (->) is used to separate the list of parameters and body.

Normal Java Method:


public void additional(int a, int b) {
System.out.println("Addition of two numbers: " + (a + b));
}

Lambda Expression:

1st way = 


(int a, int b) -> {
System.out.println("Addtion of two numbers:" + (a + b));
}


2nd way =


(int a, int b) -> System.out.println("Addtion of two numbers:" + (a + b));


3rd way = 


(a, b) -> System.out.println(a + b);


4th way = 


a, b -> System.out.println(a + b);


Lambda Expression are mainly used to provide the implementation of Functional Interface Method.

Predicate Functional Interface:

It is one of functional interface from Java8 and it is available from java.util.function package.

Predicate Functional interface contains one abstract method i.e. test() which is taking some input and returning boolean value.

Predicate is purely for condition evaluation purpose.

@FunctionalInterface
public interface Predicate<T> {

boolean test(T t);

}

Here "T" is Typed Parameter accepts any kind of input.


Function Functional Interface:

It is one of functional interface from Java8 and it is available from java.util.function package.

Function Functional interface contains one abstract method i.e. apply() which taking some input and returning any type of value.

Function Functional Interface used for processing/transforming some data.

@FunctionalInterface
public interface Function<T, R> {

R apply(T t);

}

Here "T" is Typed Parameter which represents input value where as "R" represents Return value.

Consumer Functional Interface:

It is one of functional interface from Java8 and it is available from java.util.function package.

Consumer Functional Interface contains one abstract method i.e. accept() which is taking some input and no return value.

Consumer can be used to consume object and perform certain operation.

@FunctionalInterface
public interface Consumer<T> {

void accept(T t);
}

Here "T" is Typed Parameter accepts any kind of input.

Supplier Functional Interface:

It is one of functional interface from Java8 and it is available from java.util.function package.

Supplier Functional Interface contains one abstract method i.e. get() which will return some value which out taking any input.

Supplier can be used to supply items (objects).
Supplier won't take any input and it will always supply objects.
Supplier Functional interface does not contain any default static methods.

@FunctionalInterface
public interface Supplier<T> {

T get();
}

Here "R" represents Return type value from supplier.


  • Stream is one of the important feature from Java8 Version.
  • Collections and Streams purpose also different from each other:
  • Collections >> Storing the data.
  • Streams >> Processing & Filtering data over different data sources.

Stream represents sequence of objects derived from a source (Arrays, Collections, IO Resources) over which aggregate oprations can be performed.

From Java8 Versions onwards to define stream object oracle corporation provided one predefined interface i.e. java.util.stream.Stream interface.


Characteristics of Stream:

Element Sequence:

Stream provide a set of elements of particular type in sequential manner. The Stream gets an element on demand and never store an item.

Source:

Stream can take the data from different sources such as Collection, Arrays, I/O Resources etc.,

Aggregate Operations:

Streams supports aggregate operations such as forEach, filter, map, sorted and so on.

Automated Iteration: 

Stream operations carry out iterations internally over the source of elements as opposed to collections where explicit iteration is required.

Different ways to creating Stream Object:

1) Creating empty stream object which allowed to string values only.

Stream<String> names = Stream.empty();

2) Creating stream object from collection Object.

Collection<String> names = Arrays.asList("Amol", "Rushikesh", "Rutul", "Avinash", "Mayur");
Stream nameStream = names.stream();

3) Creating stream object from Array:

String[] arr = new String[]{"a", "b", "c", "d"};
Stream<String> streamOfArrayFull = Arrays.stream(arr);

Stream Operations:

Intermediate Operations:

returns stream of elements as a result.
Examples: filter(), map(), sorted(), distinct(), etc...

Terminal Operations:

returns non-stream values like primitives/Object not return anything.
Examples: forEach(), collect(), reduce(), match(), count(), etc...

Streams in Java 8
Java 8 Stream API Life Cycle


Stream source is the source from where we get data to process it can be a Arrays, Collection, I/O channel means files or input output files.
This Stream source provided to intermediate operations they can zero or many it is not execute until the Terminal Operations perform and final we get output as aggregate results.





Post a Comment

0 Comments