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");
Share:
18,607
Roman
Author by

Roman

Updated on June 28, 2022

Comments

  • Roman
    Roman almost 2 years

    Possible Duplicate:
    builder for HashMap

    Are 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
    davogotland over 12 years
    agreed, very readable. agreed, easy to implement. (i hope i don't have bugs in my suggested implementation haha)
  • JB Nizet
    JB Nizet over 12 years
    No need for the Class arguments. See Robert's answer for a better implementation.
  • davogotland
    davogotland over 12 years
    how will K and V be known to static method newHashMap()?
  • davogotland
    davogotland over 12 years
    is it the <K, V> just after static that means that it will be fetched from the left side of an assignment operator?
  • Robert
    Robert over 12 years
    it is a generic method, as well as a generic class
  • davogotland
    davogotland over 12 years
    i 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
    davogotland over 12 years
    i know it's a generic method. am i correct about where the types are derived from?
  • Robert
    Robert over 12 years
    yes, 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
    JB Nizet over 12 years
    The 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();