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: 04-collectors.md
+15-14Lines changed: 15 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,13 @@
1
1
Collectors
2
2
------
3
3
4
-
On [day 2](http://shekhargulati.com/2015/07/26/day-2-lets-learn-about-streams/), you learned that Stream API can help you work with collections in a declarative manner. We looked at the`collect`, which is a terminal operation that collects the result set of a stream pipeline in a `List`. `collect` is a reduction operation that reduces a stream to a value. The value could be a Collection, Map, or a value object. You can use `collect` to achieve following:
4
+
On [day 2](http://shekhargulati.com/2015/07/26/day-2-lets-learn-about-streams/), you learned that the Stream API can help you work with collections in a declarative manner. We looked at `collect`, which is a terminal operation that collects the result set of a stream pipeline in a `List`. `collect` is a reduction operation that reduces a stream to a value. The value could be a Collection, Map, or a value object. You can use `collect` to achieve following:
5
5
6
6
1.**Reducing stream to a single value:** Result of the stream execution can be reduced to a single value. Single value could be a `Collection` or numeric value like int, double, etc or a custom value object.
7
7
8
-
2.**Group elements in a stream:** Group all the tasks in a stream by TaskType. This will result in a `Map<TaskType, List<Task>>` with each entry containing a TaskType and its associated Tasks. You can use any other Collection instead of a List as well. If you don't need all the tasks associated with a TaskType you can also produce `Map<TaskType, Task>` as well. One example could be grouping tasks by type and obtaining the first created task.
8
+
2.**Group elements in a stream:** Group all the tasks in a stream by TaskType. This will result in a `Map<TaskType, List<Task>>` with each entry containing a TaskType and its associated Tasks. You can use any other Collection instead of a List as well. If you don't need all the tasks associated with a TaskType, you can alternatively produce a `Map<TaskType, Task>`. One example could be grouping tasks by type and obtaining the first created task.
9
9
10
-
3.**Partition elements in a stream:** You can partition a stream into two groups -- due and completed tasks.
10
+
3.**Partition elements in a stream:** You can partition a stream into two groups -- e.g. due and completed tasks.
The code shown above uses `groupingBy``Collector` defined in the `Collectors` utility class. It creates a Map with key as the `TaskType` and value as the list containing all the tasks which have same `TaskType`. To achieve the same in Java 7 you have to write following code.
22
+
The code shown above uses `groupingBy``Collector` defined in the `Collectors` utility class. It creates a Map with key as the `TaskType` and value as the list containing all the tasks which have same `TaskType`. To achieve the same in Java 7, you would have to write the following.
23
23
24
24
```java
25
25
publicstaticvoid main(String[] args) {
@@ -43,7 +43,7 @@ public static void main(String[] args) {
43
43
44
44
## Collectors: Common reduction operations
45
45
46
-
`Collectors` utility class provides a lot of static utility methods for creating collectors for most common use cases like accumulating elements into a Collection, grouping and partitioning elements, summarizing elements according to various criteria. We will cover most common `Collector`s in this blog.
46
+
The `Collectors` utility class provides a lot of static utility methods for creating collectors for most common use cases like accumulating elements into a Collection, grouping and partitioning elements, or summarizing elements according to various criteria. We will cover the most common `Collector`s in this blog.
47
47
48
48
## Reducing to a single value
49
49
@@ -76,7 +76,7 @@ public Set<String> uniqueTitles(List<Task> tasks) {
76
76
}
77
77
```
78
78
79
-
`toSet` method uses `HashSet` as the Set implementation to store the result set.
79
+
The `toSet` method uses a`HashSet` as the Set implementation to store the result set.
We can improve the code shown above by using the `identity` default method in the `Function` interface to make code cleaner and better convey developer intent to use identity function as shown below.
90
+
We can improve the code shown above by using the `identity` default method in the `Function` interface to make code cleaner and better convey developer intent, as shown below.
at java.util.stream.Collectors.lambda$throwingMerger105ドル(Collectors.java:133)
105
105
```
106
106
107
-
You can handle duplicates by using another variant of the `toMap` function which allows us to specify a merge function. The merge function allows a client to specify how they want to resolve collisions between values associated with the same key. In the code shown below, we just used the last value but you can write intelligent algorithm to resolve the collision.
107
+
You can handle duplicates by using another variant of the `toMap` function which allows us to specify a merge function. The merge function allows a client to specify how they want to resolve collisions between values associated with the same key. In the code shown below, we just used the newer value, but you can equally write an intelligent algorithm to resolve collisions.
@@ -120,11 +120,11 @@ public Map<String, Task> collectToMap(List<Task> tasks) {
120
120
}
121
121
```
122
122
123
-
Similar to the `toMap` collector there is also `toConcurrentMap` collector that produces `ConcurrentMap` instead of a `HashMap`.
123
+
Similar to the `toMap` collector, there is also `toConcurrentMap` collector, which produces a `ConcurrentMap` instead of a `HashMap`.
124
124
125
125
### Using other collections
126
126
127
-
The specific collectors like `toList` and `toSet`does not allow you to specify the underlying List or Set implementation. You can use `toCollection` collector when you want to collect the result to other types of collections as shown below.
127
+
The specific collectors like `toList` and `toSet`do not allow you to specify the underlying List or Set implementation. You can use the `toCollection` collector when you want to collect the result to other types of collections, as shown below.
@@ -162,7 +162,7 @@ One of the most common use case of Collector is to group elements. Let's look at
162
162
163
163
### Example 1: Grouping tasks by type
164
164
165
-
Let's look the example shown below where we want to group all the tasks based on their `TaskType`. You can very easily perform this task by using the `groupingBy` Collector of the `Collectors` utility class as shown below. You can make it more succinct by using method references and static imports.
165
+
Let's look at the example shown below, where we want to group all the tasks based on their `TaskType`. You can very easily perform this task by using the `groupingBy` Collector of the `Collectors` utility class. You can make it more succinct by using method references and static imports.
There are times when you want to partition a dataset into two dataset based on a predicate. For example, we can partition tasks into two groups by defining a partitioning function that partition tasks into two groups -- one with due date before today and one with due date after today.
229
+
There are times when you want to partition a dataset into two datasets based on a predicate. For example, we can partition tasks into two groups by defining a partitioning function that partitions tasks into two groups -- one with due date before today, and one with the others.
@@ -236,7 +236,8 @@ private static Map<Boolean, List<Task>> partitionOldAndFutureTasks(List<Task> ta
236
236
237
237
## Generating statistics
238
238
239
-
Another group of collectors that are very helpful are collectors that produce statistics. These work on the primitive datatypes like int,double, long and can be used to produce statistics like the one shown below.
239
+
Another group of collectors that are very helpful are collectors that produce statistics. These work on the primitive datatypes like `int`, `double`, and `long`; and can be used to produce statistics like those shown below.
0 commit comments