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 get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024