Parallel Streams#
TODO: Add this to Practice_Streams
// let map be Map<String, Integer>.
// Use streams to output the keys that have an even value,
// sort by the keys, case-insensitive.
// output using `forEach` and a Method Reference to println.
List<String> result = map.entrySet().stream()
.filter(entry -> entry.getValue() % 2 == 0) // keep even values
.sorted(Comparator.comparing(entry -> entry.getKey().toLowerCase())) // sort by key, case-insensitive
.map(Map.Entry::getKey) // extract keys only
.collect(Collectors.toList());
result.forEach(System.out::println);
What is a Stream?#
A Stream is a sequence of elements processed in a functional style.
Key Characteristics of Streams
Not a Data Structure: Unlike collections (e.g., List, Set), a Stream does not store elements. Instead, it provides a way to process elements from a source.
Lazy Evaluation: Stream operations are performed lazily, meaning they are not executed until a terminal operation (like collect, forEach, or reduce) is invoked.
Functional Style: Streams rely on functional-style operations, such as filter, map, and reduce, which can be chained together to form a pipeline of operations.
Parallel Processing: Streams can be processed in parallel, making it easier to write parallel code and improve performance.
References
[1] Java Streams Tutorial | 2020 [2] Java 8 Stream API Tutorial [3] Master Java Streams: Complete Guide from Basics to Advanced in One Video! [4] The Java Stream API Tutorial - Baeldung [5] Java Stream API: Real-world Examples for Beginners - HowToDoInJava [6] Java - Streams - Online Tutorials Library [7] Streams in Java- Complete Tutorial with Examples [8] https://topmate.io/engineeringdigest [9] https://razorpay.me/@engineeringdigest [10] https://www.youtube.com/@EngineeringDigest/join [11] https://discord.oia.bio/engineeringdigest [12] https://insta.oia.bio/engineering-digest [13] https://x.openinapp.co/thevipulvats