java.lang.Object
de.cuioss.tools.collect.MapBuilder<K,V>
- Type Parameters:
K- the type of keys maintained by this mapV- the type of mapped values
Overview
Builder for creating Maps providing some convenience
methods. The class writes everything through into the contained collector.
Using the default constructor a newly created HashMap will
be used as collector, but you can pass you own collector as
constructor-argument. Of course this should be mutable in order to work.
Although not being a Map itself it provides the same
methods with different semantics -> Builder approach.
Standard Usage
MapBuilder<String, String> builder = new MapBuilder<>();
builder.put("key1", "value1").put("key2", "value2");
assertEquals(2, builder.size());
assertMutable(builder.toMutableMap());
assertImmutable(builder.toImmutableMap());
Using from()
This methods can be used for ensuring a real copy
assertEquals(4, MapBuilder.from("key1", 1, "key2", 2, "key3", 3, "key4", 4).size());
- Author:
- Oliver Wolff
-
Constructor Summary
ConstructorsConstructorDescriptionDefault Constructor initializing the collector with anHashMapMapBuilder(Map<K, V> collector) Constructor for MapBuilder. -
Method Summary
Modifier and TypeMethodDescriptionMapBuilder<K,V> clear()Clears the contained collectorbooleancontainsKey(Object key) Returnstrueif this map contains a mapping for the specified key.booleancontainsValue(Object value) Returnstrueif this map maps one or more keys to the specified value.static <K,V> MapBuilder<K, V> copyFrom.static <K,V> MapBuilder<K, V> from(K k1, V v1) Shorthand for creating aMapBuilderfrom a given key/value pairstatic <K,V> MapBuilder<K, V> from(K k1, V v1, K k2, V v2) Shorthand for creating aMapBuilderfrom given key/value pairsstatic <K,V> MapBuilder<K, V> from(K k1, V v1, K k2, V v2, K k3, V v3) Shorthand for creating aMapBuilderfrom given key/value pairsstatic <K,V> MapBuilder<K, V> from(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) Shorthand for creating aMapBuilderfrom given key/value pairsbooleanisEmpty()isEmpty.MapBuilder<K,V> put.MapBuilder<K,V> put.MapBuilder<K,V> putAll.MapBuilder<K,V> putIfNotNull(K key, V value) Puts the entry into the map, if the value is notnull.MapBuilder<K,V> remove.intsize()size.toImmutableMap.toMutableMap.
-
Constructor Details
-
MapBuilder
public MapBuilder()Default Constructor initializing the collector with anHashMap -
MapBuilder
Constructor for MapBuilder.
- Parameters:
collector- to be used for storage. Must not be null
-
-
Method Details
-
size
size.
- Returns:
- the number of key-value mappings in this map
- See Also:
-
isEmpty
isEmpty.
- Returns:
- true if this map contains no key-value mappings
- See Also:
-
containsKey
Returnstrueif this map contains a mapping for the specified key. More formally, returnstrueif and only if this map contains a mapping for a keyksuch that(key==null ? k==null : key.equals(k)). (There can be at most one such mapping.)- Parameters:
key- key whose presence in this map is to be tested- Returns:
trueif this map contains a mapping for the specified key- See Also:
-
containsValue
Returnstrueif this map maps one or more keys to the specified value. More formally, returnstrueif and only if this map contains at least one mapping to a valuevsuch that(value==null ? v==null : value.equals(v)). This operation will probably require time linear in the map size for most implementations of theMapinterface.- Parameters:
value- value whose presence in this map is to be tested- Returns:
trueif this map maps one or more keys to the specified value- See Also:
-
put
put.
- Parameters:
key- to be put as key, must not be emptyvalue- to be put as value, must not be empty- Returns:
- the instance itself in order to use it in a fluent way.
-
putIfNotNull
Puts the entry into the map, if the value is notnull.- Parameters:
key- to be put as key, must not be emptyvalue- to be put as value- Returns:
- the instance itself in order to use it in a fluent way.
-
put
put.
- Parameters:
entry- to be put, must notnull- Returns:
- the instance itself in order to use it in a fluent way.
-
remove
remove.
- Parameters:
key- to be removed- Returns:
- the instance itself in order to use it in a fluent way.
-
putAll
putAll.
- Parameters:
map- to be put- Returns:
- the instance itself in order to use it in a fluent way.
-
clear
Clears the contained collector- Returns:
- the instance itself in order to use it in a fluent way.
-
toMutableMap
toMutableMap.
-
toImmutableMap
toImmutableMap.
- Returns:
- an immutable
Maprepresentation of the builders content, the actual implementation does not create a copy but provides an unmodifiable view usingCollections.unmodifiableMap(Map)
-
copyFrom
copyFrom.
- Type Parameters:
K- the type of keys maintained by this mapV- the type of mapped values- Parameters:
original- map used to initialize the contained collector- Returns:
- an instance of
MapBuilderinitialized with a copy of the given Map.
-
from
Shorthand for creating aMapBuilderfrom a given key/value pair- Type Parameters:
K- the type of keys maintained by this mapV- the type of mapped values- Parameters:
k1- key to be addedv1- value to be added- Returns:
- an instance of
MapBuilderinitialized with the given key/value pair.
-
from
Shorthand for creating aMapBuilderfrom given key/value pairs- Type Parameters:
K- the type of keys maintained by this mapV- the type of mapped values- Parameters:
k1- key to be addedv1- value to be addedk2- key to be addedv2- value to be added- Returns:
- an instance of
MapBuilderinitialized with the given key/value pairs.
-
from
Shorthand for creating aMapBuilderfrom given key/value pairs- Type Parameters:
K- the type of keys maintained by this mapV- the type of mapped values- Parameters:
k1- key to be addedv1- value to be addedk2- key to be addedv2- value to be addedk3- key to be addedv3- value to be added- Returns:
- an instance of
MapBuilderinitialized with the given key/value pairs.
-
from
Shorthand for creating aMapBuilderfrom given key/value pairs- Type Parameters:
K- the type of keys maintained by this mapV- the type of mapped values- Parameters:
k1- key to be addedv1- value to be addedk2- key to be addedv2- value to be addedk3- key to be addedv3- value to be addedk4- key to be addedv4- value to be added- Returns:
- an instance of
MapBuilderinitialized with the given key/value pairs.
-