Counting Elements – Streams
By Stephen Trude / October 23, 2022 / No Comments / Certifications of Oracle, Creating an Optional, Numeric Optional Classes
Counting Elements
The count() operation performs a functional reduction on the elements of a stream, as each element contributes to the count which is the single immutable value returned by the operation. The count() operation reports the number of elements that are made available to it, which is not necessarily the same as the number of elements in the initial stream, as elements might be discarded by the intermediate operations.
The code below finds the total number of CDs in the streams, and how many of these CDs are jazz music CDs.
long numOfCDS = CD.cdList.stream().count(); // 5
long numOfJazzCDs = CD.cdList.stream().filter(CD::isJazz).count(); // 3
The count() method is also defined for the numeric streams. Below it is used on an IntStream to find how many numbers between 1 and 100 are divisible by 7.
IntStream numStream = IntStream.rangeClosed(1, 100);
long divBy7 = numStream.filter(n -> n % 7 == 0).count(); // 14
long count()
This terminal operation returns the count of elements in this stream—that is, the length of this stream.
This operation is a special case of a functional reduction.
The operation does not terminate when applied to an infinite stream.
Finding Min and Max Elements
The min() and max() operations are functional reductions, as they consider all elements of the stream and return a single value. They should only be applied to a finite stream, as they will not terminate on an infinite stream. These methods are also defined by the numeric stream interfaces for the numeric types, but without the specification of a comparator.
Optional<T> min(Comparator<? super T> cmp)
Optional<T> max(Comparator<? super T> cmp)
These terminal operations return an Optional with the minimum or maximum element of this stream according to the provided Comparator, respectively, or an empty Optional if this stream is empty. It throws a NullPointerException if the minimum element is null.
These operations are a special case of a functional reduction.
These operations do not terminate when applied to an infinite stream.
Both methods return an Optional, as the minimum and maximum elements might not exist—for example, if the stream is empty. The code below finds the minimum and maximum elements in a stream of CDs, according to their natural order. The artist name is the most significant field according to the natural order defined for CDs (p. 883).
Optional<CD> minCD = CD.cdList.stream().min(Comparator.naturalOrder());
minCD.ifPresent(out::println); // <Funkies, “Lambda Dancing”, 10, 2018, POP>
out.println(minCD.map(CD::artist).orElse(“No min CD.”)); // Funkies
Optional<CD> maxCD = CD.cdList.stream().max(Comparator.naturalOrder());
maxCD.ifPresent(out::println); // <Jaav, “Java Jive”, 8, 2017, POP>
out.println(maxCD.map(CD::artist).orElse(“No max CD.”)); // Jaav
In the code below, the max() method is applied to an IntStream to find the largest number between 1 and 100 that is divisible by 7.
IntStream iStream = IntStream.rangeClosed(1, 100);
OptionalInt maxNum = iStream.filter(n -> n % 7 == 0).max(); // 98
If one is only interested in the minimum and maximum elements in a collection, the overloaded methods min() and max() of the java.util.Collections class can be more convenient to use.