Posts

Showing posts from August, 2023

Use cases of Supplier in java 8

Image
      Introduction:     Supplier<T> is a functional interface in the Java ,with one abstract method named get().     Which will return an instance of T. In general we can say Supplier is a factory that keeps on       giving without expecting any input.         Syntax.        Here get method return T instance without expecting any parameter as input.   public abstract T get(); We already know that for any abstract method of functional interface we can   implement using lamda expression.    Eg:  Supplier<Exception> exceptionFactory = () -> new RuntimeException("error");  When we execute above statement  RuntimeException instance  not created by the JVM why?  lamda expressions are lazy until unless we invoke the method of functional interface it will not       evaluated. so when we call  exceptionFactor...

Use case of predefined functional interface called Function in java8

Image
                                     Java8 Function<T,R>  Function<T,R>: Function is a  functional interface .Its having only one abstract method called apply();  like the below Function<T,R> Here apply method takes T is  input and return  R is output. Lamda Expression:   Lambda expressions are nothing but the implementation of the functional interface. We can provide logic to apply method using lambda expression to convert  from T to R like the below                      (T t)-> return R . Use Case : If we are invoking a StudentController from Postman or any Service and passing Student information as json then Controller will receive Student object then we need convert into entity(EStudent)  to persist information into DB. Here Student is model class which is having variable na...