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 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.
public interface Predicate<T> {
boolean test(T t);
}
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.
public interface Function<T, R> {
R apply(T t);
}
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.
public interface Consumer<T> {
void accept(T t);
}
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.
public interface Supplier<T> {
T get();
}
- 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.
Characteristics of Stream:
Element Sequence:
Source:
Aggregate Operations:
Automated Iteration:
Different ways to creating Stream Object:
Stream<String> names = Stream.empty();
Collection<String> names = Arrays.asList("Amol", "Rushikesh", "Rutul", "Avinash", "Mayur");
Stream nameStream = names.stream();
String[] arr = new String[]{"a", "b", "c", "d"};
Stream<String> streamOfArrayFull = Arrays.stream(arr);
Stream Operations:
Intermediate Operations:
Terminal Operations:
![]() |
Java 8 Stream API Life Cycle |
0 Comments