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
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.
> 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.
216
216
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
218
218
219
219
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:
220
220
@@ -275,7 +275,7 @@ public List<String> allReadingTasksSortedByCreatedOnDesc(List<Task> tasks) {
275
275
}
276
276
```
277
277
278
-
### Example 2 -- Find distinct tasks
278
+
### Example 2: Find distinct tasks
279
279
280
280
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.
281
281
@@ -287,7 +287,7 @@ public List<Task> allDistinctTasks(List<Task> tasks) {
287
287
288
288
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.
289
289
290
-
### Example 3 -- Find top 5 reading tasks sorted by creation date
290
+
### Example 3: Find top 5 reading tasks sorted by creation date
291
291
292
292
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.
### Example 6 -- Check if all reading tasks have tag `books`
347
+
### Example 6: Check if all reading tasks have tag `books`
348
348
349
349
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.
350
350
@@ -366,7 +366,7 @@ public boolean isAnyReadingTasksWithTagJava8(List<Task> tasks) {
366
366
}
367
367
```
368
368
369
-
### Example 7 -- Creating a summary of all titles
369
+
### Example 7: Creating a summary of all titles
370
370
371
371
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.
372
372
@@ -379,7 +379,7 @@ public String joinAllTaskTitles(List<Task> tasks) {
379
379
}
380
380
```
381
381
382
-
### Example 8 - Working with primitive Streams
382
+
### Example 8: Working with primitive Streams
383
383
384
384
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.
385
385
@@ -412,9 +412,10 @@ We can limit the resulting stream by using the `limit` operation as shown below.
412
412
infiniteStream.filter(el -> el %2==0).limit(100).forEach(System.out::println);
413
413
```
414
414
415
-
### Creating Streams from Arrays
415
+
### Example 9: Creating Streams from Arrays
416
416
417
417
You can create streams from arrays by using the static `stream` method on the `Arrays` class as shown below.
0 commit comments