Java- Stream API

Stream API

Java lambda expressions are new in Java 8. Java lambda expressions are Java’s first step into functional programming.

Java lambda expressions are commonly used to implement simple event listeners / callbacks, or in functional programming with the Java Streams API


Functional Interfaces

Runnable r = new Runnable(){
 	@Override
 	public void run() {
  System.out.println("My Runnable");
 	}};

If you look at the above code, the actual part of the code is inside run() method. Rest all of the code is because of the way java programs are structured.

Java 8 Functional Interfaces and Lambda Expressions help us in writing smaller and cleaner code by removing a lot of boiler-plate code.

  • An interface with exactly one abstract method is called Functional Interface.

  • @FunctionalInterface annotation is added so that we can mark an interface as functional interface.

  • If the interface is annotated with @FunctionalInterface annotation and we try to have more than one abstract method, it throws compiler error.

  • The major benefit of java 8 functional interfaces is that we can use lambda expressions to instantiate them and avoid using bulky anonymous class implementation

Java 8 has defined a lot of functional interfaces in java.util.function package. Some of the useful java 8 functional interfaces are Predicate, Function, Consumer, Supplier,


java.util.function Package:

The java.util.function package in Java 8 contains many built-in functional interfaces like-

1.Predicate: The Predicate interface has an abstract method test() which gives a Boolean value as a result for the specified argument. Its prototype is

public Predicate
{
   public boolean test(T  t);
 }

2.Function: The Function interface has an abstract method apply which takes argument of one type T and returns a result of another type R. Its prototype is

public interface Function 
{
   public R apply(T t);
}

3.Consumer Interface: It accepts an input and returns no result.

@FunctionalInterface
public interface Consumer {
    void accept(T t);
}

And it contains default method andThen(Consumer<? super T> after)

4.Supplier Interface: In some scenarios we have no input but expected to return an output. Those situations **Supplier** can be used without the need to define a new functional interface every time.

@FunctionalInterface
public interface Supplier<T> {
    T get();
}

5.BinaryOperator: The BinaryOperator interface has an abstract method apply which takes two arguments and returns a result of same type. Its prototype is

public interface BinaryOperator 
{
     public T apply(T x, T y);
}