containsAll()

Pangram
Pangram in Java
import java.util.Arrays;

public class PangramChecker {

    public boolean isPangram(String input) {
        return Arrays.asList(input.toLowerCase().split(""))
            .containsAll(Arrays.asList("abcdefghijklmnopqrstuvwxyz".split("")));
    }
}

This approach starts by importing from packages for what is needed.

The input String is lowercased and chained into the split() method to create an array of Strings. The enclosing Arrays.asList() method converts the String array into a List of Strings.

Note

The chars() and toCharArray() methods won't work for Arrays.asList() because they produce primitive ints or chars respectively. For Arrays.asList() to work as desired, it must take an array of reference types. The split() method is used here because it returns an array of Strings, and String is a reference type.

The List of input character Strings is chained to the containsAll() method. A String List of the English lowercase letters is passed to containsAll(). If all letters in the English alphabet are contained in the input, then containsAll() returns true, otherwise it returns false. Because the condition is for all English letters being in the input, the input does not need to have non-letters filtered out.

22nd May 2024 · Found it useful?