Stream

Cards (33)

  • The concept of Stream is integrated/introduced in Java 8.
  • The Stream API is used to process collections of objects.
  • A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result.
  • A stream is not a data structure instead it takes input from the Collections, Arrays or I/O channels.
  • Streams don’t change the original data structure, they only provide the result as per the pipelined methods.
  • The Stream Classes in Java supports Functional Programming Paradigm and Parallel Programming
  • Intermediate operations return a stream as a result
  • Intermediate operations can be chained together to form a pipeline of operations.
  • Pipeline of operations may contain any number of intermediate operations, but there has to be only one terminal operation, that too at the end of pipeline.
  • Intermediate operations are lazily loaded. 

    When you call intermediate operations, they are actually not executed.
  • When you call intermediate operations, they are actually not executed.

    They are just stored in the memory and executed when the terminal operation is called on the stream.
  • Intermediate operations don’t give end result.
    They just transform one stream to another stream.
  • Terminal operations return non-stream values like primitive or object or collection or may not return anything.
  • Terminal operations cannot be chained together.
  • Terminal operations give end result.
  • filter()
    Returns a stream of elements which satisfy the given predicate.
  • map()
    Returns a stream consisting of results after applying given function to
    elements of the stream.
  • distinct()

    Returns a stream of unique elements.
  • sorted()

    Returns a stream consisting of elements sorted according to natural
    order.
  • limit()
    Returns a stream containing first n elements.
  • skip()
    Returns a stream after skipping first n elements.
  • forEach()
    Performs an action on all elements of a stream.
  • toArray()
    Returns an array containing elements of a stream.
  • reduce()

    Performs reduction operation on elements of a stream using initial value and binary operation.
  • collect()

    Returns mutable result container such as List or Set.
  • min()

    Returns minimum element in a stream wrapped in an Optional object.
  • max()

    Returns maximum element in a stream wrapped in an Optional object.
  • count()
    Returns the number of elements in a stream.
  • anyMatch()

    Returns true if any one element of a stream matches with given predicate.
  • allMatch
    Returns true if all the elements of a stream matches with given predicate.
  • noneMatch()
    Returns true only if all the elements of a stream doesn't match with given predicate.
  • findFirst()
    Returns first element of a stream wrapped in an Optional object.
  • findAny()
    Randomly returns any one element in a stream.