Over

120,000

Worldwide

Saturday - Sunday CLOSED

Mon - Fri 8.00 - 18.00

Call us

 

Java Functional Interfaces

Java Functional Interfaces

This section describes the concept of Functional Interfaces introduced in Java 8.

Introduction

 An interface that contains that contains exactly one abstract (unimplemented) method is known as functional interface.
 These interfaces are also called Single Abstract Method (SAM) interfaces.
 A functional interface can contain implementations by declaring multiple default and static methods.

Example:

public interface MyFirstFunctionalInterface {

  void test1();

}

.

@FunctionalInterface Annotation

.

 @FunctionalInterface annotation is used to indicate that the interface is a Functional Interface.
 The compiler gives an error if an interface with this annotation contains more than one abstract method.
 It is not mandatory to use this annotation, but it’s recommended to signal to other developers the intent of the use of the interface.

.

Examples:

@FunctionalInterface

public interface MyFirstFunctionalInterface {

  void test1();

  void test2(); //error

}

.

Error: No abstract method declared

@FunctionalInterface
public interface Foo {
static void foo() {
System.out.println(“Foo.foo”);
}


default void fooBar() {
System.out.println(“Foo.fooBar”);
}

}

.

@FunctionalInterface

@FunctionalInterface
public interface Foo {
static void foo() {
System.out.println(“Foo.foo”);
}


static void bar() {
System.out.println(“Foo.bar”);
}


default void fooBar() {
System.out.println(“Foo.fooBar”);
}


default void barFoo() {
System.out.println(“Foo.barFoo”);
}

}


}

}

.

Built-in Functional Interfaces

.

Java provides some built-in functional interfaces that are defined in java.util.Function package.

.

 Function: The Function interface has an abstract method that accepts one argument of type T and returns a result of type R.

.

public interface Function<T,R>

{

  R apply(T t);

}

.

Example:

This implementation of Function interface returns the Integer length of the String value passed as argument.

.

Function<String, Integer> myFunc = str -> str.length();

Integer len = myFunc.apply(“MyFirstFunction”);  //15

.

 Predicate: The Predicate interface has an abstract method that returns Boolean (true or false) value as result.

.

public interface Predicate

{

  boolean test(T t);

}
.

Example:

This implementation of Predicate interface tests the value passed as argument and returns true and false based on its nullity.

.

Predicate isNull = (value) -> value!=null;

.

 BinaryOperator: The BinaryOperator interface has an abstract method that accepts two arguments and returns result of same type.

.

public interface BinaryOperator

{

  T apply(T x, T y);

}

.

Example:

This implementation of BinaryOperator interface returns the sum of two values passed as arguments.

.

BinaryOperator<AddNum> binaryOp = (num1, num2) ->

{ num1.add(num2);

return num1;

};

.

 UnaryOperator: The UnaryOperator interface has an abstract method that accepts one argument and returns result of same type.

.

public interface UnaryOperator

{

  T apply(T t);

}

.

Example:

This implementation of UnaryOperator interface modifies the object passed as argument and returns it.

.

UnaryOperator<Employee> unaryOp = (employee) ->

{ employee.phone = 9999999999;

return employee;

};

.

 Supplier: The Supplier interface has an abstract method that accepts no arguments and returns (supplies) result of some type.

.

public interface Supplier<T>

{

  T get();

}

.

Example:

This implementation of Supplier interface returns a new Integer instance with a random value.

.

Supplier<Integer> supplier = () -> new Integer((int) Math.random());

.

 Consumer: The Consumer interface represents a method that accepts (consumes) one argument and performs operation on the argument, without returning a value.

.

public interface Consumer<T>

{

  void accept(T t);

}

.

Example:

This implementation of Consumer interface prints the value passed as argument.

.

Consumer<String> consumer = (value) -> System.out.println(value);

Leave a Reply

Your email address will not be published. Required fields are marked *

Working Hours

  • Monday 9am - 6pm
  • Tuesday 9am - 6pm
  • Wednesday 9am - 6pm
  • Thursday 9am - 6pm
  • Friday 9am - 6pm
  • Saturday Closed
  • Sunday Closed