Java program to check Palindrome string using lambda expression
Check if String is Palindrome
Write a Java program to implement a lambda expression to check if a given string is a palindrome.
Sample Solution:
Java Code:
Sample Output:
Madam is a palindrome? false radar is a palindrome? true defied is a palindrome? false
Explanation:
In the above exercise -
- From the java.util.function package, we import Predicate.
- In the main method, we define a lambda expression str -> { ... } to check if a string is a palindrome.
- Inside the lambda expression, we create a reversed version of the input string by using a StringBuilder to reverse the characters. We convert it back to a string using toString().
- Compare the original string with the reversed string using the equals() method. Return true if they are equal, indicating that the string is a palindrome. Otherwise, it returns false.
- Create an instance of the Predicate interface called isPalindrome and assign the lambda expression to it.
- Test the lambda expression by checking some strings are palindromes or not using the test() method on the isPalindrome predicate.
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Java program to implement a lambda expression that checks if a string is a palindrome by reversing it and comparing.
- Write a Java program to create a lambda that checks palindromicity of a string ignoring case and non-alphanumeric characters.
- Write a Java program to implement a lambda that returns true if every word in a sentence is a palindrome.
- Write a Java program to chain lambda expressions to filter out palindromic strings from a list.
Live Demo:
Java Code Editor:
Improve this sample solution and post your code through Disqus
Java Lambda Exercises Previous: Java Program to count words in a sentence using lambda expression.
Java Lambda Exercises Next: Java program to calculate sum of squares of odd and even numbers using lambda expression.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics