Functional Interfaces

A functional interface is an interface that contains only one abstract method. They can have only one functionality to exhibit. In java , lambda expressions can be used to represent the instance of a functional interface. Each functional interface has a single abstract method, called functional method for that functional interface, to which the lambda expression’s parameter and return types are matched or adapted.

We have number of interfaces in java.util.function package , for example, Function, Consumer , Supplier , BiPredicate , And a lot more.

Let’s Discuss What they are?

  1. Function<T,R> → This functional interface represents a function that accepts one argument and produces a result.
Image for post
Image for post
Function interface

Above, a basic example here we make a

Function with T=Integer means the input parameter type is integer and R= output parameter type is also integer. They can be different also.

→incrementByOneFunction is you can say your lambda expression name or variable name.

→functioning is more concise and more clear than normal function

Here number is the parameter we pass during calling . When we call incrementByOneFunction we use .apply(){ it is method provided by the interface which accepts only one value}. Now 4, is the parameter which get assigned to the number and after execution it prints 5 as an output.

2.Predicate<T> →A predicate is a statement that may be true or false depending on the values of its variables. It can be thought of as a function that returns a value that is either true or false.

Image for post
Image for post
Predicate

Using .test method Predicate only takes a statement and check whether the conditions applied are true or false.

3.Consumer<T> →This functional interface represents an operation that accepts a single input argument and returns no result.

Image for post
Image for post
Consumer

It only accepts value using .accept method . The real outcome is the side-effects it produces. Since it’s a functional interface, you can pass a lambda expression wherever a Consumer is expected. There is one more method andThen (used for chaining of functions).

So, if you update the name from shivanya to aarti , then the side -effect of this is it shows Aarti on the console.

4. Supplier →This functional interface does the opposite of the Consumer, it takes no arguments but it returns some value.

Supplier

It’s main purpose is to supply values where required. You can supply different values.

==> Most of them have BiFunctions also which means they accepts two areguments.

We have some other interfaces also — —

You can read about them here → https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html

thanks for giving your valuable time..

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store