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.


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