Guest User

Untitled

a guest
Jun 23rd, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. public class Java8 {
  2.  
  3. // made it static so it can be called from main
  4. private static int addOne(int value) {
  5. return ++value;
  6. }
  7.  
  8. public static void main(String[] args) {
  9.  
  10. // ==================== Functional interfaces and lambdas ====================
  11.  
  12. /**
  13. * Predicate => takes one argument and returns a boolean
  14. * Consumer => accepts a single argument with no return value (accept)
  15. * Function => accepts a single argument and produces a result (apply)
  16. * Supplier => represents a supply of results
  17. * UnaryOperator => single argument with a return value
  18. * BinaryOperator => takes teo arguments and returns one
  19. */
  20.  
  21.  
  22. /*
  23. Predicates are Boolean valued functions of one argument meaning they take in one argument, use a test method to evaluate it and return either true or false.
  24. */
  25. Predicate<String> stringLen = (s)-> s.length() < 10;
  26. System.out.println(stringLen.test("Apples") + " - Apples is less than 10");
  27.  
  28.  
  29. /*
  30. The Consumer interface consumes the argument. It accepts a single argument and does not return a result.
  31. Consumer uses accept method
  32. */
  33. Consumer<String> consumerStr = (s) -> System.out.println(s.toLowerCase());
  34. consumerStr.accept("ABCDefghijklmnopQRSTuvWxyZ");
  35.  
  36. /*
  37. Function which transforms a value from one type to another.
  38. It accepts one argument and produces a result.
  39. */
  40. Function<Integer,String> converter = (num)-> Integer.toString(num);
  41. System.out.println("length of 26: " + converter.apply(26).length());
  42.  
  43. /*
  44. Supplier supplies a value.
  45. It produces a result of a given type.
  46. Unlike Functions, Suppliers do not accept arguments but they do return a result.
  47. */
  48. Supplier<String> s = ()-> "Java is fun";
  49. System.out.println(s.get());
  50.  
  51. /*
  52. BinaryOperator interface takes two arguments and returns one
  53. */
  54. BinaryOperator<Integer> add = (a, b) -> a + b;
  55. System.out.println("add 10 + 25: " + add.apply(10, 25));
  56.  
  57. /*
  58. UnaryOperator interface takes a single argument and returns a single value.
  59. */
  60. UnaryOperator<String> str = String::toUpperCase;
  61. System.out.println(str.apply("This is my message in upper case"));
  62.  
  63. /*
  64. Output:
  65. true - Apples is less than 10
  66. abcdefghijklmnopqrstuvwxyz
  67. length of 26: 2
  68. Java is fun
  69. add 10 + 25: 35
  70. THIS IS MY MESSAGE IN UPPER CASE
  71. *
  72. *
  73. * */
  74.  
  75.  
  76. // BiFunction< 1ST PARAM TYPE, 2ND PARAM TYPE, RETURN TYPE>
  77. BiFunction<String, String, Integer> biFunction = (a, b) -> Integer.valueOf(a) + Integer.valueOf(b);
  78. System.out.println(biFunction.apply("2", "2"));
  79.  
  80. Greeting greet = name -> System.out.println("Hi, " + name);
  81. greet.sayHi("GG");
  82.  
  83. Calculator addition = (a,b) -> a + b;
  84. Calculator substraction = (a,b) -> a - b;
  85. Calculator division = (a,b) -> (b != 0 ? a / b : 0);
  86. Calculator multiplication = (a,b) -> a * b;
  87.  
  88. // ==================== Method references ====================
  89.  
  90. IntFunction<String> intToString = num -> Integer.toString(num);
  91. System.out.println("expected value 3, actual value: " +
  92. intToString.apply(123).length());
  93.  
  94. //static method reference using ::
  95. IntFunction<String> intToString2 = Integer::toString;
  96. System.out.println("expected value 4, actual value: " +
  97. intToString2.apply(4567).length());
  98.  
  99. //lambdas made using a constructor
  100. Function<String, BigInteger> newBigInt = BigInteger::new;
  101. System.out.println("expected value: 123456789, actual value: "+
  102. newBigInt.apply("123456789"));
  103.  
  104. //example of a lambda made from an instance method
  105. Consumer<String> print = System.out::println;
  106. print.accept("Coming to you directly from a lambda...");
  107.  
  108. //these two are the same using the static method concat
  109. UnaryOperator<String> greeting = x -> "Hello, ".concat(x);
  110. System.out.println(greeting.apply("World"));
  111.  
  112. UnaryOperator<String> makeGreeting = "Hello, "::concat;
  113. System.out.println(makeGreeting.apply("Peggy"));
  114.  
  115. Function<Integer, Integer> func = Test::addOne;
  116. // same as : Function<Integer, Integer> func = (value) -> addOne(value);
  117. System.out.println(func.apply(1));
  118.  
  119.  
  120. // ==================== Collection API ====================
  121.  
  122. /*
  123.  
  124. Set - a collection that does not contain duplicates
  125. List - an ordered collection based on the way the user entered the data
  126. Map - an object that maps keys to values.
  127.  
  128. Streams operations are either intermediate or terminal
  129. */
  130. }
  131.  
  132. @FunctionalInterface
  133. interface Greeting {
  134. void sayHi(String name);
  135. }
  136.  
  137. @FunctionalInterface
  138. interface Calculator {
  139. int calc(int a, int b);
  140. }
  141.  
  142. }
Add Comment
Please, Sign In to add comment