Intermediate Operations

distinct() : Remove duplicates

Returns a stream consisting of the distinct elements of this stream.

Stream.of([1, 1, 2, 3]) \
    .distinct() \
    .to_list() # [1, 2, 3]

drop_while() : Drop elements while the predicate is true

Returns, if this stream is ordered, a stream consisting of the remaining elements of this stream after dropping the longest prefix of elements that match the given predicate.

Stream.of([1, 2, 3]) \
    .drop_while(lambda x: x < 3) \
    .to_list() # [3]

filter() : Restrict the Stream

Returns a stream consisting of the elements of this stream that match the given predicate.

Stream.of([1, 2, 3, None]) \
    .filter(lambda x: x is not None) \
    .for_each(print) # 1 2 3

flat_map() : Streams in Streams

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.

group_by(): Group the stream by a given key

Returns a stream consisting of the elements of this stream, grouped by the given classifier and extracting the key/value pairs.

limit() : Limit the Stream to a certain number of elements

Returns a stream consisting of the elements of this stream, truncated to be no longer than max_size.

map() : Convert the elements in the Stream

Returns a stream consisting of the results of applying the given function to the elements of this stream.

map_to_float() : Convert the elements in the Stream to a Float

Returns a NumericStream consisting of the results of applying the float() function to the elements of this stream. Note that this method is not none safe.

map_to_int() : Convert the elements in the Stream to an Integer

Returns a NumericStream consisting of the results of applying the int() function to the elements of this stream. Note that this method is not none safe.

map_to_str() : Convert the elements in the Stream to a String

Returns a stream consisting of the results of applying the str() function to the elements of this stream.

numeric() : Convert the stream to a NumericStream

Returns a NumericStream consisting of the same elements as the stream contained before conversion.

parallel() : Convert the stream to a ParallelStream

Returns a ParallelStream consisting of the same elements as the stream contained before conversion.

peek() : View intermediate results

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.

reversed() : Reverse Stream

Returns a stream consisting of the elements of this stream in reverse order.

sequential() : Convert the stream to a SequentialStream

Returns a SequentialStream consisting of the same elements as the stream contained before conversion.

skip() : Skip the first n elements of the Stream

Returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream.

sorted() : Sort Stream

Returns a stream consisting of the elements of this stream, sorted according to natural order or comparator.

Here is an example with a custom comparator:

take_while() : Take elements while the predicate is true

Returns, if this stream is ordered, a stream consisting of the longest prefix of elements taken from this stream that match the given predicate.

Last updated

Was this helpful?