Skip to content

Commit 320cd90

Browse files
committed
added Google Analytics Tracking
1 parent d5a3f0d commit 320cd90

File tree

16 files changed

+173
-14
lines changed

16 files changed

+173
-14
lines changed

01-default-static-interface-methods.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,5 @@ class App implements B, A {
231231
}
232232
```
233233
This will print `inside B`.
234+
235+
[![Analytics](https://ga-beacon.appspot.com/UA-59411913-3/shekhargulati/java8-the-missing-tutorial)](https://github.com/igrigorik/ga-beacon)

02-lambdas.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,3 +379,5 @@ interface TaskExtractor<R> extends Function<Task, R> {
379379
}
380380
}
381381
```
382+
383+
[![Analytics](https://ga-beacon.appspot.com/UA-59411913-3/shekhargulati/java8-the-missing-tutorial)](https://github.com/igrigorik/ga-beacon)

03-streams.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,3 +470,6 @@ Arrays.stream(urls).parallel().map(url -> getUrlContent(url)).forEach(System.out
470470
```
471471

472472
If you need to understand when to use Parallel Stream I would recommend you read this article by Doug Lea and few other Java folks [http://gee.cs.oswego.edu/dl/html/StreamParallelGuidance.html](http://gee.cs.oswego.edu/dl/html/StreamParallelGuidance.html) to gain better understanding.
473+
474+
475+
[![Analytics](https://ga-beacon.appspot.com/UA-59411913-3/shekhargulati/java8-the-missing-tutorial)](https://github.com/igrigorik/ga-beacon)

04-collectors.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,5 @@ public static void wordCount(Path path) throws IOException {
344344
wordCount.forEach((k, v) -> System.out.println(String.format("%s ==>> %d", k, v)));
345345
}
346346
```
347+
348+
[![Analytics](https://ga-beacon.appspot.com/UA-59411913-3/shekhargulati/java8-the-missing-tutorial)](https://github.com/igrigorik/ga-beacon)

05-optionals.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,5 @@ public boolean isTaskDueToday(Optional<Task> task) {
279279
return task.flatMap(Task::getDueOn).filter(d -> d.isEqual(LocalDate.now())).isPresent();
280280
}
281281
```
282+
283+
[![Analytics](https://ga-beacon.appspot.com/UA-59411913-3/shekhargulati/java8-the-missing-tutorial)](https://github.com/igrigorik/ga-beacon)

06-map.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,92 @@
11
Map Improvements
22
---------
3+
4+
Map is one the most important data structure. In Java 8, a lot of goodies has been added to the Map API that will make it easy to work with them. We will look at all the enhancements made to them one by one. Every feature is shown along with its JUnit test case.
5+
6+
## Create Map from List
7+
8+
Most of the times we want to create a map from existing data. Let's suppose we have a list of tasks, every task has an id and other associated data like title, description, etc.
9+
10+
```java
11+
import static java.util.function.Function.identity;
12+
import static java.util.stream.Collectors.toMap;
13+
14+
@Test
15+
public void shouldCreateMapFromTaskList() throws Exception {
16+
Task t1 = new Task("Write blog on Java 8 Map improvements", TaskType.BLOGGING);
17+
Task t2 = new Task("Write factorial program in Java 8", TaskType.CODING);
18+
List<Task> tasks = Arrays.asList(t1, t2);
19+
20+
Map<String, Task> taskIdToTaskMap = tasks.stream().collect(toMap(Task::getId, identity()));
21+
22+
assertThat(taskIdToTaskMap, hasEntry(notNullValue(), equalTo(t1)));
23+
assertThat(taskIdToTaskMap, hasEntry(notNullValue(), equalTo(t2)));
24+
}
25+
```
26+
27+
## Using a different Map implementation
28+
29+
30+
The default implementation used by `Collectors.toMap` is `HashMap`. You can also specify your own Map implementation by providing a supplier.
31+
32+
```java
33+
@Test
34+
public void shouldCreateLinkedMapFromTaskList() throws Exception {
35+
Task t1 = new Task("Write blog on Java 8 Map improvements", TaskType.BLOGGING);
36+
Task t2 = new Task("Write factorial program in Java 8", TaskType.CODING);
37+
List<Task> tasks = Arrays.asList(t1, t2);
38+
39+
Map<String, Task> taskIdToTaskMap = tasks.stream().collect(toMap(Task::getId, identity(), (k1, k2) -> k1, LinkedHashMap::new));
40+
41+
assertThat(taskIdToTaskMap, instanceOf(LinkedHashMap.class));
42+
assertThat(taskIdToTaskMap, hasEntry(notNullValue(), equalTo(t1)));
43+
assertThat(taskIdToTaskMap, hasEntry(notNullValue(), equalTo(t2)));
44+
}
45+
```
46+
47+
## Handling duplicates
48+
49+
One thing that we glossed over in the last example was what should happen if there are duplicates. To handle duplicates there is a argument
50+
51+
```java
52+
@Test
53+
public void shouldHandleTaskListWithDuplicates() throws Exception {
54+
Task t1 = new Task("1", "Write blog on Java 8 Map improvements", TaskType.BLOGGING);
55+
Task t2 = new Task("1", "Write factorial program in Java 8", TaskType.CODING);
56+
List<Task> tasks = Arrays.asList(t1, t2);
57+
58+
Map<String, Task> taskIdToTaskMap = tasks.stream().collect(toMap(Task::getId, identity()));
59+
60+
assertThat(taskIdToTaskMap, hasEntry(notNullValue(), equalTo(t1)));
61+
assertThat(taskIdToTaskMap, hasEntry(notNullValue(), equalTo(t2)));
62+
}
63+
```
64+
65+
This test will fail
66+
67+
```
68+
java.lang.IllegalStateException: Duplicate key Task{title='Write blog on Java 8 Map improvements', type=BLOGGING}
69+
```
70+
71+
You can handle the error by specifying your merge function.
72+
73+
```java
74+
@Test
75+
public void shouldHandleTaskListWithDuplicates() throws Exception {
76+
Task t1 = new Task("1", "Write blog on Java 8 Map improvements", TaskType.BLOGGING);
77+
Task t2 = new Task("1", "Write factorial program in Java 8", TaskType.CODING);
78+
List<Task> tasks = Arrays.asList(t1, t2);
79+
Map<String, Task> taskIdToTaskMap = tasks.stream().collect(toMap(Task::getId, identity(), (k1, k2) -> k2));
80+
assertThat(taskIdToTaskMap, hasEntry(notNullValue(), equalTo(t2)));
81+
}
82+
```
83+
84+
## Create Map from tuples
85+
86+
```java
87+
public static <T, U> Map<T, U> createMap(SimpleEntry<T, U>... entries) {
88+
return Stream.of(entries).collect(toMap(SimpleEntry::getKey, SimpleEntry::getValue));
89+
}
90+
```
91+
92+
[![Analytics](https://ga-beacon.appspot.com/UA-59411913-3/shekhargulati/java8-the-missing-tutorial)](https://github.com/igrigorik/ga-beacon)

07-building-functional-programs.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
Building Functional Programs
22
--------
3+
4+
[![Analytics](https://ga-beacon.appspot.com/UA-59411913-3/shekhargulati/java8-the-missing-tutorial)](https://github.com/igrigorik/ga-beacon)

08-date-time-api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,3 +380,5 @@ TemporalAdjuster nextWorkingDayAdjuster = TemporalAdjusters.ofDateAdjuster(local
380380
});
381381
System.out.println(today.with(nextWorkingDayAdjuster));
382382
```
383+
384+
[![Analytics](https://ga-beacon.appspot.com/UA-59411913-3/shekhargulati/java8-the-missing-tutorial)](https://github.com/igrigorik/ga-beacon)

09-completable-futures.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
Completable Futures
22
------
3+
4+
5+
6+
[![Analytics](https://ga-beacon.appspot.com/UA-59411913-3/shekhargulati/java8-the-missing-tutorial)](https://github.com/igrigorik/ga-beacon)

10-nashorn.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,6 @@ Caused by: java.lang.ClassNotFoundException: java.util.HashMap
337337
at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:228)
338338
at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393)
339339
```
340+
341+
342+
[![Analytics](https://ga-beacon.appspot.com/UA-59411913-3/shekhargulati/java8-the-missing-tutorial)](https://github.com/igrigorik/ga-beacon)

0 commit comments

Comments
 (0)