Using numeric streams for arithmetic operations
In addition to the Stream
interface, the java.util.stream
package also provides specialized interfaces—IntStream
, DoubleStream
, and LongStream
—that are optimized for processing streams of corresponding primitive types. They are very convenient to use, and have numeric operations, such as max()
, min()
, average()
, sum()
.
The numeric interfaces have methods similar to the methods of the Stream interface, which means that everything we have talked about in the previous recipe, Creating and operating on streams, applies to numeric streams too. That is why, in this section, we will only talk about the methods that are not present in the Stream
interface.
Getting ready
In addition to the methods described in the Creating and operating on streams recipe, the following methods can be used to create a numeric stream:
- The
range(int startInclusive, int endInclusive)
andrangeClosed(int startInclusive, int endInclusive)
methods of theIntStream
andLongStream...