ClavinJune

Java Functional

Jul 1st, 2018
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.18 KB | None | 0 0
  1. package asd;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.List;
  5. import java.util.function.BinaryOperator;
  6. import java.util.function.Function;
  7.  
  8. //FunctionalInterface
  9. interface Math{
  10.     public int add(int x, int y);
  11. }
  12. interface Sort{
  13.     public int reversed(String x, String y);
  14. }
  15. public class Main {
  16.     public Main() {
  17.         List<String> names = new ArrayList<>();
  18.         names.add("Clavianus Juneardo");
  19.         names.add("Vincent Colin");
  20.         names.add("Gregory James");
  21.         names.add("Jonathan Ramlie");
  22.         names.add("Billy Purnama");
  23.        
  24.         // Functional Interface
  25.         Math math = (int x, int y) -> x + y;
  26.         System.out.println(math.add(2, 2));
  27.         Sort sort = (String x, String y) -> y.compareTo(x);
  28.         Collections.reverse(names);
  29.        
  30.         //Lambda Expression
  31.         Function<Integer, Integer> pow = x -> x*x;
  32.         System.out.println(pow.apply(2));
  33.        
  34.         BinaryOperator<Integer> add = (x,y) -> x + y;
  35.         System.out.println(add.apply(2,2));
  36.        
  37.         //parallel function
  38.         names.stream()
  39.             .sorted( (x,y) -> sort.reversed(y,x) )
  40.             .filter( name -> !name.equals("Clavianus Juneardo") )
  41.             .forEach( System.out::println );
  42.     }
  43.  
  44.     public static void main(String[] args) { new Main(); }
  45. }
Add Comment
Please, Sign In to add comment