Skip to content

Commit 41cbb47

Browse files
committed
ch03 Streams cleanup
1 parent 550ef5c commit 41cbb47

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

03-streams.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ Intermediate operations allows you to build the pipeline which gets executed whe
176176

177177
<a href="https://whyjava.files.wordpress.com/2015/07/stream-api.png"><img class="aligncenter size-full wp-image-2983" src="https://whyjava.files.wordpress.com/2015/07/stream-api.png" alt="stream-api" height="450" /></a>
178178

179-
## Example domain
179+
### Example domain
180180

181181
Throughout this tutorial we will use Task management domain to explain the concepts. Our example domain has one class called Task -- a task to be performed by user. The class is shown below.
182182

@@ -214,7 +214,7 @@ List<Task> tasks = Arrays.asList(task1, task2, task3, task4, task5);
214214

215215
> We will not discuss about Java 8 Date Time API in this chapter. For now, just think of as the fluent API to work with dates.
216216
217-
### Example 1 -- Find all reading task titles sorted by their creation date
217+
### Example 1: Find all reading task titles sorted by their creation date
218218

219219
The first example that we will discuss is to find all the reading task titles sorted by creation date. The operations that we need to perform are:
220220

@@ -275,7 +275,7 @@ public List<String> allReadingTasksSortedByCreatedOnDesc(List<Task> tasks) {
275275
}
276276
```
277277

278-
### Example 2 -- Find distinct tasks
278+
### Example 2: Find distinct tasks
279279

280280
Suppose, we have a dataset which contains duplicate tasks. We can very easily remove the duplicates and get only distinct elements by using the `distinct` method on the stream as shown below.
281281

@@ -287,7 +287,7 @@ public List<Task> allDistinctTasks(List<Task> tasks) {
287287

288288
The `distinct()` method converts one stream into another without duplicates. It uses the Object's `equals` method for determining the object equality. According to Object's equal method contract, when two objects are equal, they are considered duplicates and will be removed from the resulting stream.
289289

290-
### Example 3 -- Find top 5 reading tasks sorted by creation date
290+
### Example 3: Find top 5 reading tasks sorted by creation date
291291

292292
The `limit` function can be used to limit the result set to a given size. `limit` is a short circuiting operation which means it does not evaluate all the elements to find the result.
293293

@@ -344,7 +344,7 @@ private static List<String> allDistinctTags(List<Task> tasks) {
344344
}
345345
```
346346

347-
### Example 6 -- Check if all reading tasks have tag `books`
347+
### Example 6: Check if all reading tasks have tag `books`
348348

349349
Stream API has methods that allows the users to check if elements in the dataset match a given property. These methods are `allMatch`, `anyMatch`, `noneMatch`, `findFirst`, and `findAny`. To check if all reading titles have a tag with name `books` we can write code as shown below.
350350

@@ -366,7 +366,7 @@ public boolean isAnyReadingTasksWithTagJava8(List<Task> tasks) {
366366
}
367367
```
368368

369-
### Example 7 -- Creating a summary of all titles
369+
### Example 7: Creating a summary of all titles
370370

371371
Suppose, you want to create a summary of all the titles then you can use `reduce` operation, which reduces the stream to a value. The `reduce` function takes a lambda which joins elements of the stream.
372372

@@ -379,7 +379,7 @@ public String joinAllTaskTitles(List<Task> tasks) {
379379
}
380380
```
381381

382-
### Example 8 - Working with primitive Streams
382+
### Example 8: Working with primitive Streams
383383

384384
Apart from the generic stream that works over objects, Java 8 also provides specific streams that work over primitive types like int, long, and double. Let's look at few examples of primitive streams.
385385

@@ -412,9 +412,10 @@ We can limit the resulting stream by using the `limit` operation as shown below.
412412
infiniteStream.filter(el -> el % 2 == 0).limit(100).forEach(System.out::println);
413413
```
414414

415-
### Creating Streams from Arrays
415+
### Example 9: Creating Streams from Arrays
416416

417417
You can create streams from arrays by using the static `stream` method on the `Arrays` class as shown below.
418+
418419
```java
419420
String[] tags = {"java", "git", "lambdas", "machine-learning"};
420421
Arrays.stream(tags).map(String::toUpperCase).forEach(System.out::println);

0 commit comments

Comments
 (0)