I would like to create a
public static final LinkedMap myMap;
Somewhere I found something similar for Maps:
public class Test {
private static final Map<Integer, String> MY_MAP = createMap();
private static Map<Integer, String> createMap() {
Map<Integer, String> result = new HashMap<Integer, String>();
result.put(1, "one");
result.put(2, "two");
return Collections.unmodifiableMap(result);
}
}
But I cannot apply the 'unmodifiableMap' method it to a LinkedMap
. Can anybody help me? Is it possible at all?
The most popular workaround is almost certainly a Guava
ImmutableMap
. (Disclosure: I contribute to Guava.)or
Without other libraries, the only workaround other than the one you've written is