Sub topic 14
Working with IntStream, LongStream, and DoubleStream
A Stream<Integer> holds Integer objects, not plain int values. Java has to wrap each number in an object. This wrapping is called boxing. Boxing costs memory and time. Java gives you special streams for numbers that skip it.
The Three Primitive Streams
A primitive type is a basic value type such as int, long, or double. Values of these types are not objects. A primitive stream is a stream that holds these values directly.
| Stream type | Holds | Example source |
|---|---|---|
| `IntStream` | `int` values | `IntStream.of(1, 2, 3)` |
| `LongStream` | `long` values | `LongStream.range(0, 10)` |
| `DoubleStream` | `double` values | `DoubleStream.of(1.5, 2.5)` |
Primitive streams also have built-in math methods: sum(), average(), min(), and max(). A regular Stream<Integer> does not have sum().
Counting with range and rangeClosed
IntStream.range(a, b) gives the numbers from a up to b, but leaves out b. IntStream.rangeClosed(a, b) includes b.
import java.util.stream.IntStream;
public class IntStreamExample {
public static void main(String[] args) {
int sumOfSquares = IntStream.rangeClosed(1, 5)
.map(i -> i * i)
.sum();
System.out.println(sumOfSquares); // 55
double average = IntStream.of(2, 4, 6)
.average()
.orElse(0.0);
System.out.println(average); // 4.0
}
}The first pipeline squares 1 through 5 and adds them up: 1 + 4 + 9 + 16 + 25 = 55.
The second pipeline uses average(), which returns an OptionalDouble. An OptionalDouble is a box that may or may not hold a double. An empty stream has no average, so you call orElse to give a default.
Switching Between Stream Types
You can move from an object stream to a primitive stream with mapToInt, mapToLong, or mapToDouble. To go back, call boxed().
import java.util.List;
import java.util.stream.Collectors;
public class MapToIntExample {
public static void main(String[] args) {
List<String> words = List.of("sun", "moon", "star");
int totalLetters = words.stream()
.mapToInt(String::length)
.sum();
System.out.println(totalLetters); // 11
List<Integer> lengths = words.stream()
.mapToInt(String::length)
.boxed()
.collect(Collectors.toList());
System.out.println(lengths); // [3, 4, 4]
}
}mapToInt(String::length) turns each word into its length as an int. Then sum() adds the lengths. In the second pipeline, boxed() turns each int back into an Integer. You need the Integer objects here because a List can only hold objects.
Exercise
What does IntStream.range(1, 5).sum() return, and why is it different from IntStream.rangeClosed(1, 5).sum()?
Show answer
It returns 10, because range leaves out the end value and adds only 1 + 2 + 3 + 4. rangeClosed includes 5, so it returns 15.
Exercise
You have a List<String> called names. Write one line that finds the length of the longest name, using 0 if the list is empty.
Show answer
int longest = names.stream().mapToInt(String::length).max().orElse(0); The max() method returns an OptionalInt, so orElse(0) supplies the default.