Stream Hierarchy and Methods

Stream Hierarchy and Methods: Understanding the Different Interfaces and Methods Available in Java Streams

Stream Hierarchy and Methods


Java streams are a powerful feature of the Java programming language that allow developers to manipulate collections of data in a concise and flexible way. In this article, we will explore the different interfaces and methods available in Java streams.

  1. The BaseStream Interface

The BaseStream interface is the parent interface for all Java streams. It provides three important methods that can be used to manipulate the stream:

  • parallel(): This method is used to create a parallel version of the current stream. A parallel stream is one that can be processed concurrently, which can significantly improve the performance of certain types of operations.

  • sequential(): This method is used to create a sequential version of the current stream. A sequential stream is one that is processed in a single thread, which can be useful in certain situations where parallel processing is not necessary.

  • unordered(): This method is used to create an unordered version of the current stream. An unordered stream is one that does not guarantee the order of the elements in the stream. This can be useful in certain types of operations where the order of the elements is not important and parallel processing is desired.
  1. The IntStream, LongStream, and DoubleStream Interfaces

The IntStream, LongStream, and DoubleStream interfaces are sub-interfaces of the BaseStream interface that are used to work with primitive types in a stream. These interfaces provide methods that are specific to their respective primitive types.

For example, the IntStream interface provides the following methods:

  • sum(): This method is used to calculate the sum of the elements in the stream.
  • average(): This method is used to calculate the average of the elements in the stream.
  • boxed(): This method is used to convert a primitive stream of integers into a stream of Integer objects.

Similarly, the LongStream interface provides the sum(), average(), and boxed() methods for primitive longs, while the DoubleStream interface provides these methods for primitive doubles.

  1. The Stream Interface

The Stream interface is the final sub-interface of the BaseStream interface. It is used to work with non-primitive types in a stream. The Stream interface provides a variety of methods that can be used to manipulate the elements in the stream.

Some of the most commonly used methods in the Stream interface include:

  • filter(): This method is used to select elements from the stream based on a specified Predicate.
  • forEach(): This method is used to iterate over every element in the stream and perform a specified action on each element.
  • min(): This method is used to find the minimum element in the stream.
  • max(): This method is used to find the maximum element in the stream.
  • skip(): This method is used to skip a specified number of elements in the stream.
  • findFirst(): This method is used to find the first element in the stream.
  • count(): This method is used to count the number of elements in the stream.
  • distinct(): This method is used to return a new stream that contains only the distinct elements from the original stream.
  • sorted(): This method is used to return a new stream that contains the elements of the original stream sorted in ascending order.
  • reduce(): This method is used to combine all the elements in the stream into a single value, either by concatenating strings or by adding up numbers.
  • collect(): This method is used to convert the stream into a collection.

It's important to note that all non-terminal operations in a stream return a new modified stream as output, while terminal operations do not return a stream at all.

In conclusion, Java streams provide a powerful and flexible way to manipulate collections of data. By understanding the different interfaces and methods available in Java streams, developers can take advantage of the full power of this feature and write more concise and efficient code.

Previous Post Next Post