Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

DoubleStream: counting and averaging CPU loads

Build a stream of primitive doubles, select CPU loads at or above `0.50`, and calculate the count and mean separately for all readings and for high readings.

Version: Java SE 21

Syntax

import java.util.OptionalDouble;
import java.util.function.DoublePredicate;
import java.util.stream.DoubleStream;

// Signatures declared by DoubleStream
static DoubleStream of(double value)
static DoubleStream of(double... values)
DoubleStream filter(DoublePredicate predicate)
long count()
OptionalDouble average()

In Java SE 21, java.util.stream.DoubleStream processes primitive double values rather than Double objects. The static factory DoubleStream.of(double) creates a sequential stream with one element. The DoubleStream.of(double...) overload accepts zero or more double arguments or a double[] and creates an ordered, sequential stream.

DoubleStream.filter is a lazy intermediate operation: it returns a stream containing only values for which the DoublePredicate returns true. The predicate should not modify the source or depend on changing state. With CPU loads represented as fractions, load -> load >= 0.50 includes exactly 0.50 (50%); > would exclude it. The comparison returns false for NaN.

DoubleStream.count is a terminal operation that returns the number of elements as a long, or 0L for an empty stream. If the count can be determined without traversing the elements, count() can skip intermediate operations, so don't rely on their side effects. The terminal operation DoubleStream.average returns the arithmetic mean in a java.util.OptionalDouble; an empty stream gives an empty result, not 0.0. The mean is subject to floating-point rounding. Check isPresent() before calling getAsDouble(), which throws NoSuchElementException on an empty result.

A stream is single-use: after count(), don't call average() on the same stream. Detected reuse may cause IllegalStateException. Create a fresh stream from the same readings for each statistic, applying the filter only for the high-load group.

Examples

import java.util.OptionalDouble;
import java.util.function.DoublePredicate;
import java.util.stream.DoubleStream;

public class CpuLoads {
    public static void main(String[] args) {
        double[] cpuLoads = {0.125, 0.375, 0.50, 0.75};
        DoublePredicate highLoad = load -> load >= 0.50;

        long allCount = DoubleStream.of(cpuLoads).count();
        OptionalDouble allAverage = DoubleStream.of(cpuLoads).average();
        long highCount = DoubleStream.of(cpuLoads).filter(highLoad).count();
        OptionalDouble highAverage = DoubleStream.of(cpuLoads).filter(highLoad).average();

        System.out.println("All: count=" + allCount + ", average=" + display(allAverage));
        System.out.println("High: count=" + highCount + ", average=" + display(highAverage));
    }

    private static String display(OptionalDouble average) {
        return average.isPresent() ? Double.toString(average.getAsDouble()) : "n/a";
    }
}

Result

All: count=4, average=0.4375
High: count=2, average=0.625

Of the four readings, `0.50` and `0.75` meet the threshold. `n/a` means there is no mean, not that the mean is zero.

Related lessons

  • Java · Lesson 46 · Intermediate · Version: 2

Sources