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);
}
}
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);
}
}
No comments:
Post a Comment