Tuesday, 9 April 2019

Comparator Example using Java 8

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

class MyComp implements Comparator<Integer>
{

@Override
public int compare(Integer o1, Integer o2) {
// TODO Auto-generated method stub

return (o1<02)?-1:(o1>o2)?1:0;

//Into Lamda Expression


/*if(o1<o2)
{
return -1;
}
else if(o1>o2)
{
return 1;
}

else
{
return 0;
}*/

}


}
 class TestDurga {

//Older Version
public static int square(int n)
{
return n*n;
}

public static void main(String[] args) {

//Concise Code Java 8
Function<Integer,Integer> f=i->i*i;
Predicate<Integer> p=i->i%2==0;
System.out.println("4 is "+square(4));
System.out.println("5 is "+f.apply(5));
System.out.println("4 is even "+p.test(4));
System.out.println("5 is even "+p.test(5));
ArrayList<Integer> testList = new ArrayList<Integer>();
testList.add(5);
testList.add(15);
testList.add(20);

testList.add(0);
System.out.println(testList);
Comparator< Integer> c = (o1,o2)-> (o1<o2)?-1:(o1>o2)?1:0;
// OLD Implementation With Out Lamda
//Collections.sort(testList,new MyComp() );
//With Lamda
Collections.sort(testList,c );
System.out.println("After sorting "+testList);
testList.stream().forEach(System.out::println);
List<Integer> finalEvenNumberList=testList.stream().filter(i->i%2==0).collect(Collectors.toList());
System.out.println(finalEvenNumberList);
}

}

Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format   let map = new Map<string, string>()      map.set(this.ge...