Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 59e5948

Browse files
blakehawkinsshekhargulati
authored andcommitted
improve grammar in section 4 (#8)
1 parent 56132a9 commit 59e5948

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

‎04-collectors.md‎

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
Collectors
22
------
33

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:
55

66
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.
77

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.
99

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.
1111

1212
## Collector in Action
1313

@@ -19,7 +19,7 @@ private static Map<TaskType, List<Task>> groupTasksByType(List<Task> tasks) {
1919
}
2020
```
2121

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 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.
2323

2424
```java
2525
public static void main(String[] args) {
@@ -43,7 +43,7 @@ public static void main(String[] args) {
4343

4444
## Collectors: Common reduction operations
4545

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.
4747

4848
## Reducing to a single value
4949

@@ -76,7 +76,7 @@ public Set<String> uniqueTitles(List<Task> tasks) {
7676
}
7777
```
7878

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.
8080

8181
### Collecting data into a Map
8282

@@ -87,7 +87,7 @@ private static Map<String, Task> taskMap(List<Task> tasks) {
8787
return tasks.stream().collect(toMap(Task::getTitle, task -> task));
8888
}
8989
```
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 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.
9191

9292
```java
9393
import static java.util.function.Function.identity;
@@ -104,7 +104,7 @@ Exception in thread "main" java.lang.IllegalStateException: Duplicate key Task{t
104104
at java.util.stream.Collectors.lambda$throwingMerger105ドル(Collectors.java:133)
105105
```
106106

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.
108108

109109
```java
110110
private static Map<String, Task> taskMap_duplicates(List<Task> tasks) {
@@ -120,11 +120,11 @@ public Map<String, Task> collectToMap(List<Task> tasks) {
120120
}
121121
```
122122

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`.
124124

125125
### Using other collections
126126

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.
128128

129129
```
130130
private static LinkedHashSet<Task> collectToLinkedHaskSet(List<Task> tasks) {
@@ -162,7 +162,7 @@ One of the most common use case of Collector is to group elements. Let's look at
162162

163163
### Example 1: Grouping tasks by type
164164

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.
166166

167167
```java
168168
import static java.util.stream.Collectors.groupingBy;
@@ -226,7 +226,7 @@ private static Map<TaskType, Map<LocalDate, List<Task>>> groupTasksByTypeAndCrea
226226

227227
## Partitioning
228228

229-
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.
230230

231231
```java
232232
private static Map<Boolean, List<Task>> partitionOldAndFutureTasks(List<Task> tasks) {
@@ -236,7 +236,8 @@ private static Map<Boolean, List<Task>> partitionOldAndFutureTasks(List<Task> ta
236236

237237
## Generating statistics
238238

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.
240+
240241
```java
241242
IntSummaryStatistics summaryStatistics = tasks.stream().map(Task::getTitle).collect(summarizingInt(String::length));
242243
System.out.println(summaryStatistics.getAverage()); //32.4
@@ -330,7 +331,7 @@ public class MultisetCollectorExample {
330331

331332
## Word Count in Java 8
332333

333-
We will end this section by writing famous word count example in Java 8 using Streams and Collectors.
334+
We will end this section by writing the famous word count example in Java 8 using Streams and Collectors.
334335

335336
```java
336337
public static void wordCount(Path path) throws IOException {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /