Java LongStream Methods

The LongStream interface in Java provides a sequence of long values that supports various operations to perform computations. It is part of the Java Stream API and offers functionalities for manipulating and processing sequences of long values in a declarative manner.

This guide covers various methods available in the LongStream interface. Each method is described in simple terms to help beginners understand how to use them. These methods allow you to perform operations such as filtering, mapping, reducing, and more, making it a powerful tool for handling long values in Java.

For more detailed information, please refer to the official Java SE Documentation

Java LongStream Methods

The table below contains various methods of the Java LongStream interface, each with a link to a detailed explanation, examples, and real-world uses. Click on the method names to learn more about how to use them effectively in your applications.

Method Description
allMatch() Returns whether all elements of this stream match the provided predicate.
anyMatch() Returns whether any elements of this stream match the provided predicate.
asDoubleStream() Returns a DoubleStream consisting of the elements of this stream, converted to double.
average() Returns an OptionalDouble describing the arithmetic mean of elements of this stream.
boxed() Returns a Stream consisting of the elements of this stream, each boxed to a Long.
collect() Performs a mutable reduction operation on the elements of this stream using a Collector.
concat() Creates a concatenated LongStream.
count() Returns the count of elements in this stream.
empty() Returns an empty sequential LongStream.
filter() Returns a stream consisting of the elements of this stream that match the given predicate.
findAny() Returns an OptionalLong describing some element of the stream, or an empty OptionalLong if the stream is empty.
findFirst() Returns an OptionalLong describing the first element of this stream, or an empty OptionalLong if the stream is empty.
flatMap() Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.
forEach() Performs an action for each element of this stream.
limit() Returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.
map() Returns a stream consisting of the results of applying the given function to the elements of this stream.
mapToDouble() Returns a DoubleStream consisting of the results of applying the given function to the elements of this stream.
mapToInt() Returns an IntStream consisting of the results of applying the given function to the elements of this stream.
max() Returns an OptionalLong describing the maximum element of this stream, or an empty OptionalLong if the stream is empty.
min() Returns an OptionalLong describing the minimum element of this stream, or an empty OptionalLong if the stream is empty.
noneMatch() Returns whether no elements of this stream match the provided predicate.
of() Returns a sequential LongStream containing a single element.
peek() Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.
reduce() Performs a reduction on the elements of this stream, using an associative accumulation function, and returns an OptionalLong describing the reduced value, if any.
sorted() Returns a stream consisting of the elements of this stream, sorted according to natural order.
skip() Returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream.
sum() Returns the sum of elements in this stream.
toArray() Returns an array containing the elements of this stream.

References:

Comments