73. What is a functional interface?

What is a functional interface?

  • It’s created in Java 8 to support functional programming
  • An functional Interface is an interface with only one abstract method but it can have multiple static methods or default methods.
  • We can use lamda expression to create an instance of interface.
  • There are some predefined functional interface in Java 8:
  • Predicate<T>:

+ Represent predicate of a one argument

+ It has one abstract method name Test have one parameter and return a boolean value.

+ It’s used in filter or remove If method in Stream API

  • Supplier<T>

+ It represents for supplier of result

+ It has one abstract method named get (). That method doesn’t have any parameter, but it returns a generic value.

+ It’s used in generate method in Stream API or apply for factory design pattern

  • Function<T, R>

+ It represents of a function that accepts one argument and produces a result

+ It has one abstract method name apply. That method has one parameter and it returns a value.

+ It’s used in map () method in Stream API

  • Consumer<T>

+ It represents for an operation that accept single input and return no value

+ It has one abstract method named accept have one parameter but return no value

+ It’s often used in the For Each method in Stream API.

Leave a Reply

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