Skip to content

Add LambdaExpressionUtils.java – Unique Lambda-Based Helper Functions #6288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add unique Java Lambda Expression Utilities
  • Loading branch information
Farheen Shabbir Shaikh committed Jun 11, 2025
commit 068ece141a640e8ea4969ca2e1d043b77c0ce098
37 changes: 37 additions & 0 deletions Functional/LambdaExpressionUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* LambdaExpressionUtils.java
* Unique Java utility functions using Lambda expressions.
* Demonstrates Function, Predicate, Consumer, and Supplier.
*/

import java.util.Random;
import java.util.function.*;

public class LambdaExpressionUtils {

public static void main(String[] args) {
// Reverse a string
Function<String, String> reverse = str -> new StringBuilder(str).reverse().toString();
System.out.println("Reversed: " + reverse.apply("lambda"));

// Check palindrome
Predicate<String> isPalindrome = str -> str.equalsIgnoreCase(new StringBuilder(str).reverse().toString());
System.out.println("Is Palindrome: " + isPalindrome.test("madam"));

// Print message in all caps with exclamation
Consumer<String> shout = s -> System.out.println(s.toUpperCase() + "!");
shout.accept("functional interface");

// Check if number is even
Function<Integer, Boolean> isEven = n -> n % 2 == 0;
System.out.println("Is 10 even? " + isEven.apply(10));

// Get random greeting
Supplier<String> randomGreeting = () -> {
String[] greetings = {"Hello", "Hi", "Hey", "Hola"};
return greetings[new Random().nextInt(greetings.length)];
};
System.out.println("Random Greeting: " + randomGreeting.get());
}

}