|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.google.common.collect.ImmutableMapBuilder<K,V>
public class ImmutableMapBuilder<K,V>
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.)
| 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
|
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 |
|---|
public ImmutableMapBuilder()
public ImmutableMapBuilder(int expectedSize)
expectedSize - the approximate number of key-value pairs you expect
this map to contain| Method Detail |
|---|
public static <K,V> ImmutableMapBuilder<K,V> fromMap(java.util.Map<K,V> map)
map.
public ImmutableMapBuilder<K,V> put(@Nullable
K key,
@Nullable
V value)
getMap.
key - key with which the specified value is to be associatedvalue - value to be associated with the specified key
java.lang.IllegalStateException - if getMap has already been calledpublic java.util.Map<K,V> getMap()
put.
java.lang.IllegalStateException - if getMap has already been called
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||