Posts

Showing posts from January, 2019

Functional Interface and Lambda Expression

Functional Interface and Lambda Expression Functional Interfaces A functional interface is an interface that contains only one abstract method. It can have one or more default or static method but abstract method should be only one. Runnable , Callable, ActionListener , Comparable are some of the examples of functional interfaces as it contains only one abstract method. Let’s see some functional Interface implementation example: Example 1: public interface SimpleInterface {     public void abstractMethod(); } It’s a functional Interface as it only contains a single abstract method. Example 2: public interface ComplexInterface {     public void abstractMethod();     public default void defaultMethod(){         System.out.println(“Default method”);     }     public static void staticMethod(){         System.out.println(“Static me...

How to write Lambda Expression in Java 8

   How to write Lambda Expression? With the introduction of Lambda Expression in JAVA 8, We can write more readable, maintainable and concise code but I have seen people keep forgetting the syntax of lambda expression or they are always confused with it. In this blog, I ll just try to explain the basic things i.e. how easy is it to write Lambda Expression but before starting with the syntax, Let’ see what is lambda Expression? In simple term, Lambda expression (->) is an anonymous function which     - doesn’t have any name     - doesn’t have any modifiers     - doesn’t have any return type. To understand it in a more easy way, Let’s first write a simple Java function which display some message on the console. Example 1: Display Message public void display (){      system.out.println(“Learning Lambda Expression”);  }  Now to convert the above code into lambda expression, read the lambda definition once aga...

Java 8 - Type Inference

Image
Java 8 – Type Inference Type inference was first introduced in JDK 7 and later improved in JDK 8. It is a feature of Java which provides ability to compiler to look at each method invocation and corresponding declaration to determine the type of argument(s). The inference algorithm checks the types of the arguments and,if available, assigned type is returned. It tries to find a specific type which can full fill all type parameters. Till Java 6, We have to declare an Arraylist by mentioning its type explicitly at both side.             List<Integer> numbers = new ArrayList<Integer>(); and if we keep the type as blank at right side then Compiler generates unchecked conversion.             List<Integer> numbers = new ArrayList<>(); With JDK 7, It got improved and we started mentioning the type of arraylist only at one side. Below we can left second side as blank diamond and compil...