public class

MapBuilder

extends Object
java.lang.Object
   ↳ com.atlassian.jira.util.collect.MapBuilder<K, V>

Class Overview

Utility for easily creating Maps of all standard types. If your map does not need to contain null as either a key or a value, then builder() should be preferred.

Summary

Fields
protected final Map<K, V> map
Protected Constructors
MapBuilder()
prefer newBuilder() for inferred generic arguments.
Public Methods
MapBuilder<K, V> add(K key, V value)
Add a key/value.
MapBuilder<K, V> addAll(Map<? extends K, ? extends V> map)
Add all the entries contained in the map.
MapBuilder<K, V> addIfValueNotNull(K key, V value)
Add a key/value only if the value is not null.
static <K, V> Map<K, V> build(K key1, V value1, K key2, V value2, K key3, V value3, K key4, V value4)
Returns an immutable Map containing the given key-value pairs.
static <K, V> Map<K, V> build(K key1, V value1, K key2, V value2, K key3, V value3)
Returns an immutable Map containing the given key-value pairs.
static <K, V> Map<K, V> build(K key1, V value1, K key2, V value2)
Returns an immutable Map containing the given key-value pairs.
static <K, V> Map<K, V> build(K key, V value)
Returns an immutable Map containing the given key-value pair.
static <K, V> Map<K, V> emptyMap()
Static factory method for creating an empty map.
static <K, V> MapBuilder<K, V> newBuilder(Map<? extends K, ? extends V> map)
Static factory method for creating a fresh MapBuilder based on the contents of a source Map.
static <K, V> MapBuilder<K, V> newBuilder()
Static factory method for creating a fresh MapBuilder.
static <K, V> MapBuilder<K, V> newBuilder(K key1, V value1, K key2, V value2)
Static factory method for creating a fresh MapBuilder and adding the given key/value pairs.
static <K, V> MapBuilder<K, V> newBuilder(K key, V value)
Static factory method for creating a fresh MapBuilder and adding the given key/value pair.
static <K, V> MapBuilder<K, V> newBuilder(K key1, V value1, K key2, V value2, K key3, V value3)
Static factory method for creating a fresh MapBuilder and adding the given key/value pairs.
static <K, V> MapBuilder<K, V> newBuilder(K key1, V value1, K key2, V value2, K key3, V value3, K key4, V value4)
Static factory method for creating a fresh MapBuilder and adding the given key/value pairs.
static <K, V> Map<K, V> singletonMap(K key, V value)
Static factory method for creating an immutable Map with a single entry.
@Deprecated Map<K, V> toFastMap()
This method is deprecated. Use ImmutableMap directly instead. Since v6.1.
SortedMap<K, V> toFastSortedMap()
This method is deprecated. Use ImmutableSortedMap directly instead. Since v6.1.
SortedMap<K, V> toFastSortedMap(Comparator<K> comparator)
This method is deprecated. Use ImmutableSortedMap directly instead. Since v6.1.
HashMap<K, V> toHashMap()
Returns a mutable HashMap where the entries are in no defined order.
@Deprecated Map<K, V> toImmutableMap()
This method is deprecated. use toMap() instead, or consider using ImmutableMap if if the map does not need to accept nulls.
LinkedHashMap<K, V> toLinkedHashMap()
Returns a mutable LinkedHashMap where the entries are in the same order as they were added.
Map<K, V> toListOrderedMap()
Returns an immutable Map where the entries are in the same order as they were added.
Map<K, V> toMap()
Returns an immutable Map where the entries are in no defined order.
Map<K, V> toMutableMap()
SortedMap<K, V> toSortedMap()
SortedMap<K, V> toSortedMap(Comparator<K> comparator)
TreeMap<K, V> toTreeMap(Comparator<K> comparator)
TreeMap<K, V> toTreeMap()
[Expand]
Inherited Methods
From class java.lang.Object

Fields

protected final Map<K, V> map

Protected Constructors

protected MapBuilder ()

prefer newBuilder() for inferred generic arguments.

Public Methods

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

Add a key/value. Allows nulls. Prefer builder() if that is not needed.

Parameters
key the key
value the value
Returns
  • this

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

Add all the entries contained in the map. Accepts a null map and does nothing.

Parameters
map the map to add
Returns
  • this

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

Add a key/value only if the value is not null. If the intent is to produce an immutable map that does not contain null values, then ImmutableMap should be preferred. This method should only be useful when intending to call toMutableMap().

Parameters
key the key
value the value
Returns
  • this

public static Map<K, V> build (K key1, V value1, K key2, V value2, K key3, V value3, K key4, V value4)

Returns an immutable Map containing the given key-value pairs. Prefer of(Object, Object, Object, Object, Object, Object, Object, Object) if none of the keys or values will ever need to be null.

Parameters
key1 The Key
value1 The Value
key2 The Key
value2 The Value
key3 The Key
value3 The Value
key4 The Key
value4 The Value
Returns
  • an immutable Map containing the given key-value pair.

public static Map<K, V> build (K key1, V value1, K key2, V value2, K key3, V value3)

Returns an immutable Map containing the given key-value pairs. Prefer of(Object, Object, Object, Object, Object, Object) if none of the keys or values will ever need to be null.

Parameters
key1 The Key
value1 The Value
key2 The Key
value2 The Value
key3 The Key
value3 The Value
Returns
  • an immutable Map containing the given key-value pair.

public static Map<K, V> build (K key1, V value1, K key2, V value2)

Returns an immutable Map containing the given key-value pairs. Prefer of(Object, Object, Object, Object) if none of the keys or values will ever need to be null.

Parameters
key1 The Key
value1 The Value
key2 The Key
value2 The Value
Returns
  • an immutable Map containing the given key-value pair.

public static Map<K, V> build (K key, V value)

Returns an immutable Map containing the given key-value pair. Prefer of(Object, Object) if neither key nor value will ever need to be null.

Parameters
key The Key
value The Value
Returns
  • an immutable Map containing the given key-value pair.

public static Map<K, V> emptyMap ()

Static factory method for creating an empty map. This is exactly equivalent to Collections.emptyMap() and is provided only for completeness.

Returns
  • an empty immutable map

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

Static factory method for creating a fresh MapBuilder based on the contents of a source Map.

Parameters
map the source map, may be null
Returns
  • a new MapBuilder, populated with the values from map, if one is provided

public static MapBuilder<K, V> newBuilder ()

Static factory method for creating a fresh MapBuilder.

Returns

public static MapBuilder<K, V> newBuilder (K key1, V value1, K key2, V value2)

Static factory method for creating a fresh MapBuilder and adding the given key/value pairs.

Parameters
key1 the key
value1 the value
key2 the key
value2 the value
Returns

public static MapBuilder<K, V> newBuilder (K key, V value)

Static factory method for creating a fresh MapBuilder and adding the given key/value pair.

Parameters
key the key
value the value
Returns

public static MapBuilder<K, V> newBuilder (K key1, V value1, K key2, V value2, K key3, V value3)

Static factory method for creating a fresh MapBuilder and adding the given key/value pairs.

Parameters
key1 the key
value1 the value
key2 the key
value2 the value
key3 the key
value3 the value
Returns

public static MapBuilder<K, V> newBuilder (K key1, V value1, K key2, V value2, K key3, V value3, K key4, V value4)

Static factory method for creating a fresh MapBuilder and adding the given key/value pairs.

Parameters
key1 the key
value1 the value
key2 the key
value2 the value
key3 the key
value3 the value
key4 the key
value4 the value
Returns

public static Map<K, V> singletonMap (K key, V value)

Static factory method for creating an immutable Map with a single entry. This is exactly equivalent to Collections.singletonMap(key, value) and is only provided for completeness.

Parameters
key the key
value the value
Returns

@Deprecated public Map<K, V> toFastMap ()

This method is deprecated.
Use ImmutableMap directly instead. Since v6.1.

This returns a guava ImmutableMap, which does not accept nulls. Please use that class directly, instead.

Returns
  • a fast immutable map
Throws
NullPointerException if there are any nulls in this builder.

public SortedMap<K, V> toFastSortedMap ()

This method is deprecated.
Use ImmutableSortedMap directly instead. Since v6.1.

This returns a guava ImmutableSortedMap, which does not accept nulls. Please use that class directly, instead.

Returns
  • a fast immutable sorted map
Throws
NullPointerException if there are any nulls in this builder.
ClassCastException if the keys in this builder do not implement Comparable.

public SortedMap<K, V> toFastSortedMap (Comparator<K> comparator)

This method is deprecated.
Use ImmutableSortedMap directly instead. Since v6.1.

This returns a guava ImmutableSortedMap, which does not accept nulls. Please use that class directly, instead.

Parameters
comparator for sorting the map
Returns
  • a fast immutable sorted map
Throws
NullPointerException if there are any nulls in this builder.
ClassCastException if the keys in this builder do not implement Comparable.

public HashMap<K, V> toHashMap ()

Returns a mutable HashMap where the entries are in no defined order.

Returns
  • a mutable HashMap where the entries are in no defined order.

@Deprecated public Map<K, V> toImmutableMap ()

This method is deprecated.
use toMap() instead, or consider using ImmutableMap if if the map does not need to accept nulls.

Returns an immutable map backed by a HashMap, which will accept nulls. If that ability is not needed, then ImmutableMap is preferred.

Returns
  • an immutable map backed by a HashMap

public LinkedHashMap<K, V> toLinkedHashMap ()

Returns a mutable LinkedHashMap where the entries are in the same order as they were added.

Returns
  • a mutable LinkedHashMap where the entries are in the same order as they were added.

public Map<K, V> toListOrderedMap ()

Returns an immutable Map where the entries are in the same order as they were added. The map may contain nulls. If this ability is not needed, then using ImmutableMap is preferred.

Returns
  • an immutable Map where the entries are in the same order as they were added.

public Map<K, V> toMap ()

Returns an immutable Map where the entries are in no defined order. The map may contain nulls. If this ability is not needed, then ImmutableMap is preferred.

Returns
  • an immutable Map where the entries are in no defined order.

public Map<K, V> toMutableMap ()

Returns
  • a mutable Map where the entries are in no defined order.

public SortedMap<K, V> toSortedMap ()

Returns
  • an immutable SortedMap where the entries are in the natural order of the keys. Note that the key type must implement Comparable.

public SortedMap<K, V> toSortedMap (Comparator<K> comparator)

Parameters
comparator used to sort the keys.
Returns

public TreeMap<K, V> toTreeMap (Comparator<K> comparator)

Parameters
comparator used to sort the keys.
Returns
  • a mutable TreeMap where the entries are in the order defined by the supplied Comparator.

public TreeMap<K, V> toTreeMap ()

Returns
  • a mutable TreeMap where the entries are in the natural order of the keys. Note that the key type must implement Comparable.