In this post, we will see “How to check if the String or Number is Palindrome or not with the help of Java 8 Streams API?”
Java 8 Streams power to test Palindrome... Check it out...!!! Share on X
Here, we are going to cover below points:
- What is Palindrome?
- How to check a Palindrome using Java 8?
- How to check a Palindrome through other ways?
Check out: How to Pass Lambda as a Parameter in Java 8
Let’s begin,
1. What is Palindrome?
Palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g., madam or level or radar etc.
Also, palindrome numbers are those numbers which represent same number if all digits are reversed. e.g., 10001
2. How to check Palindrome using Java 8?
Let’s say: In this particular example, we are checking if ‘radar’ & ‘apple’ are palindromes or not.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.stream.IntStream; public class Check_Palindrome_String_Using_Streams_Java8 { public static void main(String[] args) { //Check if 'radar' is Palindrome System.out.println("Is radar a Palindrome? - " + isPalindrome("radar")); //true //Check if 'apple' is Palindrome System.out.println("Is apple a Palindrome? - " + isPalindrome("apple")); //false } public static boolean isPalindrome(String originalString) { String tempString = originalString.replaceAll("\\s+", "").toLowerCase(); return IntStream.range(0, tempString.length() / 2) .noneMatch(i - > tempString.charAt(i) != tempString.charAt(tempString.length() - i - 1)); } } |
1 2 | is radar a Palindrome? - true is apple a Palindrome? - false |
3. How to check Palindrome through other ways?
Using For-Loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class Check_Palindrome_String_Using_ForLoop_Java { public static void main(String[] args) { //Check if 'radar' is Palindrome System.out.println("Is radar a Palindrome? - " + isPalindrome("radar")); //true //Check if 'apple' is Palindrome System.out.println("Is apple a Palindrome? - " + isPalindrome("apple")); //false } public static boolean isPalindrome(String orinalString) { String reversedString = ""; for (int i = orinalString.length() - 1; i >= 0; i--) reversedString = reversedString + orinalString.charAt(i); return orinalString.equals(reversedString); } } |
Using StringBuilder:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class Check_Palindrome_String_Using_StringBuilder_Java { public static void main(String[] args) { //Check if 'radar' is Palindrome System.out.println("Is radar a Palindrome? - " + isPalindrome("radar")); //true //Check if 'apple' is Palindrome System.out.println("Is apple a Palindrome? - " + isPalindrome("apple")); //false } public static boolean isPalindrome(String originalString) { String reversedString = new StringBuilder(originalString).reverse().toString(); return originalString.equals(reversedString); } } |
Using Apache StringUtils:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import org.apache.commons.lang3.StringUtils; public class Check_Palindrome_String_Using_Apache_StringUtils_Java { public static void main(String[] args) { //Check if 'radar' is Palindrome System.out.println("Is radar a Palindrome? - " + isPalindrome("radar")); //true //Check if 'apple' is Palindrome System.out.println("Is apple a Palindrome? - " + isPalindrome("apple")); //false } public static boolean isPalindrome(String orinalString) { String reversedString = StringUtils.reverse(orinalString); return orinalString.equals(reversedString); } } |
All the above ways of verifying the Palindrome will generate the same output.
That’s it, Java 8 Streams API is so powerful and notice how easily it simplified the way of checking the Palindrome.
Java 8 Streams power to test Palindrome... Check it out...!!! Share on X
Do you like this Post? – then check my other helpful posts:
- Double the even / odd numbers of a specified ArrayList using Streams
- Double the numbers of specified ArrayList using Streams