Class MapBuilder<K,V>

java.lang.Object
de.cuioss.tools.collect.MapBuilder<K,V>
Type Parameters:
K - the type of keys maintained by this map
V - the type of mapped values

public final class MapBuilder<K,V> extends Object

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 Details

    • MapBuilder

      public MapBuilder()
      Default Constructor initializing the collector with an HashMap
    • MapBuilder

      public MapBuilder(Map<K,V> collector)

      Constructor for MapBuilder.

      Parameters:
      collector - to be used for storage. Must not be null
  • Method Details

    • size

      public int size()

      size.

      Returns:
      the number of key-value mappings in this map
      See Also:
    • isEmpty

      public boolean isEmpty()

      isEmpty.

      Returns:
      true if this map contains no key-value mappings
      See Also:
    • containsKey

      public boolean containsKey(Object key)
      Returns true if this map contains a mapping for the specified key. More formally, returns true if and only if this map contains a mapping for a key k such 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:
      true if this map contains a mapping for the specified key
      See Also:
    • containsValue

      public boolean containsValue(Object value)
      Returns true if this map maps one or more keys to the specified value. More formally, returns true if and only if this map contains at least one mapping to a value v such that (value==null ? v==null : value.equals(v)). This operation will probably require time linear in the map size for most implementations of the Map interface.
      Parameters:
      value - value whose presence in this map is to be tested
      Returns:
      true if this map maps one or more keys to the specified value
      See Also:
    • put

      public MapBuilder<K,V> put(K key, V value)

      put.

      Parameters:
      key - to be put as key, must not be empty
      value - to be put as value, must not be empty
      Returns:
      the instance itself in order to use it in a fluent way.
    • putIfNotNull

      public MapBuilder<K,V> putIfNotNull(K key, V value)
      Puts the entry into the map, if the value is not null.
      Parameters:
      key - to be put as key, must not be empty
      value - to be put as value
      Returns:
      the instance itself in order to use it in a fluent way.
    • put

      public MapBuilder<K,V> put(Map.Entry<? extends K,? extends V> entry)

      put.

      Parameters:
      entry - to be put, must not null
      Returns:
      the instance itself in order to use it in a fluent way.
    • remove

      public MapBuilder<K,V> remove(Object key)

      remove.

      Parameters:
      key - to be removed
      Returns:
      the instance itself in order to use it in a fluent way.
    • putAll

      public MapBuilder<K,V> putAll(Map<? extends K,? extends V> map)

      putAll.

      Parameters:
      map - to be put
      Returns:
      the instance itself in order to use it in a fluent way.
    • clear

      public MapBuilder<K,V> clear()
      Clears the contained collector
      Returns:
      the instance itself in order to use it in a fluent way.
    • toMutableMap

      public Map<K,V> toMutableMap()

      toMutableMap.

      Returns:
      a mutable Map representation of the builders content, the actual implementation is a HashMap
    • toImmutableMap

      public Map<K,V> toImmutableMap()

      toImmutableMap.

      Returns:
      an immutable Map representation of the builders content, the actual implementation does not create a copy but provides an unmodifiable view using Collections.unmodifiableMap(Map)
    • copyFrom

      public static <K, V> MapBuilder<K,V> copyFrom(Map<K,V> original)

      copyFrom.

      Type Parameters:
      K - the type of keys maintained by this map
      V - the type of mapped values
      Parameters:
      original - map used to initialize the contained collector
      Returns:
      an instance of MapBuilder initialized with a copy of the given Map.
    • from

      public static <K, V> MapBuilder<K,V> from(K k1, V v1)
      Shorthand for creating a MapBuilder from a given key/value pair
      Type Parameters:
      K - the type of keys maintained by this map
      V - the type of mapped values
      Parameters:
      k1 - key to be added
      v1 - value to be added
      Returns:
      an instance of MapBuilder initialized with the given key/value pair.
    • from

      public static <K, V> MapBuilder<K,V> from(K k1, V v1, K k2, V v2)
      Shorthand for creating a MapBuilder from given key/value pairs
      Type Parameters:
      K - the type of keys maintained by this map
      V - the type of mapped values
      Parameters:
      k1 - key to be added
      v1 - value to be added
      k2 - key to be added
      v2 - value to be added
      Returns:
      an instance of MapBuilder initialized with the given key/value pairs.
    • from

      public static <K, V> MapBuilder<K,V> from(K k1, V v1, K k2, V v2, K k3, V v3)
      Shorthand for creating a MapBuilder from given key/value pairs
      Type Parameters:
      K - the type of keys maintained by this map
      V - the type of mapped values
      Parameters:
      k1 - key to be added
      v1 - value to be added
      k2 - key to be added
      v2 - value to be added
      k3 - key to be added
      v3 - value to be added
      Returns:
      an instance of MapBuilder initialized with the given key/value pairs.
    • from

      public static <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 a MapBuilder from given key/value pairs
      Type Parameters:
      K - the type of keys maintained by this map
      V - the type of mapped values
      Parameters:
      k1 - key to be added
      v1 - value to be added
      k2 - key to be added
      v2 - value to be added
      k3 - key to be added
      v3 - value to be added
      k4 - key to be added
      v4 - value to be added
      Returns:
      an instance of MapBuilder initialized with the given key/value pairs.