Here we will write a Program to Check if a number is Palindrome from Java 8.
- Enter the use input from Scanner(System.in) object.
- Next sc.nextInt() method take the input as a Integer.
- Here anyMatch() method is returned any element in a stream matches a given Predicate.
- Inside anyMatch() method we compare the given number from reverse of the given number using equals() and string builder reverse() method.
- Print the Boolean type status that number is palindrome or not.
import java.util.Scanner;
import java.util.stream.Stream;
public class CheckPalindrome {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int i = sc.nextInt();
boolean check = Stream.of(i)
.anyMatch(r -> r.equals(Integer.parseInt(new StringBuilder(String.valueOf(i)).reverse().toString())));
System.out.println("Given number is palindrome are :- " + check);
sc.close();
}
}
Output :-
Enter the number
121
Given number is palindrome are :- true