Square CSS

Monday, May 25, 2020

Supplier : Functional Interface Java 8

Introduction


In this tutorial we will look at the use of Supplier a functional interface introduced in java 8.

Refer The video Tutorial for detail


 

Use Case 


Supplier is a functional interface which is used to return data without accepting anything as parameter.The sample code is shown below.




    //this accepts an argument & return a result. Supplier<String> supplier=new Supplier<String>() { @Override public String get() { return "Data from supplier functional interface "; } };


The above case Supplier will not receive anything but return the result. The function is returning a String.


Conclusion 


In this tutorial we saw the use case of functional interface - Supplier. which is introduced in java 8 to act as supplier without receiving any parameter. 

Function : Functional Interface Java 8

Introduction


In this tutorial we will look at the use of Function a functional interface introduced in java 8.

Refer The video Tutorial for detail


 

Use Case 


Function is a functional interface which accepts one argument and returns another.The sample code is shown below.


    //this interface accepts one argument and return one result. Function<Integer,String> function=new Function<Integer,String>() { @Override public String apply(Integer t) { return "The data is: "+t; } }


The above case Function will receive one argument and return one result. The function is receiving Integer t and returning a String.

Conclusion 


In this tutorial we saw the use case of functional interface - Function. which is introduced in java 8 as request response perimeter.

Consumer : Functional Interface Java 8

Introduction


In this tutorial we will look at the use of Consumer a functional interface introduced in java 8.

Refer The video Tutorial for detail


 

Use Case 


Consumer is a functional interface which is used to modify value based on some data.The sample code is shown below.



    //return type is void Consumer<Integer> consumer=new Consumer<Integer>() { @Override public void accept(Integer i) { System.out.println("Nothing is returned in case of consumer"); } }


The above case will not return anything it will just consume some data to process it.

Conclusion 


In this tutorial we saw the use case of functional interface - Consumer. which is introduced in java 8 to modify data by consuming it in response.

Predicate : Functional Interface Java 8

Introduction


In this tutorial we will look at the use of Predicate a functional interface introduced in java 8.

Refer The video Tutorial for detail




Use Case 


Predicate is a functional interface which is used for returning boolean value based on some data.The sample code is shown below.



    //return boolean value Predicate<String> pred=new Predicate<String>() { @Override public boolean test(String str) { return str.length()>10; } }


The above case will return true if the string passed to the method will have length grater than 10.

Conclusion 


In this tutorial we saw the use case of functional interface - Predicate. which is introduced in java 8 to get the boolean result.

Java 8 : Default Method In Interface

Introduction


Java 8 comes with use of default method in an interface. Multiple Interfaces can have the same default method.

Refer The Video For More Detail




Uses of Default Method


We can create a method with default keyword and the method will be referred from the interface.


  public interface DefaultInterface{
public default void getDefault() {
        //Custom Code
      }
  }


In case if two interface have same default method then at the time of compilation only the editor will ask for the implementation of default method. 



  public interface DefaultInterface1 {
public default void getDefault() {
               System.out.println("Default Interface1");
       }
  }


public interface DefaultInterface2 {
public default void getDefault() {
                System.out.println("Default Interface2");

       }
  }


When a class implements the above two interface then the user needs to override the default method of either of the interface. This will remove the diamond problem which occurs in multiple inheritance.



  public class ImplementInterfaceWithDefaultMethod
implements DefaultInterface1,DefaultInterface2{

    @Override
   public void getDefault() {
DefaultInterface1.super.getDefault();
        
   }


Conclusion


The default method can be created inside the interface since Java 8. It can be used by calling the Interface with its default method using super keyword.


Java 8 : Static Method In Interface

Introduction


Java 8 comes with use of static method in an interface. Prior to Java 8 static method comes only on class level.

Refer The Video For More Detail



Uses of Static Method


We can create a method with static keyword and the method will be referred from the interface.


  public interface InterfaceWithStaticMethod {

public static void getStaticData() {
System.out.println("Data from static method of interface !!!");
}

}






  public class MainClass {

InterfaceWithStaticMethod.getStaticData();

}











The static method can be called in the class just by calling with interface having static method.











Conclusion


The static method can be created inside the interface since Java 8. It can be used by calling the Interface with its static method.


@EnableScheduling : Create Scheduler In Spring

Introduction


We can create scheduler in spring using @EnableScheduling and @Scheduler in Spring based application.


Refer Video Tutorial For Detail :





@EnableScheduling


@EnableScheduling is annotated on class level to configure class for scheduling jobs in Spring.
This annotation is used along with @configuration for configuring scheduler inside the class as method. 


  @Configuration
  @EnableScheduling //to enable scheduling in class
public class MyScheduler{
        //Custom Code
  



@Scheduler


@Scheduler is used on the method level to run method at scheduled time. The annotation takes parameter to configure time for scheduling. This can be done using cron expression or using delay functions provided in spring.



  //annotate with scheduler
  @Scheduled(cron="*/2 * * * * *")//for every 2 seconds  
public void jobForScheduler() {
        //Custom Code to execute every 2 seconds
  


Conclusion


To enable scheduling in Spring we need to annotate class with @EnableScheduling and @configuration. The method to be considered for job to be annotated with @Scheduled

@EnableCaching : Maintain Cache In Spring


Introduction : 

We can enable cache in spring by the use of @EnableCaching and @Cachable.


Refer Video Tutorial For Detail :




@EnableCaching


The annotation @EnableCaching should be used on the class level to configure the class for enabling cache. This annotation should be used to make it configurable for cache maintenance.



  @EnableCaching //provided annotation on class level
  public class CachingDemo{
         //Custom Code
  }


@Cachable

The annotation @Cachable should be used above the methods which needs to maintain cache.
Once the request is complete the response is stored in cache for same request coming in future.
The response is mapped to the key for further requests. 


@Cacheable("userInfo") //provided cacheable annotation along with userInfo as key
public String checkCache() { //Custom Code }


Conclusion


Major annotation to be used for caching in spring is @EnableCaching and @Cachable. The key should be provide where the cached is used so that the cache data should be mapped to the key.

Sunday, May 24, 2020

@Lookup : Annotation In Spring




Introduction :


@Lookup tells spring to return the instance of the return type of method.
In other words whatever be the return type of method, it's instance will be returned.

Use case :


In situations where a prototype bean needs to be injected in a singleton @Autowired won't work.
In such situations @Lookup is helpful.

Example :


Suppose we have a prototype bean shown below.



@Component @Scope("Prototype")
public class CheckPrototype{
//Some Code }
   




If we want above bean in some singleton class then instead of @Autowired the below code will work.



@Lookup//to handle prototype in singleton public CheckPrototype getCheckPrototype() { // return new Instance }
     

Limitations :


1. @Lookup won't work for static, final and private fields
2. @Lookup wont work if the bean class is final.

Some Algorithms

Algorithm: Tower of Hanoi

Tower of Hanoi consists of three towers called as pegs with n number of rings. Rings are of different size.  Conditions to be fulfill...

Popular Posts