Building a map from key-values pairs in Java
18,607
Solution 1
What's wrong with
Map<String, Integer> map = new HashMap<>();
map.put("key1", 10);
map.put("key2", 20);
map.put("key3", 30);
That looks very readable to me, and I don't see what you gain from your MapBuilder. Anyway, such a MapBuilder wouldn't be hard to implement.
Solution 2
Why not just roll your own?
public class MapBuilder<K,V> {
private Map<K,V> map;
public static <K,V> MapBuilder<K,V> newHashMap(){
return new MapBuilder<K,V>(new HashMap<K,V>());
}
public MapBuilder(Map<K,V> map) {
this.map = map;
}
public MapBuilder<K,V> with(K key, V value){
map.put(key, value);
return this;
}
public Map<K,V> build(){
return map;
}
}
Solution 3
How about creating your own AbstractMap
with a put method that returns this
?
public class MyMap<K, V> extends AbstractMap<K, V>{
@Override
public Set<java.util.Map.Entry<K, V>> entrySet() {
// return set
return null;
}
public MyMap<K, V> puts(K key, V value) {
this.put(key, value);
return this;
};
}
Then use that method to chain pairs:
new MyMap<String, String>()
.puts("foo", "bar")
.puts("Hello", "World");
Author by
Roman
Updated on June 28, 2022Comments
-
Roman almost 2 years
Possible Duplicate:
builder for HashMapAre there any utility class which allows to create a Map from a number of key-value pairs in a convenient and readable manner?
I thought that
guava
should have contain something but I couldn't find anything with necessary functionality.What I want is something like this:
MapBuilder.newHashMap() .with("key1", 10) .with("key2", 20) .with("key3", 30) .build();
P.S. I also know about double-brace approach (
new HashMap<>() {{ put(..); put(..); }}
) but I don't find it either readable or convenient. -
davogotland over 12 yearsagreed, very readable. agreed, easy to implement. (i hope i don't have bugs in my suggested implementation haha)
-
JB Nizet over 12 yearsNo need for the Class arguments. See Robert's answer for a better implementation.
-
davogotland over 12 yearshow will
K
andV
be known to static methodnewHashMap()
? -
davogotland over 12 yearsis it the
<K, V>
just afterstatic
that means that it will be fetched from the left side of an assignment operator? -
Robert over 12 yearsit is a generic method, as well as a generic class
-
davogotland over 12 yearsi saw it, and i asked about it. as i understand, that won't make it possible to create it without assigning it? not something one wants to do really often, but still :)
-
davogotland over 12 yearsi know it's a generic method. am i correct about where the types are derived from?
-
Robert over 12 yearsyes, the types are bound when the method is called, and these types are then passed to the class. E.g, MapBuilder.<String, String>newHashMap().with("Hello", "World")
-
JB Nizet over 12 yearsThe types are given when calling the method: MapBuilder.<String, Integer>newHashMap(). If you declare a MapBuilder variable, it can be inferred from the type of the builder: MapBuilder<String, Integer> b = MapBuilder.newHashMap();