|
1 | | -# About |
| 1 | +# About Streams |
2 | 2 |
|
3 | | -Streams in Java offer a modern, functional approach to processing data. Instead of writing verbose loops, you can build pipelines that transform collections with clarity and elegance. |
| 3 | +**Streams** are a functional abstraction for processing sequences of data in Java. |
| 4 | +Unlike collections like [`List`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/List.html), a [`Stream`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/stream/Stream.html) does not store elements—it describes a pipeline of operations to transform or compute data. |
4 | 5 |
|
5 | | -Streams are built on three key components: |
| 6 | +Streams are typically created from collections, arrays, or manually using `Stream.of(...)`. |
6 | 7 |
|
7 | | -1. **Source** – Typically a collection like a `List` or `Set`. |
8 | | -2. **Intermediate Operations** – Transformations such as `filter`, `map`, or `sorted`. |
9 | | -3. **Terminal Operation** – Produces a result, like `collect`, `count`, or `forEach`. |
| 8 | +For example: |
10 | 9 |
|
11 | 10 | ```java |
12 | | -List<String> names = List.of("Dharshini", "Naveen", "Selena"); |
13 | | - |
14 | | -List<String> filtered = names.stream() |
15 | | - .filter(name -> name.startsWith("N")) |
16 | | - .collect(Collectors.toList()); |
17 | | - |
18 | | -// => ["Naveen"] |
| 11 | +Stream<String> emptyStream = Stream.of(); |
| 12 | +Stream<Integer> singleInteger = Stream.of(1); |
| 13 | +Stream<Boolean> threeBooleans = Stream.of(true, false, true); |
| 14 | +Stream<Object> mixedTypes = Stream.of("hello", 1, true); // allowed in Stream<Object> |
19 | 15 | ``` |
20 | 16 |
|
21 | 17 |
|
22 | | -[Java Stream API Overview]:https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html |
23 | | -[Collectors Class]:https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html |
24 | | -[Java Functional Programming with Streams]:https://www.baeldung.com/java-8-streams |
| 18 | +[Java Stream API Overview]:https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/stream/package-summary.html |
| 19 | +[Collectors Class]:https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/stream/Collectors.html |
| 20 | +[Stream Interface Documentation]:https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/stream/Stream.html |
25 | 21 | [Stream Operations Explained]:https://www.geeksforgeeks.org/stream-in-java/ |
0 commit comments