Java Stream API & Functional Interface
Shivangam Soni
Full Stack Developer | Researching NLP | JavaScript, TypeScript, React, Next, Express, Mongo, Python
Stream API
Stream API was added in JAVA 8 & it has provided a much better way of working with Collections. It provides a way to write Readable, Concise & Declarative code with the help of Functional Interface.
Stream API provides access to the Collections Data, which can be processed Sequentially or Parallel. Stream provides plenty of methods to perform operations on data which can produce a Result or Side-Effects.
Commonly used Stream Methods
I've explained Functional Interface Later in the Article.
How to use Stream API
In order to use Stream, we need a Collection or Array.
import java.util.Arrays
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
public class D2 {
? ? public static void main(String[] args) {
? ? ? ? List<Integer> nums = Arrays.asList(1, -4, 5, 2, 6, 9);
? ? ? ? Predicate<Integer> isEven = new Predicate<Integer>() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public boolean test(Integer t) {
? ? ? ? ? ? ? ? return t % 2 == 0;
? ? ? ? ? ? }
? ? ? ? };
? ? ? ? Consumer<Integer> print = new Consumer<Integer>() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void accept(Integer t) {
? ? ? ? ? ? ? ? System.out.println(t);
? ? ? ? ? ? }
? ? ? ? };
? ? ? ? nums.stream().filter(isEven).forEach(print);
? ? }
}
In this code, I'm Printing All the Even Numbers from a List, using Stream API & Functional Interface (more on this later).
Functional Interface
Now, the Code above seems like a really Verbose way of performing a simple task due to the way we have to define Functional Interfaces. But Java has a much better way of dealing with this issue. But before that we need to understand Functional Interface.
Functional Interface is used to represent & enforce SAM, i.e., Single Abstract Method. It's an interface that has only one Method, this empowers Functional Programming in JAVA & is at the Heart of Stream API.
The code above is a nice example of how to use Functional Interfaces by defining an Anonymous Inner Class.
Common Functional Interfaces
Lambda Expressions
The code in the First Section is a good example of how to use the Functional Interface, but as I pointed out before, it's a bit Verbose. To solve this problem, JAVA provides a better syntax known as the Lambda Expression.
Lambda Expression just replaces the Functional Interfaces definition into just a few lines of code.
// Normal Definition
Predicate<Integer> isEven = new Predicate<Integer>() {
? ? @Override
? ? public boolean test(Integer t) {
? ? ? ? return t % 2 == 0;
? ? }
};
// Lambda Expression
Predicate<Integer> isEven = n -> {
? ? return n % 2 == 0;
};
// Lamdba Expression with Implicit Return
Predicate<Integer> isEven = n -> n % 2 == 0;
In the code above, we are replacing the entire Inner Class Definition with just a few lines of code. Both the Lambda Expressions are directly defining the test method that takes in an argument n.
In the first Lambda, I'm explicitly returning the result of a boolean expression.
But in Lambda Expression it is possible to omit the Code Block & return as well if there is only one statement. Hence, in the second Lambda, I'm just writing the boolean expression that will implicitly get returned.
Even Filter using Lambda
Here is a re-implementation of Stream API Example using Lambda
import java.util.Arrays
import java.util.List;
public class D2 {
? ? public static void main(String[] args) {
? ? ? ? List<Integer> nums = Arrays.asList(1, -4, 5, 2, 6, 9);
? ? ? ? nums.stream()
? ? ? ? ? ? ? ? .filter(n -> n % 2 == 0)
? ? ? ? ? ? ? ? .forEach(n -> System.out.println(n));
? ? }
}
Now, with the help of Lambda Expressions, I'm able to perform the same task of Printing all the Even elements in a List using just 3 Lines of Code.
Day 2 Challenge
Challenge
Continue development of Product Management Project & Implement the Following:
Solution:
GitHub Repo?README includes the Explanation of My Solutions.
Thank You, Navin Reddy , for the 10-Day Challenge.