An open API service indexing awesome lists of open source software.

https://github.com/chaseofthejungle/java-streams-troubleshooting-guide

An overview of common Java errors and bottlenecks, and how to resolve them.
https://github.com/chaseofthejungle/java-streams-troubleshooting-guide

java java-streams

Last synced: 24 days ago
JSON representation

An overview of common Java errors and bottlenecks, and how to resolve them.

Awesome Lists containing this project

README

          

# Java Streams Troubleshooting Guide


#### Table of Contents:

1. [Overview of Java Streams](#streams)
2. [Mistake #1: Utilizing Streams with Non-Complex Iterations](#one)
3. [Mistake #2: Omitting .close() Method for Stream Sources](#two)
4. [Mistake #3: Improper Handling of Terminal/Console Operations](#three)
5. [Mistake #4: Too Many Parallel Streams](#four)
6. [Mistake #5: Modifying External Variable State](#five)
7. [Mistake #6: Null Values in Streams Not Being Handled](#six)
8. [Mistake #7: Suboptimal Implementation of Intermediate Operations](#seven)
9. [Mistake #8: .collect() Improperly Utilized for Mutable Reduction Scenarios](#eight)
10. [Mistake #9: Forgetting to Utilize sorted() Method with limit()](#nine)
11. [Mistake #10: Overlooking 'Optional' with the findAny() and findFirst() Methods](#ten)
12. [Supplemental Resources](#supplemental)




## 1. Overview of Java Streams

**Java streams** were introduced in Java 8 as a mechanism for processing data sequences and collections in a concise and functional manner, including for complex operations. While streams are not collections for element storage (unlike data structures), they are abstractions of (immutable) function collections. Thus, although pointers to stream locations to retrieve elements is not realized, functions for performing data operations can be specified, transforming streams and their origin streams.

Streams are still commonly used today, as they are highly readable and processor efficient (streams evaluate only when they are computed, and can optimize multi-core processors).




## 2. Mistake #1: Utilizing Streams with Non-Complex Iterations

Instead of:

```
List firstNames = Arrays.asList("Tyler", "Barry", "Sophia");
firstNames.forEach(System.out::println);
```

Restrict stream usage to more complex tasks, such as reducing, mapping, and filtering.


## 3. Mistake #2: Omitting .close() Method for Stream Sources


## 4. Mistake #3: Improper Handling of Terminal/Console Operations


## 5. Mistake #4: Too Many Parallel Streams

Overuse of ```.parallel()``` calls for input and output handling can result in performance degradation.

Best Practice: Reserve use of parallel streams for more intensive CPU-bound jobs.


## 6. Mistake #5: Modifying External Variable State


## 7. Mistake #6: Null Values in Streams Not Being Handled


## 8. Mistake #7: Suboptimal Implementation of Intermediate Operations


## 9. Mistake #8: .collect() Improperly Utilized for Mutable Reduction Scenarios


## 10. Mistake #9: Forgetting to Utilize sorted() Method with limit()


## 11. Mistake #10: Overlooking 'Optional' with the findAny() and findFirst() Methods




## 12. Supplemental Resources

* *[Stream Documentation from Official Java Platform SE 8 API](https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html)*
* *[Java Clean Coding Guide](https://github.com/chaseofthejungle/java-clean-coding-guide)*
* *[Java Quick Reference Guide](https://github.com/chaseofthejungle/java-quick-reference-guide)*




TODO: Overview of 10 common Java stream errors, and how to resolve them with refined code.