Skip to content
This repository was archived by the owner on Aug 30, 2020. It is now read-only.

Commit dbbe9a4

Browse files
committed
set map and list factory examples added
1 parent c1e0254 commit dbbe9a4

File tree

5 files changed

+305
-0
lines changed

5 files changed

+305
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@
2020

2121
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2222
hs_err_pid*
23+
/target/

pom.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.github.hemantsonu20</groupId>
5+
<artifactId>java-9-features</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
8+
<properties>
9+
<maven.compiler.source>1.9</maven.compiler.source>
10+
<maven.compiler.target>1.9</maven.compiler.target>
11+
</properties>
12+
13+
<build>
14+
<plugins>
15+
<plugin>
16+
<groupId>org.apache.maven.plugins</groupId>
17+
<artifactId>maven-compiler-plugin</artifactId>
18+
<version>3.7.0</version>
19+
<configuration>
20+
<source>1.9</source>
21+
<target>1.9</target>
22+
</configuration>
23+
</plugin>
24+
</plugins>
25+
</build>
26+
27+
</project>
28+
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.github.hemantsonu20.java9.factory;
2+
3+
import java.util.List;
4+
5+
public class ListFactory {
6+
7+
public static void main(String[] args) {
8+
9+
of0();
10+
of1();
11+
of2();
12+
of10();
13+
ofArray();
14+
immutableCheck();
15+
noNullValue();
16+
duplicatesAllowedAsUsual();
17+
}
18+
19+
private static void of0() {
20+
21+
List<String> list = List.of();
22+
print(list);
23+
}
24+
25+
private static void of1() {
26+
27+
List<String> list = List.of("One");
28+
print(list);
29+
}
30+
31+
private static void of2() {
32+
33+
List<String> list = List.of("One", "Two");
34+
print(list);
35+
}
36+
37+
private static void of10() {
38+
39+
List<String> list = List.of("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten");
40+
print(list);
41+
}
42+
43+
private static void ofArray() {
44+
45+
String[] arr = { "One", "Two" };
46+
List<String> list = List.of(arr);
47+
print(list);
48+
}
49+
50+
private static void immutableCheck() {
51+
52+
List<String> list = List.of("One", "Two");
53+
54+
try {
55+
list.clear();
56+
} catch (UnsupportedOperationException e) {
57+
print("of factory methods create Immutable collections");
58+
}
59+
}
60+
61+
private static void noNullValue() {
62+
63+
try {
64+
List.of("One", null, "Two", null);
65+
} catch (NullPointerException e) {
66+
print("no null values are allowed in of factory methods");
67+
}
68+
}
69+
70+
private static void duplicatesAllowedAsUsual() {
71+
72+
List<String> list = List.of("One", "Two", "One");
73+
print(list);
74+
}
75+
76+
private static void print(Object obj) {
77+
78+
System.out.println(obj);
79+
}
80+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.github.hemantsonu20.java9.factory;
2+
3+
import java.util.Map;
4+
import java.util.Map.Entry;
5+
6+
/**
7+
* There are sets of factory methods for immutable maps in {@link Map} class. It
8+
* has of methods for 0 to 10 size map. Also it has
9+
* {@link Map#ofEntries(Entry...)} method to create immutable maps from
10+
* {@link Entry} objects.
11+
*
12+
* @see Map#of()
13+
* @see Map#of(Object, Object)
14+
* @see Map#entry(Object, Object)
15+
* @see Map#ofEntries(Entry...)
16+
*
17+
* @author hemantsonu20
18+
*
19+
*/
20+
public class MapFactory {
21+
22+
public static void main(String[] args) {
23+
24+
of0();
25+
of1();
26+
of2();
27+
of10();
28+
immutableCheck();
29+
noNullKeyOrValue();
30+
noDuplicateKey();
31+
ofEntries();
32+
}
33+
34+
private static void of0() {
35+
36+
Map<String, Integer> map = Map.of();
37+
print(map);
38+
}
39+
40+
private static void of1() {
41+
42+
Map<String, Integer> map = Map.of("One", 1);
43+
print(map);
44+
}
45+
46+
private static void of2() {
47+
48+
Map<String, Integer> map = Map.of("One", 1, "Two", 2);
49+
print(map);
50+
}
51+
52+
private static void of10() {
53+
54+
Map<String, Integer> map = Map.of("One", 1, "Two", 2, "Three", 3, "Four", 4, "Five", 5, "Six", 6, "Seven", 7,
55+
"Eight", 8, "Nine", 9, "Ten", 10);
56+
print(map);
57+
}
58+
59+
private static void immutableCheck() {
60+
61+
Map<String, Integer> map = Map.of("One", 1);
62+
63+
try {
64+
map.put("Two", 2);
65+
} catch (UnsupportedOperationException e) {
66+
print("of factory methods create Immutable collections");
67+
}
68+
}
69+
70+
/**
71+
* {@link Map#of()} methods throw null pointer if any key or value is null
72+
*
73+
*/
74+
private static void noNullKeyOrValue() {
75+
76+
try {
77+
Map.of("One", 1, null, 2);
78+
79+
} catch (NullPointerException e) {
80+
print("no null keys are allowed in of factory methods");
81+
}
82+
try {
83+
Map.of("One", null);
84+
85+
} catch (NullPointerException e) {
86+
print("no null values are allowed in of factory methods");
87+
}
88+
}
89+
90+
private static void noDuplicateKey() {
91+
92+
try {
93+
Map.of("One", 1, "One", 1);
94+
95+
} catch (IllegalArgumentException e) {
96+
print("no duplicate keys are allowed in of factory methods");
97+
}
98+
}
99+
100+
private static void ofEntries() {
101+
102+
Map.Entry<String, Integer> entry1 = Map.entry("One", 1);
103+
Map.Entry<String, Integer> entry2 = Map.entry("Two", 2);
104+
105+
Map<String, Integer> map = Map.ofEntries(entry1, entry2);
106+
print(map);
107+
}
108+
109+
private static void print(Object obj) {
110+
111+
System.out.println(obj);
112+
}
113+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.github.hemantsonu20.java9.factory;
2+
3+
import java.util.Set;
4+
5+
public class SetFactory {
6+
7+
public static void main(String[] args) {
8+
9+
of0();
10+
of1();
11+
of2();
12+
of10();
13+
ofArray();
14+
immutableCheck();
15+
noNullValue();
16+
noDuplicates();
17+
}
18+
19+
private static void of0() {
20+
21+
Set<String> set = Set.of();
22+
print(set);
23+
}
24+
25+
private static void of1() {
26+
27+
Set<String> set = Set.of("One");
28+
print(set);
29+
}
30+
31+
private static void of2() {
32+
33+
Set<String> set = Set.of("One", "Two");
34+
print(set);
35+
}
36+
37+
private static void of10() {
38+
39+
Set<String> set = Set.of("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten");
40+
print(set);
41+
}
42+
43+
private static void ofArray() {
44+
45+
String[] arr = { "One", "Two" };
46+
Set<String> set = Set.of(arr);
47+
print(set);
48+
}
49+
50+
private static void immutableCheck() {
51+
52+
Set<String> set = Set.of("One", "Two");
53+
54+
try {
55+
set.clear();
56+
} catch (UnsupportedOperationException e) {
57+
print("of factory methods create Immutable collections");
58+
}
59+
}
60+
61+
private static void noNullValue() {
62+
63+
try {
64+
Set.of("One", null, "Two", null);
65+
} catch (NullPointerException e) {
66+
print("no null values are allowed in of factory methods");
67+
}
68+
}
69+
70+
private static void noDuplicates() {
71+
72+
try {
73+
Set.of("One", "Two", "One");
74+
} catch (IllegalArgumentException e) {
75+
print("no duplicates are allowed in of factory methods");
76+
}
77+
}
78+
79+
private static void print(Object obj) {
80+
81+
System.out.println(obj);
82+
}
83+
}

0 commit comments

Comments
 (0)