Java 8 Streams By Examples

Whenever we hear about Java 8 Streams the first thing which strike in mind would be Java I/O stream and classes like InputStream and OutputStream. But here in Java 8 Stream is quite different concept than Java I/O Streams. Java 8 Stream best implementation of functional programming which can be used easily with Java Collection Framework.

What is Stream in Java 8?

Stream in Java 8 can be considered as an expressive syntax structure which supports functional-style operations such as filter, map-reduce transformations on elements of collections. Stream can be defined as a chain of various functional operations on various data sources and Java Collection except java.util.Map.

Let’s have an example of it. We have one Employee class with Name and Salary Information.

We have to filter employees with salary greater than 100K, than sort all employees on salary in ascending order and print name and salary of filtered employees in sorted order.

Let’s try this in imperative style using Collection classes.

Same code using Functional Style in Java 8

Code using streams seems to be more expressive, compact and readable.

Important properties of Java 8 Streams

  • Streams are created from various sources like BufferedReader, Arrays, Lists, Sets etc. Majority of them are Collection classes
  • Stream operations are of two types,
    • Intermediate (Transformation)
    • Termination (Collection, Aggregation)
  • Streams carry values from data source to chained operations
  • Chained operations in a stream take lambda expression as an argument
  • Streams don’t have capabilities to store intermediate data
    • That means while applying any lambda function it doesn’t create new set of data. It just create chain of operations and execute the lambda expression if it is requested by termination operation
  • Streams supports Lazy evaluation
    • For example Stream is created from a List of 10 elements, there are 3 Intermediate operation, and as a Termination operation findFirst() method is placed. So rather than executing every Intermediate operation 10 times and giving first element final result. Stream will just identify the element by findFirst() method and execute all intermediate methods just once. This is called Lazy Evaluation.
  • Mostly Streams are considered to be immutable as majority the operations in Streams are stateless (Still it is dependent upon the programmer)
  • We can access first and/or next element from source as Streams are not supporting indexes
  • You can create Lists or Arrays from Streams as an output using Termination operation
  • Stream are not reusable after calling a termination operation.
  • Stream can also be wrapped around data sources which generate infinite elements

I hope this article would be add some value to you. Stay tuned for such more articles on Java 8.

Leave a Reply

Notify of