Check if a string is a palindrome in Java 8



Write a Java 8 program (using Java 8 Stream API) that determines if the given string is palindrome or not.

Java 8 program to Check if a string is a palindrome

import java.util.stream.IntStream;

public final class Strings {

    public static boolean isPalindrome(String str) {

        if (str == null || str.isBlank()) {
            // or throw IllegalArgumentException
            return false;
        }

        return IntStream.range(0, str.length() / 2)
                .noneMatch(p -> str.charAt(p) != str.charAt(str.length() - p - 1));
    }
}

Test

public class Main {

    private static final String TEXT = "ABCDEFEDCBA";

    public static void main(String[] args) {

        boolean resultV1 = Strings.isPalindrome(TEXT);
        System.out.println("'" + TEXT + "' is palindrome? " + resultV1);
    }

}

Output:

'ABCDEFEDCBA' is palindrome? true

Free Spring Boot Tutorial - 5 Hours Full Course


Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course

Comments