com.google.common.collect
Class ImmutableMapBuilder<K,V>

java.lang.Object
  extended by com.google.common.collect.ImmutableMapBuilder<K,V>

public class ImmutableMapBuilder<K,V>
extends java.lang.Object

A convenient way to populate immutable Map instances, especially static-final "constant Maps". Code such as

   static final Map<String,Integer> ENGLISH_TO_INTEGER_MAP
       = createNumbersMap();

   static Map<String,Integer> createNumbersMap() {
     Map<String,Integer> map = Maps.newHashMap();
     map.put("one", 1);
     map.put("two", 2);
     map.put("three", 3);
     return Collections.unmodifiableMap(map);
   }
 
... can be rewritten far more simply as ...
   static final Map<String,Integer> ENGLISH_TO_INTEGER_MAP
     = new ImmutableMapBuilder<String,Integer>()
       .put("one", 1)
       .put("two", 2)
       .put("three", 3)
       .getMap();
 
(Actually, for small immutable Maps, you can use members of the even-more-convenient Maps.immutableMap() family of methods.)

Author:
Kevin Bourrillion

Constructor Summary
ImmutableMapBuilder()
          Creates a new ImmutableMapBuilder with an unspecified expected size.
ImmutableMapBuilder(int expectedSize)
          Creates a new ImmutableMapBuilder with the given expected size.
 
Method Summary
static
<K,V> ImmutableMapBuilder<K,V>
fromMap(java.util.Map<K,V> map)
          Creates a new ImmutableMapBuilder populated with the contents of map.
 java.util.Map<K,V> getMap()
          Returns a newly-created, immutable HashMap instance containing the keys and values that were specified using put.
 ImmutableMapBuilder<K,V> put(K key, V value)
          Adds a key-value mapping to the map that will be returned by getMap.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ImmutableMapBuilder

public ImmutableMapBuilder()
Creates a new ImmutableMapBuilder with an unspecified expected size.


ImmutableMapBuilder

public ImmutableMapBuilder(int expectedSize)
Creates a new ImmutableMapBuilder with the given expected size.

Parameters:
expectedSize - the approximate number of key-value pairs you expect this map to contain
Method Detail

fromMap

public static <K,V> ImmutableMapBuilder<K,V> fromMap(java.util.Map<K,V> map)
Creates a new ImmutableMapBuilder populated with the contents of map.


put

public ImmutableMapBuilder<K,V> put(@Nullable
                                    K key,
                                    @Nullable
                                    V value)
Adds a key-value mapping to the map that will be returned by getMap.

Parameters:
key - key with which the specified value is to be associated
value - value to be associated with the specified key
Returns:
this map builder (to enable call chaining)
Throws:
java.lang.IllegalStateException - if getMap has already been called

getMap

public java.util.Map<K,V> getMap()
Returns a newly-created, immutable HashMap instance containing the keys and values that were specified using put.

Returns:
a new, immutable HashMap instance
Throws:
java.lang.IllegalStateException - if getMap has already been called


Copyright © 2008 Google. All Rights Reserved.