Here’s a little class I’ve found useful lately:
public class MapBuilder<K , V> {
public static <K , V> MapBuilder <K , V> mapping(K key, V value) {
return new MapBuilder<K , V>(key, value);
}
private final Map<K , V> map = new HashMap<K , V>();
private MapBuilder(K key, V value) {
map.put(key, value);
}
public MapBuilder<K , V> and(K key, V value) {
map.put(key, value);
return this;
}
public Map<K , V> build() {
return map;
}
}
You use it like this:
private static final Map<String , String> knownUris =
mapping("http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", "xhtml1-transitional.dtd")
.and("http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent", "xhtml-lat1.ent")
.and("http://www.w3.org/TR/xhtml1/DTD/xhtml-symbol.ent", "xhtml-symbol.ent")
.and("http://www.w3.org/TR/xhtml1/DTD/xhtml-special.ent", "xhtml-symbol.ent")
.build();
Obviously you have to import static the mapping method for it to work.
It’s a bit annoying that even much worse languages (like PHP) have a nice built-in syntax for declaring associative arrays. But there you go.