Intermediate Operations
distinct() : Remove duplicates
distinct() : Remove duplicatesReturns 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
drop_while() : Drop elements while the predicate is trueReturns, 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
filter() : Restrict the StreamReturns 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 3flat_map() : Streams in Streams
flat_map() : Streams in StreamsReturns 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.
Stream.of([1, 2, 3]) \
    .flat_map(lambda x: Stream.of([x, x])) \
    .to_list() # [1, 1, 2, 2, 3, 3]group_by(): Group the stream by a given key
group_by(): Group the stream by a given keyReturns a stream consisting of the elements of this stream, grouped by the given classifier and extracting the key/value pairs.
class Point:
    def __init__(self, x: int, y: int):
        self.x = x
        self.y = y
    def __str__(self):
        return f"Point({self.x}, {self.y})"
Stream.of([Point(1, 2), Point(1, 5), Point(3, 4), Point(3, 1)]) \
    .group_by(lambda p: p.x) \
    .map(lambda g: (g[0], [str(p) for p in g[1]])) \
    .for_each(print)  # (1, ['Point(1, 2)', 'Point(1, 5)'])
                      # (3, ['Point(3, 4)', 'Point(3, 1)'])limit() : Limit the Stream to a certain number of elements
limit() : Limit the Stream to a certain number of elementsReturns a stream consisting of the elements of this stream, truncated to be no longer than max_size.
Stream.of([1, 2, 3]) \
    .limit(2) \
    .to_list() # [1, 2]map() : Convert the elements in the Stream
map() : Convert the elements in the StreamReturns a stream consisting of the results of applying the given function to the elements of this stream.
Stream.of([1, "2", 3.0, None]) \
    .map(str) \
    .to_list() # ["1", "2", "3.0", "None"]map_to_float() : Convert the elements in the Stream to a Float
map_to_float() : Convert the elements in the Stream to a FloatReturns 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.
Stream.of([1, "2", 3.0]) \
    .map_to_float() \
    .to_list() # [1.0, 2.0, 3.0]map_to_int() : Convert the elements in the Stream to an Integer
map_to_int() : Convert the elements in the Stream to an IntegerReturns 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.
Stream.of([1, "2", 3.0]) \
    .map_to_int() \
    .to_list() # [1, 2, 3]map_to_str() : Convert the elements in the Stream to a String
map_to_str() : Convert the elements in the Stream to a StringReturns a stream consisting of the results of applying the str() function to the elements of this stream.
Stream.of([1, 2, 3]) \
    .map_to_str() \
    .to_list() # ["1", "2", "3"]numeric() : Convert the stream to a NumericStream
numeric() : Convert the stream to a NumericStreamReturns a NumericStream consisting of the same elements as the stream contained before conversion.
Stream.of([1, "2", 3.0]) \
    .map(int) \
    .numeric() \
    .sum() # 6parallel() : Convert the stream to a ParallelStream
parallel() : Convert the stream to a ParallelStreamReturns a ParallelStream consisting of the same elements as the stream contained before conversion.
Stream.sequential_of([1, 2, 3]) \
    .parallel() \
    .map_to_str() \
    .for_each(print) # "1", "2", "3" (mapped in parallel mode)peek() : View intermediate results
peek() : View intermediate resultsReturns 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.
Stream.of([2, 1, 3]) \
    .sorted() \
    .peek(print) \ # 1, 2, 3
    .reversed() \
    .for_each(print) # 3, 2, 1reversed() : Reverse Stream
reversed() : Reverse StreamReturns a stream consisting of the elements of this stream in reverse order.
Stream.of([1, 2, 3]) \
    .reversed() \
    .to_list()  # [3, 2, 1]sequential() : Convert the stream to a SequentialStream
sequential() : Convert the stream to a SequentialStreamReturns a SequentialStream consisting of the same elements as the stream contained before conversion.
Stream.parallel_of([1, 2, 3]) \
    .sequential() \
    .map_to_str() \
    .for_each(print) # "1", "2", "3" (mapped in sequential mode)skip() : Skip the first n elements of the Stream
skip() : Skip the first n elements of the StreamReturns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream.
Stream.of([1, 2, 3]) \
    .skip(2) \
    .to_list() # [3]sorted() : Sort Stream
sorted() : Sort StreamReturns a stream consisting of the elements of this stream, sorted according to natural order or comparator.
Stream.of([2, 9, 1]) \
    .sorted() \
    .to_list()  # [1, 2, 9]Here is an example with a custom comparator:
Stream.of(["a", "cc", "bbb"]) \
    .sorted(lambda x, y: len(y) - len(x)) \
    .to_list()  # ['bbb', 'cc', 'a']take_while() : Take elements while the predicate is true
take_while() : Take elements while the predicate is trueReturns, if this stream is ordered, a stream consisting of the longest prefix of elements taken from this stream that match the given predicate.
Stream.of([1, 2, 3]) \
    .take_while(lambda x: x < 3) \
    .to_list() # [1, 2]Last updated
Was this helpful?