Java 8
package com.java8.functional;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class Test {
// Functional interfaces
// -Predicate
// -BinaryOpeator
// -Function
public static void main(String[] args) {
// -Predicate
Predicate<String> strPrd=(s)->s.equals("baya");
Predicate<String> strPrd1=(s)->s.equals("baya1");
System.out.println(strPrd.or(strPrd1).test("baya1"));
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Predicate<Integer> even=(i)->i%2==0;
System.out.println(list.stream().filter(even).collect(Collectors.toList()));
System.out.println(list.stream().filter(even.negate()).collect(Collectors.toList()));
// -BinaryOpeator
BinaryOperator<Integer> sum=(i,j)->i+j;
System.out.println(sum.apply(10, 20));
System.out.println(list.stream().reduce(sum).get());
System.out.println(list.stream().reduce(BinaryOperator.minBy(Integer::compare)).get());
System.out.println(list.stream() .reduce( Integer::max).get());
// -Function
Function<Integer,Integer> multiplyBy2=(v)->v*2;
System.out.println(multiplyBy2.apply(10));
System.out.println(list.stream().map(multiplyBy2).collect(Collectors.toList()));
List<Emp> empList=new ArrayList<>();
empList.add(new Emp(10,"Baya",1000f,1));
empList.add(new Emp(11,"Baya1",1000f,4));
empList.add(new Emp(12,"Baya2",1000f,3));
empList.add(new Emp(13,"Baya3",1000f,5));
empList.add(new Emp(14,"Baya4",1000f,1));
empList.add(new Emp(15,"Baya5",1000f,6));
empList.forEach(System.out::println);
Predicate<Emp> greaterThan3=(e)->e.grade>3;
Function<Emp,Emp> hikeMapper=(e)->{e.sal=e.sal+e.sal*10/100; return e;};
System.out.println(empList.stream().filter(greaterThan3).map(hikeMapper).collect(Collectors.toList()));
}
/*Developer dev1 = new Developer("jordan", BigDecimal.valueOf(9999));
Developer dev2 = new Developer("jack", BigDecimal.valueOf(8888));
Developer dev3 = new Developer("jaden", BigDecimal.valueOf(10000));
Developer dev4 = new Developer("ali", BigDecimal.valueOf(2000));
Developer dev5 = new Developer("mkyong", BigDecimal.valueOf(1));
List<Developer> list = Arrays.asList(dev1, dev2, dev3, dev4, dev5);
// 1. Create a Comparator
Comparator<Developer> comparing = Comparator.comparing(Developer::getSalary);
// 2. BinaryOperator with a custom Comparator
BinaryOperator<Developer> bo = BinaryOperator.maxBy(comparing);
Developer result = find(list, bo);
System.out.println(result); // Developer{name='jaden', salary=10000}
// one line
// find developer with highest pay
Developer developer = find(list, BinaryOperator.maxBy(Comparator.comparing(Developer::getSalary)));
System.out.println(developer); // Developer{name='jaden', salary=10000}
// find developer with lowest pay
Developer developer2 = find(list, BinaryOperator.minBy(Comparator.comparing(Developer::getSalary)));
System.out.println(developer2); // Developer{name='mkyong', salary=1}
}
public static Developer find(List<Developer> list, BinaryOperator<Developer> accumulator) {
Developer result = null;
for (Developer t : list) {
if (result == null) {
result = t;
} else {
result = accumulator.apply(result, t);
}
}
return result;
}*/
Comments
Post a Comment