java.lang.Object
org.elasticsearch.common.util.Maps
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <K,
V> Map<K, V> copyMapWithAddedEntry
(Map<K, V> map, K key, V value) Adds an entry to an immutable map by copying the underlying map and adding the new entry.static <K,
V> Map<K, V> copyMapWithAddedOrReplacedEntry
(Map<K, V> map, K key, V value) Adds a new entry to or replaces an existing entry in an immutable map by copying the underlying map and adding the new or replacing the existing entry.static <K,
V> Map<K, V> copyMapWithRemovedEntry
(Map<K, V> map, K key) Remove the specified key from the provided immutable map by copying the underlying map and filtering out the specified key if that key exists.static <K,
V> Map<K, V> This method creates a copy of thesource
map usingcopyValueFunction
to create a defensive copy of each value.static <K,
V> boolean deepEquals
(Map<K, V> left, Map<K, V> right) Returnstrue
if the two specified maps are equal to one another.Returns an array where all internal maps and optionally arrays are flattened into the root map.static <K,
V> Map<K, V> newHashMapWithExpectedSize
(int expectedSize) Returns a hash map with a capacity sufficient to keep expectedSize elements without being resized.static <K,
V> LinkedHashMap<K, V> newLinkedHashMapWithExpectedSize
(int expectedSize) Returns a linked hash map with a capacity sufficient to keep expectedSize elements without being resized.static <K,
V> Map<K, V> newMapWithExpectedSize
(int expectedSize) Returns a map with a capacity sufficient to keep expectedSize elements without being resized.static <K,
V> Map<K, V> ofEntries
(Collection<Map.Entry<K, V>> entries) A convenience method to convert a collection of map entries to a map.toUnmodifiableOrderedMap
(Function<T, ? extends K> keyMapper, Function<T, ? extends V> valueMapper) Returns aCollector
that accumulates the input elements into a linked hash map and finishes the resulting set into an unmodifiable map.static <T,
K, V> Collector<T, ?, NavigableMap<K, V>> toUnmodifiableSortedMap
(Function<T, ? extends K> keyMapper, Function<T, ? extends V> valueMapper) Returns aCollector
that accumulates the input elements into a sorted map and finishes the resulting set into an unmodifiable sorted map.static <K,
V1, V2> Map<K, V2> transformValues
(Map<K, V1> source, Function<V1, V2> copyValueFunction) Copy a map and transform it values using supplied function
-
Constructor Details
-
Maps
public Maps()
-
-
Method Details
-
copyMapWithAddedEntry
Adds an entry to an immutable map by copying the underlying map and adding the new entry. This method expects there is not already a mapping for the specified key in the map.- Type Parameters:
K
- the type of the keys in the mapV
- the type of the values in the map- Parameters:
map
- the immutable map to concatenate the entry tokey
- the key of the new entryvalue
- the value of the entry- Returns:
- an immutable map that contains the items from the specified map and the concatenated entry
-
copyMapWithAddedOrReplacedEntry
Adds a new entry to or replaces an existing entry in an immutable map by copying the underlying map and adding the new or replacing the existing entry.- Type Parameters:
K
- the type of the keys in the mapV
- the type of the values in the map- Parameters:
map
- the immutable map to add to or replace inkey
- the key of the new entryvalue
- the value of the new entry- Returns:
- an immutable map that contains the items from the specified map and a mapping from the specified key to the specified value
-
copyMapWithRemovedEntry
Remove the specified key from the provided immutable map by copying the underlying map and filtering out the specified key if that key exists.- Type Parameters:
K
- the type of the keys in the mapV
- the type of the values in the map- Parameters:
map
- the immutable map to remove the key fromkey
- the key to be removed- Returns:
- an immutable map that contains the items from the specified map with the provided key removed
-
ofEntries
A convenience method to convert a collection of map entries to a map. The primary reason this method exists is to have a single source file with an unchecked suppression rather than scattered at the various call sites.- Type Parameters:
K
- the type of the keysV
- the type of the values- Parameters:
entries
- the entries to convert to a map- Returns:
- an immutable map containing the specified entries
-
deepEquals
Returnstrue
if the two specified maps are equal to one another. Two maps are considered equal if both represent identical mappings where values are checked with Objects.deepEquals. The primary use case is to check if two maps with array values are equal.- Parameters:
left
- one map to be tested for equalityright
- the other map to be tested for equality- Returns:
true
if the two maps are equal
-
flatten
public static Map<String,Object> flatten(Map<String, Object> map, boolean flattenArrays, boolean ordered) Returns an array where all internal maps and optionally arrays are flattened into the root map. For example the map {"foo": {"bar": 1, "baz": [2, 3]}} will become {"foo.bar": 1, "foo.baz.0": 2, "foo.baz.1": 3}. Note that if maps contains keys with "." or numbers it is possible that such keys will be silently overridden. For example the map {"foo": {"bar": 1}, "foo.bar": 2} will become {"foo.bar": 1} or {"foo.bar": 2}.- Parameters:
map
- - input to be flattenedflattenArrays
- - if false, arrays will be ignoredordered
- - if true the resulted map will be sorted- Returns:
-
toUnmodifiableSortedMap
public static <T,K, Collector<T,V> ?, toUnmodifiableSortedMapNavigableMap<K, V>> (Function<T, ? extends K> keyMapper, Function<T, ? extends V> valueMapper) Returns aCollector
that accumulates the input elements into a sorted map and finishes the resulting set into an unmodifiable sorted map. The resulting read-only view through the unmodifiable sorted map is a sorted map.- Type Parameters:
T
- the type of the input elements- Returns:
- an unmodifiable
NavigableMap
where the underlying map is sorted
-
toUnmodifiableOrderedMap
public static <T,K, Collector<T,V> ?, toUnmodifiableOrderedMapMap<K, V>> (Function<T, ? extends K> keyMapper, Function<T, ? extends V> valueMapper) Returns aCollector
that accumulates the input elements into a linked hash map and finishes the resulting set into an unmodifiable map. The resulting read-only view through the unmodifiable map is a linked hash map.- Type Parameters:
T
- the type of the input elements- Returns:
- an unmodifiable
Map
where the underlying map has a consistent order
-
newMapWithExpectedSize
Returns a map with a capacity sufficient to keep expectedSize elements without being resized.- Type Parameters:
K
- the key typeV
- the value type- Parameters:
expectedSize
- the expected amount of elements in the map- Returns:
- a new pre-sized
HashMap
-
newHashMapWithExpectedSize
Returns a hash map with a capacity sufficient to keep expectedSize elements without being resized.- Type Parameters:
K
- the key typeV
- the value type- Parameters:
expectedSize
- the expected amount of elements in the map- Returns:
- a new pre-sized
HashMap
-
newLinkedHashMapWithExpectedSize
Returns a linked hash map with a capacity sufficient to keep expectedSize elements without being resized.- Type Parameters:
K
- the key typeV
- the value type- Parameters:
expectedSize
- the expected amount of elements in the map- Returns:
- a new pre-sized
LinkedHashMap
-
copyOf
This method creates a copy of thesource
map usingcopyValueFunction
to create a defensive copy of each value. -
transformValues
public static <K,V1, Map<K,V2> V2> transformValues(Map<K, V1> source, Function<V1, V2> copyValueFunction) Copy a map and transform it values using supplied function
-