Class MapBuilder<K,V>

java.lang.Object
org.apache.juneau.internal.MapBuilder<K,V>
Type Parameters:
K - Key type.
V - Value type.

public final class MapBuilder<K,V> extends Object
Builder for maps.
See Also:
  • Constructor Details

    • MapBuilder

      public MapBuilder(Class<K> keyType, Class<V> valueType, Type... valueTypeArgs)
      Constructor.
      Parameters:
      keyType - The key type.
      valueType - The value type.
      valueTypeArgs - The value type generic arguments if there are any.
    • MapBuilder

      public MapBuilder(Map<K,V> addTo)
      Constructor.
      Parameters:
      addTo - The map to add to.
  • Method Details

    • build

      public Map<K,V> build()
      Builds the map.
      Returns:
      A map conforming to the settings on this builder.
    • sparse

      public MapBuilder<K,V> sparse()
      When specified, the build() method will return null if the map is empty.

      Otherwise build() will never return null.

      Returns:
      This object.
    • unmodifiable

      When specified, build() will return an unmodifiable map.
      Returns:
      This object.
    • copy

      public MapBuilder<K,V> copy()
      Forces the existing set to be copied instead of appended to.
      Returns:
      This object.
    • sorted

      public MapBuilder<K,V> sorted()
      Converts the set into a SortedMap.
      Returns:
      This object.
    • sorted

      public MapBuilder<K,V> sorted(Comparator<K> comparator)
      Converts the set into a SortedMap using the specified comparator.
      Parameters:
      comparator - The comparator to use for sorting.
      Returns:
      This object.
    • addAll

      public MapBuilder<K,V> addAll(Map<K,V> value)
      Appends the contents of the specified map into this map.

      This is a no-op if the value is null.

      Parameters:
      value - The map to add to this map.
      Returns:
      This object.
    • add

      public MapBuilder<K,V> add(K key, V value)
      Adds a single entry to this map.
      Parameters:
      key - The map key.
      value - The map value.
      Returns:
      This object.
    • addJson

      public MapBuilder<K,V> addJson(String... values)
      Adds entries to this list via JSON object strings.
      Parameters:
      values - The JSON object strings to parse and add to this list.
      Returns:
      This object.
    • addAny

      public MapBuilder<K,V> addAny(Object... values)
      Adds arbitrary values to this list.

      Objects can be any of the following:

      • Maps of key/value types convertible to the key/value types of this map.
      • JSON object strings parsed and convertible to the key/value types of this map.
      Parameters:
      values - The values to add.
      Returns:
      This object.
    • addPairs

      public MapBuilder<K,V> addPairs(Object... pairs)
      Adds a list of key/value pairs to this map.
      Parameters:
      pairs - The pairs to add.
      Returns:
      This object.