5- Predefined Functional Interfaces Theory and Basics

 

PREDEFINED   FUNCTIONAL   INTERFACES


What are Predefined Functional Interfaces?

Predefined Functional Interfaces include most commonly used methods which are available to a programmer by default.


Why do we use predefined functional interfaces?

 In our day to day programming many times we come across re-occurring functionalities to be developed.

In that case, we can utilize the predefined functional interfaces instead of creating our own every time. They will obviously save our development time and minimize chances of mistakes.


Types of Predefined Functional Interfaces:


Consumer<T>

Consumer<T> is used when we have to provide some input parameter, perform certain operation, but don’t need to return anything.

Moreover, we can use Consumer to consume object and perform certain operation. 


interface Consumer<T> {

   void accept(T t);

}


Here, T-> Input parameter type
          t-> Input argument


Consumer interface is internally used by forEach  loop.

-----------------------------------------------------------------------------------------

Predicate<T>


We can use Predicate<T> to implement some conditional checks.


public interface Predicate<T> {

    boolean test(T t);

}


Here, T-> Input parameter type
          t-> Input argument


Predicate interface is internally used by filter method.

--------------------------------------------------------------------------------------------

Supplier<R>


Supplier<R> doesn’t take any input and it always returns some object.


 We use it when we need to get some value based on some operation like supply Random numbers, supply Random OTPs, supply Random Passwords etc. 


interface Supplier<R>{

    R get();

}


Here, R-> Return Type
      


Supplier interface is internally used by orElseGet method

-------------------------------------------------------------------------------------------------------------

Image 1Table for Predefined Functional Interface










Comments

Popular posts from this blog

INDEX OF JAVA 8 and MICROSERVICES BLOG