In short, if you want to write a map of e.g. constants in Java, which in e.g. Python and Javascript you would write as a literal,
T CON
You can write yourself a quick helper function:
@SuppressWarnings("unchecked")
public static Map ImmutableMap(Object... keyValPair){
Map map = new HashMap();
if(keyValPair.length % 2 != 0){
throw new IllegalArgumentException("Keys and values must be pairs.");
}
for(int i = 0; i < keyValPair.length; i += 2){
map.put((K) keyValPair[i], (V) keyValPair[i+1]);
}
return Collections.unmodifiableMap(map);
}
Note the code above isn't going to stop you from overwriting constants of the same name, using CONST_1
multiple places in your list will result in the final CONST_1
's value appearing.
Usage is something like:
Map constants = ImmutableMap(
"CONST_0", 1.0,
"CONST_1", 2.0
);