Skip to content

Commit 774affe

Browse files
committed
updated ch01
1 parent 7f94810 commit 774affe

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

code/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ repositories {
1010
}
1111

1212
dependencies {
13+
testCompile "org.hamcrest:hamcrest-all:1.3"
1314
testCompile group: 'junit', name: 'junit', version: '4.12'
1415
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
package com.shekhargulati.java8_tutorial.ch06;
22

3-
/**
4-
* Created by shekhargulati on 20/12/15.
5-
*/
3+
import java.util.AbstractMap.SimpleEntry;
4+
import java.util.Map;
5+
import java.util.stream.Stream;
6+
7+
import static java.util.stream.Collectors.toMap;
8+
69
public class MapExample {
10+
11+
public static <T, U> Map<T, U> createMap(SimpleEntry<T, U>... entries) {
12+
return Stream.of(entries).collect(toMap(SimpleEntry::getKey, SimpleEntry::getValue));
13+
}
14+
15+
public static <T, U> void doSth(Map<T, U> map) {
16+
17+
18+
}
719
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
package com.shekhargulati.java8_tutorial.ch06;
22

3-
import static org.junit.Assert.*;
3+
import org.junit.Test;
4+
5+
import java.util.AbstractMap.SimpleEntry;
6+
import java.util.Map;
7+
8+
import static org.hamcrest.CoreMatchers.equalTo;
9+
import static org.junit.Assert.assertThat;
410

5-
/**
6-
* Created by shekhargulati on 20/12/15.
7-
*/
811
public class MapExampleTest {
912

13+
@Test
14+
public void shouldCreateAHashMapUsingSimpleEntries() throws Exception {
15+
Map<String, Integer> nameAndAge = MapExample.createMap(new SimpleEntry<>("shekhar", 32), new SimpleEntry<>("rahul", 33), new SimpleEntry<>("sameer", 33));
16+
assertThat(nameAndAge.size(), equalTo(3));
17+
18+
}
1019
}

0 commit comments

Comments
 (0)