Last active
September 18, 2020 17:15
-
-
Save ucguy4u/f68c76a0d1a5375c9e5626e60cfb1309 to your computer and use it in GitHub Desktop.
Sample program to introduce functional interfaces with lambda notation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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