Posts

Java 8 - Predefined Functional Interface

Predefined Functional Interface are, 1. Predicate 2. Function 3. Consumer 4. Supplier etc., These all are defined the package : java.util.function. Functional Interface: * A interface which contain must only one abstract method. * And It may contain multiple default method and static methods. eg. Comparable, Runnable etc., interface Predicate<T> { boolean test(T t); } test() - abstract method negate() - default method and() - default method or() - default method isEqual() - static method

Java 8 - Static Method Inside interface

Static Method Inside interface: We should call the static method using interface name. Purpose: * Just to define general utility methods. Which is helpful for multiple services. interface Intefer{ public static void m1(){ System.out.println("interface static method") } } class Test implements Interfer{ public static void main(String args[]){ Test t = new Test(); t.m1();  // Wrong statement Test.m1();  // Wrong statement Intefer.m1(); // Right statement } } Overriding concept is not applicable for interface static methods. Because interface static method is not available in the implemented class as per above example. Some of the examples below, interface Intefer{ public static void m1(){ System.out.println("interface static method") } } Case 1: (Valid) class Test implements Interfer{ public static void m1(){ } } Case 2: (Valid) class Test implements Interfer{ public void m1(){ } } Case 3: (V...

Java 8 - Interface with default method VS Abstract Class

Interface with default method: * Every variable is public static final. whether we are decalaring or not. Not possible to declare instance variable. * Never talks about state of object. * We can't declare constructor * We can't declare instance and static block * Functional interface with default methods can be refered lambda expression. * Inside interface we can't override object methods. Abstract Class: * We can declare instance variable. * It can talks about state of object. * We can declare Constructor. * We can declare instance and static block * Abstract class cant refer lambda expression. * Inside Abstract class we can override object methods.

Java 8 - Default Methods in Interfaces

Default Methods in Interfaces: In Java 7, * All methods inside interface are public & abstract if you declare or not. * Every variable inside the interface, public static final if you declare or not. In java 8, * Concrete methods are allowed in the form of default method. Eg. interface Interf{ default void m1(){ System.out.println("default method"); } } Using Default Method: Class Test implements Interf{ public static void main(String args[]){ Test t = new Test(); t.m1(); } } output: default method Override default method: Class Test implements Interf{ public void m1(){ System.out.println("override default method"); } public static void main(String args[]){ Test t = new Test(); t.m1(); } } output: override default method If we are using default method in the interface, when we implements two interfaces with same method name, there might be an issue For example, interface Left{ default void m1(){ System....

Java 8 - Anonymous Inner Class vs Lambda Expression

Image

Java 8 - Anonymous Inner class with Lambda Expression:

Anonymous Inner class with Lambda Expression: Where we are using Anonymous inner class, there may be chance of using Lambda expression. What is Anonymous Inner Class: Class without having any name that is called Anonymous inner class, Where we will use: Eg Java7: Class ThreadDemo{ public static void main (String args[]){ Runnable r = new Runnable(){ public void run(){ for(int i=0;i<10;i++){ System.out.println("Child Thread"); } } }; Thread t = new Thread(r); t.start(); for(int i=0;i<10;i++){ System.out.println("Main Thread"); } } } Java 8: Class ThreadDemo{ public static void main (String args[]){ Runnable r = () -> { for(int i=0;i<10;i++){ System.out.println("Child Thread"); } }; Thread t = new Thread(r); t.start(); for(int i=0;i<10;i++){ System.out.println("Main Thread"); } } Note : Every Anonymous Class can we replace with Lambd...

Java 8 - Lambda Expression for Collections:

Lambda Expression for Collections: Using Lambda expression we can sort the collections datas. ArrayList<Integer> l = new ArrayList<>(); l.add(10); l.add(20); l.add(5); l.add(7); l.add(25); Java 7: Class MyCompartor implements comparator<Interger>{ public int compare(Interger I1, Integer I2){ return (I1 > I2)?-1: (I1 < I2)?1:0); } } Collections.sort(l, new MyComparator()); Java 8: Collections.sort(l, (I1, I2 ) -> (I1 > I2)?-1: (I1 < I2)?1:0 ); TreeSet<Integer> t = new TreeSet<>(); -> Default Natural Sorting. TreeSet<Integer> t = new TreeSet<>( (I1, I2) -> (I1 > I2)?-1:(I1 < I2)?1:0 ); Below is the example to sort the custom class , Class Employee { private int id; private String name; Employee(int id, String name){ this.id = id; this.name =name; } } Java 8 : Class Test{ public static void main(String args[]){ ArrayList<Emp...