How do I create ranges of numbers as Streams?

In Java, you can use IntStream, LongStream, or DoubleStream to create ranges of numbers as streams. For example, to create a range of integer numbers, we can call the range() static method of the IntStream interface.

Here’s an example:

package org.kodejava.util;

import java.util.stream.IntStream;

public class IntRangeExample {
    public static void main(String[] args) {
        IntStream.range(1, 11).forEach(System.out::println);
    }
}

In this example, IntStream.range(1, 11) creates a stream of integers from 1 (inclusive) to 11 (exclusive), which means it will print numbers from 1 to 10.

Suppose you have an application, where you need to assign a unique-id to each book in the library inventory. You may generate a sequence of unique IDs using a stream.

package org.kodejava.util;

import java.util.stream.IntStream;

public class IntRangeOtherExample {
    public static void main(String[] args) {
        final int START_ID = 1001;
        final int NUM_BOOKS = 10;

        IntStream.range(START_ID, START_ID + NUM_BOOKS)
                .forEach(id -> System.out.println("Assigned id for a new book: " + id));
    }
}

In this example, we’re starting id sequence from 1001 (START_ID), and we have NUM_BOOKS set to 10. So, We’re generating 10 unique ids starting from 1001, each for a new incoming book.

Or another scenario where you want a list of 10 random integers within a range (for example, to simulate test data):

package org.kodejava.util;

import java.util.Random;

public class RandomRangeExample {
    public static void main(String[] args) {
        final int MIN_VAL = 10;
        final int MAX_VAL = 100;
        final int NUM_ELEMENTS = 10;

        new Random()
                .ints(NUM_ELEMENTS, MIN_VAL, MAX_VAL)
                .forEach(System.out::println);
    }
}

Here, we’re generating NUMBER_ELEMENTS number of integers between MIN_VALUE and MAX_VALUE.

An example output of the last code snippet is:

42
46
54
30
67
69
46
11
39
59

The DoubleStream interface has several factory methods you can use to create ranges of double values. Here’s an example:

package org.kodejava.util;

import java.util.stream.DoubleStream;

public class DoubleStreamExample {
    public static void main(String[] args) {
        DoubleStream.iterate(0, n -> n + 0.5)
                .limit(10)
                .forEach(System.out::println);
    }
}

In this example, DoubleStream.iterate(0, n -> n + 0.5) creates a stream of double starting with 0 and then applying the unary function n -> n + 0.5 to each subsequent element. As a result, it will print out 0 followed by 0.5, 1.0, 1.5, etc., up to 4.5 as there are 10 elements due to .limit(10). Modify 0, 0.5 and 10 to match the start value, increment and the limit respectively that suit your needs.

For a real-world scenario, suppose we need double values to represent temperatures difference every half degree for the next 8 hours starting from 20 degrees Celsius. This can be represented as follows:

package org.kodejava.util;

import java.util.stream.DoubleStream;

public class DoubleStreamTemperature {
    public static void main(String[] args) {
        DoubleStream.iterate(20, c -> c + 0.5)
                .limit(16)
                .forEach(temp -> System.out.println("Predicted temperature for next hour: " + temp + "C"));
    }
}

In this example, DoubleStream.iterate(20, c -> c + 0.5) creates a stream starting with 20 (degrees Celsius in this context), and then by adding 0.5 to each subsequent temperature (which stands for temperature difference after an hour). .limit(16) keeps the listening for 8 hours only as we want temperature for every half an hour.

How do I write range character class regex?

To define a character class that includes a range of values, put - metacharacter between the first and last character to be matched. For example [a-e]. You can also specify multiple ranges like this [a-zA-Z]. This will match any letter of the alphabet from a to z (lowercase) or A to Z (uppercase).

In the example below we are matching the word that begins with bat and ends with a single number that have a value range from 3 to 7.

package org.kodejava.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CharacterClassesRangeClassDemo {
    public static void main(String[] args) {
        // Defines regex that will search all sequences of string
        // that begin with bat and number which range [3-7]
        String regex = "bat[3-7]";
        String input =
                "bat1, bat2, bat3, bat4, bat5, bat6, bat7, bat8";

        // Compiles the given regular expression into a pattern.
        Pattern pattern = Pattern.compile(regex);

        // Creates a matcher that will match the given input
        // against this pattern.
        Matcher matcher = pattern.matcher(input);

        // Find every match and prints it.
        while (matcher.find()) {
            System.out.format("Text \"%s\" found at %d to %d.%n",
                    matcher.group(), matcher.start(),
                    matcher.end());
        }
    }
}

The program will match the following string from the input:`

Text "bat3" found at 12 to 16.
Text "bat4" found at 18 to 22.
Text "bat5" found at 24 to 28.
Text "bat6" found at 30 to 34.
Text "bat7" found at 36 to 40.