Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Friday, July 17, 2015

Java 8 : Method reference

This post more deep into demonstrate about Method reference.  This code is contains following three functional features of Method reference.
  • Reference to constructor (1)
  • Reference to Static method (2)
  • Reference to Instance Method of particular object (3)
Note : The color codes refers with numbers are used in the comments. to make notes clear

I would not say this program is self explanatory. Comments are provided in the major three lines.
Explanation goes here,

Reference to Instance Method of particular object (3)
The "MyComparator" class have override method and instance method too. Collection sort functionality expect compactor object to process the provided list input. So, Created "MyComparator" class instance and supplied to the sort functionality.

Reference to Static method (2)
As like instance method we can call the static methods in the reference. "doPrint" also has the static method reference. This method contain collection forEach also time being skip this content now. It will be covered in later posts.

Reference to constructor (1)
Constructor also calling in the lambda expression way. In case of multiple constructor available, the smarty will recognize by the suitable parameter. 

Hint : Try to execute the below code, play with it, change something. then you will surely understand the concept. Method reference major concept are covered in a single program. It is useful for reference while doing other task.
----------------------------------------------- Java 8------------------------------------------------------------------
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ComparatorMethodReference {
List<String> l;
public ComparatorMethodReference(List<String> l) {
this.l=l;
}
public static void main(String[] args) {
List<String> l = new ArrayList<String>();

l.add("Banana");
l.add("Apple");
l.add("Mango");

Func fun=ComparatorMethodReference::new; // (1) Reference by constructor
ComparatorMethodReference comp=fun.loadList(l);
comp.doJava8Sort();
}

public static void doPrint(List<String> l) {
l.forEach(System.out::println); // (2) Reference to Static method
System.out.println(); // For Empty line
}

public void doJava8Sort() {
MyComparator comparator=new MyComparator();
Collections.sort(l,comparator);
doPrint(l);
Collections.sort(l,comparator::myReverse); // (3) Reference to Instance Method of particular object
doPrint(l);
}
}
class MyComparator implements Comparator<String> {
@Override
public int compare(String st1, String st2) {
return st1.compareTo(st2);
}
public int myReverse(String st1,String st2)
{
return st2.compareTo(st1);
}
}

@FunctionalInterface
interface Func {
ComparatorMethodReference loadList(List<String> l);
}

Java 8 : Lambda expression

Lambda expression is a key point to achieve functional programming. It facilitate to make your code more concise. Java developer aware of anonymous class. Now taste the anonymous methods. This can be created using lambda expressions.
-------------------------------------------------------Java 8--------------------------------------------------------
package com.ran;

@FunctionalInterface
interface ExpressionHolder {
int eval(int a,int b);
}
public class LamdaExpressionInJava8 {
//Functional interface
public static void main(String[] args) {
ExpressionHolder addExpression=(a1,a2)-> a1 + a2;
System.out.println(addExpression.eval(1, 2));
}
}
-----------------------------------------------------------------------------------------------------------------------
From the above code the high lighted portion is sample for lambda expression.
Here ExpressionHolder is a interface which has apply method in it. Int the case of  traditional way of java development would go with implementation class for ExpressionHolder or anonumous class. Here the complete implementation has covered in the lambda expression. This is inline implementation of the functional interface.

Method parameters --> functional body

For comparison purpose I have given the java 7 code also with the sample model. This code may help to understand the lambda expression in best.
Anonumous class implemenation of ExpressionHolder has been exhibited here.
----------------------------------------------------Java 7----------------------------------------------------
package com.ran.java7;

interface ExpressionHolder {
int apply(int a,int b);
}
public class LamdaExpressionInJava7 {
//Functional interface
public static void main(String[] args) {
ExpressionHolder addExpression=new ExpressionHolder() {
@Override
public int apply(int a, int b) {
return a+b;
}
};
System.out.println(addExpression.apply(1, 2));
}
}
----------------------------------------------------------------------------------------------------------------

Wednesday, July 15, 2015

Does Java 8 support multiple inheritance

This thought were brought in my mind , after reading the default method / defender method concept.
     The default method is useful, when situation comes to introduce new method or functionality in the existing interface.
     In the previous version of java, new method can not be pushed in the already delivered interface. Because It leads to rework on all the implementation classes. Default Method will have there own default implementation on the interface. So , it stop us from the re implementation.

     Every one aware of that , Java does not support multiple inheritance directly. As developer we cooked this design with help of interface. So, multiple interface can be implemented in one class. Interface had stopped the presence of diamond problem.

     Situation has changed now. You can have implementation in the interface using default method. So, if we have more than one interface implemented in a single class , what will happen?

You can prioritize the calling order of the interface. It could be more text based explanation, will see code here.

package com.ran;
interface Car{
default void engine(){ System.out.println("Car Engine implemenation");
}
}
interface Boat{
default void engine(){
System.out.println("Boat Engine implemenation");
}
}
public class JamesBondSuperCar implements Car,Boat {
public void engine() { Car.super.engine();
Boat.super.engine();
} public static void main(String[] args) {
JamesBondSuperCar bond=new JamesBondSuperCar();
bond.engine();
}
}
The above code is self explanatory for JamesBondSuperCar. It has mixed of the boad and car functionality to drive the special effect on screen. Ya.! It helped here too.

Merging two sorted arrays - Big O (n+m) time complexity

 Problem :  Merge the two sorted arrays. Edge case :  Array can empty Arrays can be in different size let getMaxLength = ( input1 , input...