Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Java8 {
- // made it static so it can be called from main
- private static int addOne(int value) {
- return ++value;
- }
- public static void main(String[] args) {
- // ==================== Functional interfaces and lambdas ====================
- /**
- * Predicate => takes one argument and returns a boolean
- * Consumer => accepts a single argument with no return value (accept)
- * Function => accepts a single argument and produces a result (apply)
- * Supplier => represents a supply of results
- * UnaryOperator => single argument with a return value
- * BinaryOperator => takes teo arguments and returns one
- */
- /*
- 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.
- */
- Predicate<String> stringLen = (s)-> s.length() < 10;
- System.out.println(stringLen.test("Apples") + " - Apples is less than 10");
- /*
- The Consumer interface consumes the argument. It accepts a single argument and does not return a result.
- Consumer uses accept method
- */
- Consumer<String> consumerStr = (s) -> System.out.println(s.toLowerCase());
- consumerStr.accept("ABCDefghijklmnopQRSTuvWxyZ");
- /*
- Function which transforms a value from one type to another.
- It accepts one argument and produces a result.
- */
- Function<Integer,String> converter = (num)-> Integer.toString(num);
- System.out.println("length of 26: " + converter.apply(26).length());
- /*
- Supplier supplies a value.
- It produces a result of a given type.
- Unlike Functions, Suppliers do not accept arguments but they do return a result.
- */
- Supplier<String> s = ()-> "Java is fun";
- System.out.println(s.get());
- /*
- BinaryOperator interface takes two arguments and returns one
- */
- BinaryOperator<Integer> add = (a, b) -> a + b;
- System.out.println("add 10 + 25: " + add.apply(10, 25));
- /*
- UnaryOperator interface takes a single argument and returns a single value.
- */
- UnaryOperator<String> str = String::toUpperCase;
- System.out.println(str.apply("This is my message in upper case"));
- /*
- Output:
- true - Apples is less than 10
- abcdefghijklmnopqrstuvwxyz
- length of 26: 2
- Java is fun
- add 10 + 25: 35
- THIS IS MY MESSAGE IN UPPER CASE
- *
- *
- * */
- // BiFunction< 1ST PARAM TYPE, 2ND PARAM TYPE, RETURN TYPE>
- BiFunction<String, String, Integer> biFunction = (a, b) -> Integer.valueOf(a) + Integer.valueOf(b);
- System.out.println(biFunction.apply("2", "2"));
- Greeting greet = name -> System.out.println("Hi, " + name);
- greet.sayHi("GG");
- Calculator addition = (a,b) -> a + b;
- Calculator substraction = (a,b) -> a - b;
- Calculator division = (a,b) -> (b != 0 ? a / b : 0);
- Calculator multiplication = (a,b) -> a * b;
- // ==================== Method references ====================
- IntFunction<String> intToString = num -> Integer.toString(num);
- System.out.println("expected value 3, actual value: " +
- intToString.apply(123).length());
- //static method reference using ::
- IntFunction<String> intToString2 = Integer::toString;
- System.out.println("expected value 4, actual value: " +
- intToString2.apply(4567).length());
- //lambdas made using a constructor
- Function<String, BigInteger> newBigInt = BigInteger::new;
- System.out.println("expected value: 123456789, actual value: "+
- newBigInt.apply("123456789"));
- //example of a lambda made from an instance method
- Consumer<String> print = System.out::println;
- print.accept("Coming to you directly from a lambda...");
- //these two are the same using the static method concat
- UnaryOperator<String> greeting = x -> "Hello, ".concat(x);
- System.out.println(greeting.apply("World"));
- UnaryOperator<String> makeGreeting = "Hello, "::concat;
- System.out.println(makeGreeting.apply("Peggy"));
- Function<Integer, Integer> func = Test::addOne;
- // same as : Function<Integer, Integer> func = (value) -> addOne(value);
- System.out.println(func.apply(1));
- // ==================== Collection API ====================
- /*
- Set - a collection that does not contain duplicates
- List - an ordered collection based on the way the user entered the data
- Map - an object that maps keys to values.
- Streams operations are either intermediate or terminal
- */
- }
- @FunctionalInterface
- interface Greeting {
- void sayHi(String name);
- }
- @FunctionalInterface
- interface Calculator {
- int calc(int a, int b);
- }
- }
Add Comment
Please, Sign In to add comment