I am looking for a "CLEAN & Simple" method of initialising a ConcurrentHashMap.
With Java 8 I have this:-
private static final Map<String, String> myStreamedMap = Stream.of(
new AbstractMap.SimpleImmutableEntry<>("Key1", "Value1"),
new AbstractMap.SimpleImmutableEntry<>("Key2", "Value2"),
new AbstractMap.SimpleImmutableEntry<>("Key3", "Value3"),
new AbstractMap.SimpleImmutableEntry<>("Key4", "Value4")).
collect(Collectors.toMap((entry) -> entry.getKey(), (entry) -> entry.getValue()));
Which provides the desired end result, however I feel
"new AbstractMap.SimpleImmutableEntry<>
"
Makes it hard to see whats going on.
Is there any way I can "hide" this and keep to a single line?
UPDATE
Came up with this (Obvious) solution
private static final Map<String, String> myStreamedMapWith = Stream.of(
with("Key1", "Value1"),
with("Key2", "Value2"),
with("Key3", "Value3"),
with("Key4", "Value4")).
collect(Collectors.toMap((entry) -> entry.getKey(), (entry) -> entry.getValue()));
private static AbstractMap.SimpleImmutableEntry<String, String> with(final String key, final String value) {
return new AbstractMap.SimpleImmutableEntry<>(key, value);
}