↓ Skip to main content
Streams

JavaStream: Characters

·1 min
Table of Contents

Continuing from the last blog how about checking a pangram using streams.

public class PangramChecker {

    private String allChars = "abcdefghijklmnopqrstuvwxyz";

    public boolean isPangram(String input) {
        if (input.length() <= 0 || input.isBlank()){
            return false;
        }
        return (allChars.chars().map(x -> (char) x).filter(x -> input.toLowerCase().indexOf(x) >= 0).count() == 26);
    }

}

All the for loops have been squeezed into a simple line

The Logic
#

  • Define a String for all the characters in the English language - allChars
  • Get the Stream using allChars.chars()
  • Since it returns an int map it to Char
  • Look for every character one by one in the Input String using indexOf. (It returns -1 for character not found.)
  • Get the count of all the characters found, if anyone was not found we will have less than 26.

It can be further optimized… want to try?

Author
Samarthya
Engineer, learner, tinkerer. This is my trophy shelf of everyday work and leisure.

Related

Java Streams

·2 mins
Stream is an new abstraction that lets one process data in a declarative way. A stream does not store data and, in that sense, is not a data structure. It also never modifies the underlying data source. The easiest way to learn a new API or a construct is by trying it. So here is an exercise that I tried to learn the Stream API. This is an exercise from exercism.io.

PS: Process Status

·2 mins
MAN Pages are one source of truth for all curious queries. man ps Name ps - report a snapshot of the current processes. It is used to display instant information about the active processes in the system.

Kernel Mode

·2 mins
The CPU operates in two modes in Linux User mode (non privileged mode) Kernel mode (System mode) When operating in Kernel mode it has un-restricted access to resources like memory and input output devices.