Skip to content

Commit 8f586a0

Browse files
authored
Java Hashmap with different value types (eugenp#11495)
* Java Hashmap with different value types * some fixes according to review comments * convert liveTest -> unitTest * convert liveTest -> unitTest
1 parent ddcb423 commit 8f586a0

5 files changed

Lines changed: 141 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.collections.mulipletypesinmap;
2+
3+
public interface DynamicTypeValue {
4+
String valueDescription();
5+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.collections.mulipletypesinmap;
2+
3+
import java.time.Instant;
4+
import java.time.ZoneId;
5+
import java.time.format.DateTimeFormatter;
6+
7+
public class InstantTypeValue implements DynamicTypeValue {
8+
private static DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
9+
.withZone(ZoneId.systemDefault());
10+
private Instant value;
11+
12+
public InstantTypeValue(Instant value) {
13+
this.value = value;
14+
}
15+
16+
@Override
17+
public String valueDescription() {
18+
if (value == null) {
19+
return "The value is null.";
20+
}
21+
return String.format("The value is an instant: %s", FORMATTER.format(value));
22+
}
23+
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.collections.mulipletypesinmap;
2+
3+
import java.util.Arrays;
4+
5+
public class IntArrayTypeValue implements DynamicTypeValue {
6+
private int[] value;
7+
8+
public IntArrayTypeValue(int[] value) {
9+
this.value = value;
10+
}
11+
12+
@Override
13+
public String valueDescription() {
14+
if (value == null) {
15+
return "The value is null.";
16+
}
17+
return String.format("The value is an array of %d integers: %s", value.length, Arrays.toString(value));
18+
}
19+
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.collections.mulipletypesinmap;
2+
3+
public class IntegerTypeValue implements DynamicTypeValue {
4+
private Integer value;
5+
6+
public IntegerTypeValue(Integer value) {
7+
this.value = value;
8+
}
9+
10+
@Override
11+
public String valueDescription() {
12+
if(value == null){
13+
return "The value is null.";
14+
}
15+
return String.format("The value is a %s integer: %d", value > 0 ? "positive" : "negative", value);
16+
}
17+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.baeldung.multipletypesinmap;
2+
3+
import com.baeldung.collections.mulipletypesinmap.DynamicTypeValue;
4+
import com.baeldung.collections.mulipletypesinmap.InstantTypeValue;
5+
import com.baeldung.collections.mulipletypesinmap.IntArrayTypeValue;
6+
import com.baeldung.collections.mulipletypesinmap.IntegerTypeValue;
7+
import org.junit.jupiter.api.Test;
8+
9+
import java.time.Instant;
10+
import java.time.ZoneId;
11+
import java.time.format.DateTimeFormatter;
12+
import java.util.Arrays;
13+
import java.util.HashMap;
14+
import java.util.Map;
15+
16+
import static org.assertj.core.api.Assertions.assertThat;
17+
18+
class MultipleTypesInMapUnitTest {
19+
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
20+
.withZone(ZoneId.systemDefault());
21+
22+
private static final Integer intValue = 777;
23+
private static final int[] intArray = new int[]{2, 3, 5, 7, 11, 13};
24+
private static final Instant instant = Instant.now();
25+
26+
private static final String KEY_INT = "E1 (Integer)";
27+
private static final String KEY_INT_ARRAY = "E2 (IntArray)";
28+
private static final String KEY_INSTANT = "E3 (Instant)";
29+
30+
@Test
31+
void givenThreeTypes_whenUsingRawMap_thenPrintDescription() {
32+
Map<String, Object> rawMap = new HashMap<>();
33+
rawMap.put(KEY_INT, intValue);
34+
rawMap.put(KEY_INT_ARRAY, intArray);
35+
rawMap.put(KEY_INSTANT, instant);
36+
37+
rawMap.forEach((k, v) -> {
38+
if (v instanceof Integer) {
39+
Integer theV = (Integer) v;
40+
String desc = String.format("The value is a %s integer: %d", theV > 0 ? "positive" : "negative", theV);
41+
System.out.println(k + " -> " + desc);
42+
assertThat(k).isEqualTo(KEY_INT);
43+
assertThat(desc).isEqualTo("The value is a positive integer: 777");
44+
} else if (v instanceof int[]) {
45+
int[] theV = (int[]) v;
46+
String desc = String.format("The value is an array of %d integers: %s", theV.length, Arrays.toString(theV));
47+
System.out.println(k + " -> " + desc);
48+
assertThat(k).isEqualTo(KEY_INT_ARRAY);
49+
assertThat(desc).isEqualTo("The value is an array of 6 integers: [2, 3, 5, 7, 11, 13]");
50+
} else if (v instanceof Instant) {
51+
Instant theV = (Instant) v;
52+
String desc = String.format("The value is an instant: %s", FORMATTER.format(theV));
53+
System.out.println(k + " -> " + desc);
54+
assertThat(k).isEqualTo(KEY_INSTANT);
55+
assertThat(desc).matches("^The value is an instant: \\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}");
56+
} else {
57+
throw new IllegalStateException("Unknown Type found.");
58+
}
59+
});
60+
}
61+
62+
@Test
63+
void givenThreeTypes_whenUsingAnInterface_thenPrintDescription() {
64+
Map<String, DynamicTypeValue> theMap = new HashMap<>();
65+
theMap.put(KEY_INT, new IntegerTypeValue(intValue));
66+
theMap.put(KEY_INT_ARRAY, new IntArrayTypeValue(intArray));
67+
theMap.put(KEY_INSTANT, new InstantTypeValue(instant));
68+
69+
theMap.forEach((k, v) -> System.out.println(k + " -> " + v.valueDescription()));
70+
71+
assertThat(theMap.get(KEY_INT).valueDescription()).isEqualTo("The value is a positive integer: 777");
72+
assertThat(theMap.get(KEY_INT_ARRAY).valueDescription()).isEqualTo("The value is an array of 6 integers: [2, 3, 5, 7, 11, 13]");
73+
assertThat(theMap.get(KEY_INSTANT).valueDescription()).matches("^The value is an instant: \\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}");
74+
}
75+
}

0 commit comments

Comments
 (0)