You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: collection-pipeline/README.md
+33-40Lines changed: 33 additions & 40 deletions
Original file line number
Diff line number
Diff line change
@@ -3,8 +3,10 @@ title: Collection Pipeline
3
3
category: Functional
4
4
language: en
5
5
tag:
6
-
- Reactive
6
+
- Functional decomposition
7
7
- Data processing
8
+
- Data transformation
9
+
- Reactive
8
10
---
9
11
10
12
## Intent
@@ -15,7 +17,7 @@ The Collection Pipeline design pattern is intended to process collections of dat
15
17
16
18
Real-world example
17
19
18
-
> Imagine you're in a large library filled with books, and you're tasked with finding all the science fiction books published after 2000, then arranging them by author name in alphabetical order, and finally picking out the top 5 based on their popularity or ratings.
20
+
> Imagine a real-world example of a factory assembly line for manufacturing cars. In this assembly line, each station performs a specific task on the car chassis, such as installing the engine, painting the body, attaching the wheels, and inspecting the final product. Each station takes the output from the previous station and adds its own processing step. This sequence of operations is analogous to the Collection Pipeline design pattern, where each step in the pipeline transforms the data and passes it on to the next step, ensuring an efficient and organized workflow.
19
21
20
22
In plain words
21
23
@@ -27,63 +29,56 @@ Wikipedia says
27
29
28
30
**Programmatic Example**
29
31
30
-
The Collection Pipeline pattern is implemented in this code example by using Java's Stream API to perform a series of transformations on a collection of Car objects. The transformations are chained together to form a pipeline. Here's a breakdown of how it's done:
31
-
32
-
1. Creation of Cars: A list of Car objects is created using the `CarFactory.createCars()` method.
32
+
The Collection Pipeline is a programming pattern where you organize some computation as a sequence of operations which compose by taking a collection as output of one operation and feeding it into the next.
33
33
34
-
`var cars = CarFactory.createCars();`
34
+
Here's a programmatic example of the Collection Pipeline design pattern:
35
35
36
-
2. Filtering and Transforming: The `FunctionalProgramming.getModelsAfter2000(cars)` method filters the cars to only include those made after the year 2000, and then transforms the filtered cars into a list of their model names.
In the `getModelsAfter2000` method, the pipeline is created as follows:
38
+
We start with a list of `Car` objects and we want to filter out those that were manufactured after the year 2000. This is done using the `stream()` method to create a stream from the list, the `filter()` method to filter out the cars we want, and the `collect()` method to collect the results into a new list.
.collect(groupingBy(Car::getCategory)); // Group cars by category
58
+
}
61
59
```
62
60
63
-
4.Filtering, Sorting and Transforming: The `FunctionalProgramming.getSedanCarsOwnedSortedByDate(List.of(john))` method filters the cars owned by a person to only include sedans, sorts them by date, and then transforms the sorted cars into a list of Car objects.
In the `getSedanCarsOwnedSortedByDate` method, the pipeline is created as follows:
63
+
Finally, we want to filter the cars owned by a person to only include sedans, sort them by date, and then transform the sorted cars into a list of `Car` objects.
.flatMap(person -> person.getCars().stream()) // Flatten the list of cars owned by each person
69
+
.filter(car ->Category.SEDAN.equals(car.getCategory())) // Filter to only include sedans
70
+
.sorted(comparing(Car::getDate)) // Sort the cars by date
71
+
.collect(toList()); // Collect the results into a new list
72
+
}
76
73
```
77
74
78
75
In each of these methods, the Collection Pipeline pattern is used to perform a series of operations on the collection of cars in a declarative manner, which improves readability and maintainability.
This pattern is applicable in scenarios involving bulk data operations such as filtering, mapping, sorting, or reducing collections. It's particularly useful in data analysis, transformation tasks, and where a sequence of operations needs to be applied to each element of a collection.
79
+
* When you need to perform a series of transformations on a collection of data.
80
+
* When you want to improve readability and maintainability of complex data processing code.
81
+
* When working with large datasets where intermediate results should not be stored in memory.
87
82
88
83
## Known Uses
89
84
@@ -115,9 +110,7 @@ Trade-offs:
115
110
116
111
## Credits
117
112
118
-
*[Function composition and the Collection Pipeline pattern](https://www.ibm.com/developerworks/library/j-java8idioms2/index.html)
119
-
*[Collection Pipeline described by Martin Fowler](https://martinfowler.com/articles/collection-pipeline/)
0 commit comments