Skip to main content

Java 8 Functional Interfaces.

Java 8 functional Interfaces.

What is Functional programming:

Functional programming is a mathematical style of programming which executes sequentially, it takes inputs and gives output, it is not preferable to modify the input data. It's totally about programming with values.

Sample:

function addNumbers(a, b){
        return a+b;
}

Functional Programming.

Java 8 functional programming:

An interface with a single method is known as a Functional Interface. 

what is lambda expression:

Lambda expression is a separator between input and output.
An interface with a single method is known as a Functional Interface. 

  • Whether the interface have only one abstract (unimplemented) method?
  • Whether the parameters of the lambda expression match the parameters of the single method?
  • Whether the return type of the lambda expression match the return type of the single method?

If the answer is true for above three questions, then the given lambda expression is matched successfully against the interface.

Types of Functional interfaces:

These features are functional interfaces (an interface with only one abstract method) which belongs to the java.util.function package.

1. Consumers

public interface Consumer<T>

Represents an operation that accepts a single input argument and returns no result. Unlike most other functional interfaces, Consumer is expected to operate via side-effects.This is a functional interface whose functional method is accept(Object).

2. Suppliers

public interface Supplier<T>
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

3. Predicates

public interface Predicate<T>

Represents a predicate (boolean-valued function) of one argument.This is a functional interface whose functional method is test(Object).















Comments

Popular posts from this blog

Runnable vs Callable interfaces in Java

The Runnable interface is the most widely used interface in Java to provide multithreading features, to execute tasks parallelly. The Callable interface is the improvised version of the Runnable interface, this interface is introduced since Java 1.5. Runnable interface: public interface Runnable void run() When to Use Runnable: If the developer just wants to fire any operation and not depending on the result, then we can just use Runnable. Callable Interface returns result from the thread execution or return null. Callable Interface: Public interface Callable V call() throws Exception Computes a result, or throws an exception if unable to do so. The Callable interface works along with Future class, we can get the result using the future object, using get() method. So once we submit thread with the call() method, it will wait until getting the result from the thread.