Skip to content

Commit 068ece1

Browse files
author
Farheen Shabbir Shaikh
committed
Add unique Java Lambda Expression Utilities
1 parent 5d51a5a commit 068ece1

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Functional/LambdaExpressionUtils.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* LambdaExpressionUtils.java
3+
* Unique Java utility functions using Lambda expressions.
4+
* Demonstrates Function, Predicate, Consumer, and Supplier.
5+
*/
6+
7+
import java.util.Random;
8+
import java.util.function.*;
9+
10+
public class LambdaExpressionUtils {
11+
12+
public static void main(String[] args) {
13+
// Reverse a string
14+
Function<String, String> reverse = str -> new StringBuilder(str).reverse().toString();
15+
System.out.println("Reversed: " + reverse.apply("lambda"));
16+
17+
// Check palindrome
18+
Predicate<String> isPalindrome = str -> str.equalsIgnoreCase(new StringBuilder(str).reverse().toString());
19+
System.out.println("Is Palindrome: " + isPalindrome.test("madam"));
20+
21+
// Print message in all caps with exclamation
22+
Consumer<String> shout = s -> System.out.println(s.toUpperCase() + "!");
23+
shout.accept("functional interface");
24+
25+
// Check if number is even
26+
Function<Integer, Boolean> isEven = n -> n % 2 == 0;
27+
System.out.println("Is 10 even? " + isEven.apply(10));
28+
29+
// Get random greeting
30+
Supplier<String> randomGreeting = () -> {
31+
String[] greetings = {"Hello", "Hi", "Hey", "Hola"};
32+
return greetings[new Random().nextInt(greetings.length)];
33+
};
34+
System.out.println("Random Greeting: " + randomGreeting.get());
35+
}
36+
37+
}

0 commit comments

Comments
 (0)