If I want an empty map or a one element map, Java Collections has a method for that. Why is there no method for more than one element? What's the best way to create a static final map with 2 elements in it? I know I can do something like:
private static final Map<String, String> MAP = new HashMap<String, String>() {
开发者_开发百科 { put("a", "b"); put("c", "d"); }
};
But then Eclipse complains about the serialVersionUID...
The reason Collections
has methods for 0 and 1 entry maps is because they are special cases... the empty Map
is an immutable singleton for example.
For what you want to do, though, I'd strongly recommend using Guava. Its Immutable*
collections (ImmutableMap
specifically) are what you want I think:
private static final ImmutableMap<String, String> MAP = ImmutableMap.of(
"a", "b",
"c", "d");
You can do the above for small maps, and for bigger maps you can write:
private static final ImmutableMap<String, String> MAP =
ImmutableMap.<String, String>builder()
.put("a", "b")
.put("b", "c")
...
.put("y", "z")
.build();
If you don't use Guava, you'll still likely want to ensure that this map can't be changed. This is a lot uglier:
private static final Map<String, String> MAP;
static {
Map<String, String> temp = new HashMap<String, String>();
temp.put("a", "b");
temp.put("b", "c");
MAP = Collections.unmodifiableMap(temp);
}
Mutable objects like maps chan be changed even if they are static and final. Final just means that the reference to the object can't change. You don't need to add the objects on initialization. The map can be added to and otherwise modified in other areas of the code, but you cannot assign a different object to the variable MAP
later.
If you want it to be initialized with two items, you can add them in a static block after the declaration.
static {
MAP.put("a", "b");
MAP.put("c", "d");
}
This article has some good information on how static blocks work.
If you want an immutable map with two items, you can use a static block again like so:
private static final Map<String, String> MAP;
static {
Map<String, String> tempMap = new HashMap<String, String>();
tempMap.put("a", "b");
tempMap.put("c", "d");
MAP = Collections.unmodifiableMap(tempMap);
}
Try this (modified version of what Mark Storer wrote):
private static final Map<String, String> Map = buildMap();
private static Map<String, String> buildMap() {
Map<String, String> map = new HashMap<String, String>();
map.put("a", "b");
map.put("c", "d");
return Collections.unmodifiableMap(map);
}
Use the return value of a static function:
private static final Map<String, String> Map = buildMap();
private static Map<String, String> buildMap() {
Map<String, String> map = new HashMap<String, String>();
map.put("a", "b");
map.put("c", "d");
return map;
}
EDIT: Nope. My C/C++ "const" thinking is showing. A final variable may only be assigned once, but the object assigned to that variable is still quite mutable.
If they provide 0..N for any N you would be asking why isn't N+1 supported. N=0 and N=1 are easily handled special cases.
精彩评论