JAVA Stream API with examples- Part 1
Stream API provides a functional approach to processing collections of objects. Stream API is not related to Java I/O streams (FileInputStream and FileOutputStream).
Java I/O streams are related to streams of bytes whereas Stream API is for processing streams of objects, not bytes.
Simply put, streams are wrappers around a data source, allowing us to operate with that data source and making bulk processing convenient and fast. Streams do not provide a means to directly access or manipulate the elements of the data sources.
To perform a computation, stream operations are composed into a stream pipeline. A stream pipeline consists of a source (which might be an array, a collection, a generator function, and an I/O channel, etc), zero or more intermediate operations (which transform a stream into another stream, such as filter(Predicate)
), and a terminal operation (which produces a result or side-effect, such as count()
or forEach(Consumer)
).
Stream creation:
We can create a stream using Stream.of(),Collection.stream() and Stream.builder().
1.Stream creation from an existing array,
2. Stream creation from an existing list,
3. Stream creation using Stream.Builder(),