Streams

  • Declarative/Functional wrapper to operative on data sources, to make bulk operations fast and quick.
  • An interface not concrete implementation

Different ways to create a stream

  • using stream() from collection
// Stream from collection
List<String> fruitsCollection = Arrays.asList("apple", \
                                        "mango", \
                                        "oranges", \
                                        "grape", \
                                        "guava");
Stream fruitsStream = fruitsCollection.stream();
  • using stream.of(...)
// Stream creation from specific value
Stream alphabetStream = Stream.of("a", "b", "c");
  • using Arrays.stream()
// Stream from array
String[] fruitsArray = ["apple", "oranges", "mango"];
Stream fruitsStream = Arrays.stream(fruitsArray);
  • using stream.generate() for infinite stream
// Infinite Stream
Stream randomNumberStream = Stream.generate( () -> Math.random() );
Last Updated:
Contributors: Jubin Mathew