Streams in Java 8

Streams API is a new feature introduced in Java 8. Streams API is used to process collection of objects. Streams are functional in nature, lazy, does not store elements. Streams conveys elements from a sources through a pipeline of computational operations. The sources can be a data structure or an array or an I/O channel. The ordering of elements are maintained as it is in the source.

To work with streams one needs to familiar with the below 3 stages.



1. Create streams.
2. Perform intermediate operations
3. Perform terminal operation

Create streams:

The below ways are used to create streams.

1. Stream.of(val1,val2,val3)
2. Stream.of(ArrayOfElements)
3. Stream.generate()
4. Stream.iterate()
5. list.stream()
6. String chars (using chars() function of String class)

Intermediate operations:

Intermediate operations transform one stream into another steam. Thus the result of intermediate operations are always be a steam. A terminal operation is followed to complete the necessary outcome. The below are some of the intermediate operations supported by Steams API.

1. filter()
2. map()
3. sorted()
4. flatmap()
5. distinct()
6. peek()
7. limit()
8. skip()

Terminal Operation

Terminal operation returns a certain type instead of a stream. The below are some of terminal operations supported by Streams API.

1. collect()
2. reduce()
3. find()
4. count()
5. min()
6. max()
7. forEach()
8. anyMatch()
9. allMatch()
10. noneMatch()
11. forEachOrdered()
12. toArray()



Share/Bookmark

0 comments:

Post a Comment

Your suggestions are welcome:

Followers

Networked Followers

TopOfBlogs

  © Copyright Techwebbee by Techwebbee 2010

Back to TOP