Skip to content

Instantly share code, notes, and snippets.

@ucguy4u
Last active September 18, 2020 17:15
Show Gist options
  • Save ucguy4u/f68c76a0d1a5375c9e5626e60cfb1309 to your computer and use it in GitHub Desktop.
Save ucguy4u/f68c76a0d1a5375c9e5626e60cfb1309 to your computer and use it in GitHub Desktop.
Sample program to introduce functional interfaces with lambda notation
package com.ucguy4u.functional.java;
import java.util.function.*;
/**
*
* @author chauhanuday
*/
public class Lambdas {
public static void main(String[] args) {
// using the test method of Predicate
System.out.println("Predicate example :");
Predicate<String> stringLen = (s) -> s.length() < 10;
System.out.println(stringLen.test("Apples") + " - Apples is less than 10");
// Consumer example uses accept method
System.out.println("\nConsumer example :");
Consumer<String> consumerStr = (s) -> System.out.println(s.toLowerCase());
consumerStr.accept("ABCDefghijklmnopQRSTuvWxyZ");
// Function example
System.out.println("\nFunction example :");
Function<Integer, String> converter = (num) -> Integer.toString(num);
System.out.println("\nlength of 26: " + converter.apply(26).length());
// Supplier example
System.out.println("\nSupplier example :");
Supplier<String> s = () -> "Java is fun";
System.out.println(s.get());
// Binary Operator example
System.out.println("\nBinaryOperator example :");
BinaryOperator<Integer> add = (a, b) -> a + b;
System.out.println("add 10 + 25: " + add.apply(10, 25));
// Unary Operator example
System.out.println("\nUnaryOperator example :");
UnaryOperator<String> str = (msg) -> msg.toUpperCase();
System.out.println(str.apply("This is my message in upper case"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment