Class Stream


  • public class Stream
    extends java.lang.Object
    A stream of elements supporting sequential operations such as filter, map, reduce, and various terminal operations.
    • Constructor Summary

      Constructors 
      Constructor Description
      Stream​(java.util.Iterator<java.lang.Object> iterator)
      Constructs a new Stream from the given iterator.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Optional allMatch​(LambdaExpression le)
      Checks if all elements in the stream match the given predicate.
      Optional anyMatch​(LambdaExpression le)
      Checks if any element in the stream matches the given predicate.
      Optional average()
      Calculates the average of all numeric elements in the stream.
      java.lang.Long count()
      Counts the number of elements in the stream.
      Stream distinct()
      Returns a stream consisting of the distinct elements of this stream.
      Stream filter​(LambdaExpression le)
      Returns a stream consisting of the elements that match the given predicate.
      Optional findFirst()
      Returns an Optional describing the first element of this stream, or an empty Optional if the stream is empty.
      Stream flatMap​(LambdaExpression le)
      Returns a stream consisting of the results of replacing each element of this stream with the contents of a stream produced by applying the provided function to each element.
      java.lang.Object forEach​(LambdaExpression le)
      Performs an action for each element of this stream.
      java.util.Iterator<?> iterator()
      Returns the underlying iterator for this stream.
      Stream limit​(java.lang.Number count)
      Returns a stream consisting of the first count elements of this stream.
      Stream map​(LambdaExpression le)
      Returns a stream consisting of the results of applying the given function to the elements of this stream.
      Optional max()
      Returns the maximum element of this stream according to natural order.
      Optional max​(LambdaExpression le)
      Returns the maximum element of this stream according to the provided comparator.
      Optional min()
      Returns the minimum element of this stream according to natural order.
      Optional min​(LambdaExpression le)
      Returns the minimum element of this stream according to the provided comparator.
      Optional noneMatch​(LambdaExpression le)
      Checks if no elements of this stream match the given predicate.
      Stream peek​(LambdaExpression le)
      Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed.
      Optional reduce​(LambdaExpression le)
      Performs a reduction on the elements of this stream, using the provided accumulator function.
      java.lang.Object reduce​(java.lang.Object seed, LambdaExpression le)
      Performs a reduction on the elements of this stream, using the provided initial value and accumulator function.
      Stream sorted()
      Returns a stream consisting of the elements of this stream, sorted according to natural order.
      Stream sorted​(LambdaExpression le)
      Returns a stream consisting of the elements of this stream, sorted according to the order induced by the provided comparator.
      Stream substream​(java.lang.Number start)
      Returns a stream consisting of the elements of this stream from the specified start position to the end.
      Stream substream​(java.lang.Number start, java.lang.Number end)
      Returns a stream consisting of the elements of this stream from the specified start position up to, but not including, the specified end position.
      java.lang.Number sum()
      Returns the sum of all numeric elements in the stream.
      java.lang.Object[] toArray()
      Returns an array containing all the elements of this stream.
      java.util.List<java.lang.Object> toList()
      Returns a list containing all the elements of this stream.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Stream

        public Stream​(java.util.Iterator<java.lang.Object> iterator)
        Constructs a new Stream from the given iterator.
        Parameters:
        iterator - the iterator providing the stream elements
    • Method Detail

      • filter

        public Stream filter​(LambdaExpression le)
        Returns a stream consisting of the elements that match the given predicate.
        Parameters:
        le - the predicate to apply to each element
        Returns:
        the new filtered stream
      • map

        public Stream map​(LambdaExpression le)
        Returns a stream consisting of the results of applying the given function to the elements of this stream.
        Parameters:
        le - the function to apply to each element
        Returns:
        the new mapped stream
      • flatMap

        public Stream flatMap​(LambdaExpression le)
        Returns a stream consisting of the results of replacing each element of this stream with the contents of a stream produced by applying the provided function to each element.
        Parameters:
        le - the function to apply to each element which produces a new stream
        Returns:
        the new flattened stream
      • distinct

        public Stream distinct()
        Returns a stream consisting of the distinct elements of this stream.
        Returns:
        the new stream with distinct elements
      • sorted

        public Stream sorted()
        Returns a stream consisting of the elements of this stream, sorted according to natural order.
        Returns:
        the new sorted stream
      • sorted

        public Stream sorted​(LambdaExpression le)
        Returns a stream consisting of the elements of this stream, sorted according to the order induced by the provided comparator.
        Parameters:
        le - the comparator to determine sort order
        Returns:
        the new sorted stream
      • forEach

        public java.lang.Object forEach​(LambdaExpression le)
        Performs an action for each element of this stream.
        Parameters:
        le - the action to perform for each element
        Returns:
        null
      • peek

        public Stream peek​(LambdaExpression le)
        Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed.
        Parameters:
        le - the action to perform on each element
        Returns:
        the new stream
      • iterator

        public java.util.Iterator<?> iterator()
        Returns the underlying iterator for this stream.
        Returns:
        the underlying iterator
      • limit

        public Stream limit​(java.lang.Number count)
        Returns a stream consisting of the first count elements of this stream.
        Parameters:
        count - the number of elements to limit to
        Returns:
        the new limited stream
      • substream

        public Stream substream​(java.lang.Number start)
        Returns a stream consisting of the elements of this stream from the specified start position to the end.
        Parameters:
        start - the start position (inclusive)
        Returns:
        the new substream
      • substream

        public Stream substream​(java.lang.Number start,
                                java.lang.Number end)
        Returns a stream consisting of the elements of this stream from the specified start position up to, but not including, the specified end position.
        Parameters:
        start - the start position (inclusive)
        end - the end position (exclusive)
        Returns:
        the new substream
      • toList

        public java.util.List<java.lang.Object> toList()
        Returns a list containing all the elements of this stream.
        Returns:
        the list of elements
      • toArray

        public java.lang.Object[] toArray()
        Returns an array containing all the elements of this stream.
        Returns:
        the array of elements
      • reduce

        public Optional reduce​(LambdaExpression le)
        Performs a reduction on the elements of this stream, using the provided accumulator function. The first element is used as the initial value.
        Parameters:
        le - the accumulator function
        Returns:
        an Optional describing the reduced value, or empty if this stream is empty
      • reduce

        public java.lang.Object reduce​(java.lang.Object seed,
                                       LambdaExpression le)
        Performs a reduction on the elements of this stream, using the provided initial value and accumulator function.
        Parameters:
        seed - the initial value for the reduction
        le - the accumulator function
        Returns:
        the result of the reduction
      • max

        public Optional max()
        Returns the maximum element of this stream according to natural order.
        Returns:
        an Optional describing the maximum element, or empty if this stream is empty
      • max

        public Optional max​(LambdaExpression le)
        Returns the maximum element of this stream according to the provided comparator.
        Parameters:
        le - the comparator to determine the maximum
        Returns:
        an Optional describing the maximum element, or empty if this stream is empty
      • min

        public Optional min()
        Returns the minimum element of this stream according to natural order.
        Returns:
        an Optional describing the minimum element, or empty if this stream is empty
      • min

        public Optional min​(LambdaExpression le)
        Returns the minimum element of this stream according to the provided comparator.
        Parameters:
        le - the comparator to determine the minimum
        Returns:
        an Optional describing the minimum element, or empty if this stream is empty
      • average

        public Optional average()
        Calculates the average of all numeric elements in the stream.
        Returns:
        an Optional containing the average, or empty if the stream is empty
      • sum

        public java.lang.Number sum()
        Returns the sum of all numeric elements in the stream.
        Returns:
        the sum of all elements
      • count

        public java.lang.Long count()
        Counts the number of elements in the stream.
        Returns:
        the number of elements
      • anyMatch

        public Optional anyMatch​(LambdaExpression le)
        Checks if any element in the stream matches the given predicate.
        Parameters:
        le - the predicate lambda expression
        Returns:
        an Optional containing true if any element matches, or empty if the stream is empty
      • allMatch

        public Optional allMatch​(LambdaExpression le)
        Checks if all elements in the stream match the given predicate.
        Parameters:
        le - the predicate lambda expression
        Returns:
        an Optional containing true if all elements match, or empty if the stream is empty
      • noneMatch

        public Optional noneMatch​(LambdaExpression le)
        Checks if no elements of this stream match the given predicate.
        Parameters:
        le - the predicate lambda expression
        Returns:
        an Optional containing true if no elements match, or empty if the stream is empty
      • findFirst

        public Optional findFirst()
        Returns an Optional describing the first element of this stream, or an empty Optional if the stream is empty.
        Returns:
        an Optional describing the first element of this stream