Java 8 Functional Interface

Learn about Java 8 functional interface, rules and functional interfaces defined in java 8 part of this tutorial.


Ask your questions in Our Forum

To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

What is Functional Interface

A functional interface is an interface that contains one and only one abstract method. Normally, this method specifies the intended purpose of the interface. Thus, a functional interface typically represents a single action.

In other word Java 8 functional interface permits exactly one abstract method inside.

Functional interfaces can be represented using Lambda Expression, Method Reference, Constructor reference.

For example, the standard interface Runnable is a functional interface because it defines only one method: run( ). Therefore, run( ) defines the action of Runnable. Furthermore, a functional interface defines the target type of a lambda expression. Here is a key point: a lambda expression can be used only in a context in which its target type is specified. One other thing: a functional interface is sometimes referred to as a SAM type, where SAM stands for Single Abstract Method.

Functional Interface Defined in Java 8

Java 8 defines 43 functional interface in-build with java.util.function package. We will see about each functional interface with example program in upcoming chapters.

Functional interfaces provide target types for lambda expressions and method references.

Interface Description
BiConsumer<T,U>
Represents an operation that accepts two input arguments and returns no result.
BiFunction<T,U,R>
Represents a function that accepts two arguments and produces a result.
BinaryOperator<T>
Represents an operation upon two operands of the same type, producing a result of the same type as the operands.
BiPredicate<T,U>
Represents a predicate (boolean-valued function) of two arguments.
BooleanSupplier
Represents a supplier of boolean-valued results.
Consumer<T>
Represents an operation that accepts a single input argument and returns no result.
DoubleBinaryOperator
Represents an operation upon two double-valued operands and producing a double-valued result.
DoubleConsumer
Represents an operation that accepts a single double-valued argument and returns no result.
DoubleFunction<R>
Represents a function that accepts a double-valued argument and produces a result.
DoublePredicate
Represents a predicate (boolean-valued function) of one double-valued argument.
DoubleSupplier
Represents a supplier of double-valued results.
DoubleToIntFunction
Represents a function that accepts a double-valued argument and produces an int-valued result.
DoubleToLongFunction
Represents a function that accepts a double-valued argument and produces a long-valued result.
DoubleUnaryOperator
Represents an operation on a single double-valued operand that produces a double-valued result.
Function<T,R>
Represents a function that accepts one argument and produces a result.
IntBinaryOperator
Represents an operation upon two int-valued operands and producing an int-valued result.
IntConsumer
Represents an operation that accepts a single int-valued argument and returns no result.
IntFunction<R>
Represents a function that accepts an int-valued argument and produces a result.
IntPredicate
Represents a predicate (boolean-valued function) of one int-valued argument.
IntSupplier
Represents a supplier of int-valued results.
IntToDoubleFunction
Represents a function that accepts an int-valued argument and produces a double-valued result.
IntToLongFunction
Represents a function that accepts an int-valued argument and produces a long-valued result.
IntUnaryOperator
Represents an operation on a single int-valued operand that produces an int-valued result.
LongBinaryOperator
Represents an operation upon two long-valued operands and producing a long-valued result.
LongConsumer
Represents an operation that accepts a single long-valued argument and returns no result.
LongFunction<R>
Represents a function that accepts a long-valued argument and produces a result.
LongPredicate
Represents a predicate (boolean-valued function) of one long-valued argument.
LongSupplier
Represents a supplier of long-valued results.
LongToDoubleFunction
Represents a function that accepts a long-valued argument and produces a double-valued result.
LongToIntFunction
Represents a function that accepts a long-valued argument and produces an int-valued result.
LongUnaryOperator
Represents an operation on a single long-valued operand that produces a long-valued result.
ObjDoubleConsumer<T>
Represents an operation that accepts an object-valued and a double-valued argument, and returns no result.
ObjIntConsumer<T>
Represents an operation that accepts an object-valued and a int-valued argument, and returns no result.
ObjLongConsumer<T>
Represents an operation that accepts an object-valued and a long-valued argument, and returns no result.
Predicate<T>
Represents a predicate (boolean-valued function) of one argument.
Supplier<T>
Represents a supplier of results.
ToDoubleBiFunction<T,U>
Represents a function that accepts two arguments and produces a double-valued result.
ToDoubleFunction<T>
Represents a function that produces a double-valued result.
ToIntBiFunction<T,U>
Represents a function that accepts two arguments and produces an int-valued result.
ToIntFunction<T>
Represents a function that produces an int-valued result.
ToLongBiFunction<T,U>
Represents a function that accepts two arguments and produces a long-valued result.
ToLongFunction<T>
Represents a function that produces a long-valued result.
UnaryOperator<T>
Represents an operation on a single operand that produces a result of the same type as its operand.

@interface FunctionalInterface

Java 8 has @FunctionalInterface annotation used for compilation errors, if interface with @FunctionalInterface violates the contract of exactly one abstract method Error: (Invalid ‘@FunctionalInterface’ annotation; Java8FunctionalInterface is not a functional interface)

  1. Its not mandatory to use @FunctionalInterface annotation for Functional Interfaces. We can define Functional Interface without @FunctionalInterface annotation. Its only instruct the compiler to enforce single abstract method.
  2. Default method in java 8 not abstract. So we can use default method in functional interface.
  3. Check below interface which has methods others than abstract method but interface still valid functional interface.

Java8FunctionalInterface.java

package com.ehowtonow.java8;

@FunctionalInterface
public interface Java8FunctionalInterface {

   public void helloJava8();

    @Override
    public String toString();                //Overridden from Object class
 
    @Override
    public boolean equals(Object obj);   

   default void helloJava(){
        System.out.println("Java 8 Default method");
    }
}

We will see more functional interface examples in upcoming chapters.


Ask your questions in Our Forum

To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

Posts You may Like

  1. How to Create Pretty Print JSON using Jackson
  2. Sending Simple Email Using Java Mail API
  3. How to find last occurrence of substring inside string
  4. How to delete directory using Java
  5. JAX-RS @QueryParam with @DefaultValue – Jersey
  6. How to get the size of Collection
  7. How to traverse the directories using Java
  8. How to Read JSON using GSON Stream
  9. How to convert List into Array
  10. Java 8 Stream Collectors groupingBy with sort Example
  11. First Java Application in Eclipse
  12. Email Receiver using Java Mail API
  13. Java Mail API – Introduction
  14. Java Tutorial – Introduction to Java Virtual Machine (JVM)
  15. XML SAX Parser Introduction

Be the first to comment

Leave a Reply

Your email address will not be published.


*