public class MapUtils
extends java.lang.Object
Map
and SortedMap
instances.
It contains various type safe methods as well as other useful features like deep copying.
It also provides the following decorators:
fixedSizeMap(Map)
fixedSizeSortedMap(SortedMap)
lazyMap(Map,Factory)
lazyMap(Map,Transformer)
lazySortedMap(SortedMap,Factory)
lazySortedMap(SortedMap,Transformer)
predicatedMap(Map,Predicate,Predicate)
predicatedSortedMap(SortedMap,Predicate,Predicate)
transformedMap(Map, Transformer, Transformer)
transformedSortedMap(SortedMap, Transformer, Transformer)
multiValueMap( Map )
multiValueMap( Map, Class )
multiValueMap( Map, Factory )
Modifier and Type | Field and Description |
---|---|
static java.util.SortedMap |
EMPTY_SORTED_MAP
An empty unmodifiable sorted map.
|
Modifier and Type | Method and Description |
---|---|
static void |
debugPrint(java.io.PrintStream out,
java.lang.Object label,
java.util.Map<?,?> map)
Prints the given map with nice line breaks.
|
static <K,V> java.util.Map<K,V> |
emptyIfNull(java.util.Map<K,V> map)
Returns an immutable empty map if the argument is
null ,
or the argument itself otherwise. |
static <K,V> IterableMap<K,V> |
fixedSizeMap(java.util.Map<K,V> map)
Returns a fixed-sized map backed by the given map.
|
static <K,V> java.util.SortedMap<K,V> |
fixedSizeSortedMap(java.util.SortedMap<K,V> map)
Returns a fixed-sized sorted map backed by the given sorted map.
|
static <K> java.lang.Boolean |
getBoolean(java.util.Map<? super K,?> map,
K key)
Gets a Boolean from a Map in a null-safe manner.
|
static <K> java.lang.Boolean |
getBoolean(java.util.Map<? super K,?> map,
K key,
java.lang.Boolean defaultValue)
Looks up the given key in the given map, converting the result into
a boolean, using the default value if the the conversion fails.
|
static <K> boolean |
getBooleanValue(java.util.Map<? super K,?> map,
K key)
Gets a boolean from a Map in a null-safe manner.
|
static <K> boolean |
getBooleanValue(java.util.Map<? super K,?> map,
K key,
boolean defaultValue)
Gets a boolean from a Map in a null-safe manner,
using the default value if the the conversion fails.
|
static <K> java.lang.Byte |
getByte(java.util.Map<? super K,?> map,
K key)
Gets a Byte from a Map in a null-safe manner.
|
static <K> java.lang.Byte |
getByte(java.util.Map<? super K,?> map,
K key,
java.lang.Byte defaultValue)
Looks up the given key in the given map, converting the result into
a byte, using the default value if the the conversion fails.
|
static <K> byte |
getByteValue(java.util.Map<? super K,?> map,
K key)
Gets a byte from a Map in a null-safe manner.
|
static <K> byte |
getByteValue(java.util.Map<? super K,?> map,
K key,
byte defaultValue)
Gets a byte from a Map in a null-safe manner,
using the default value if the the conversion fails.
|
static <K> java.lang.Double |
getDouble(java.util.Map<? super K,?> map,
K key)
Gets a Double from a Map in a null-safe manner.
|
static <K> java.lang.Double |
getDouble(java.util.Map<? super K,?> map,
K key,
java.lang.Double defaultValue)
Looks up the given key in the given map, converting the result into
a double, using the default value if the the conversion fails.
|
static <K> double |
getDoubleValue(java.util.Map<? super K,?> map,
K key)
Gets a double from a Map in a null-safe manner.
|
static <K> double |
getDoubleValue(java.util.Map<? super K,?> map,
K key,
double defaultValue)
Gets a double from a Map in a null-safe manner,
using the default value if the the conversion fails.
|
static <K> java.lang.Float |
getFloat(java.util.Map<? super K,?> map,
K key)
Gets a Float from a Map in a null-safe manner.
|
static <K> java.lang.Float |
getFloat(java.util.Map<? super K,?> map,
K key,
java.lang.Float defaultValue)
Looks up the given key in the given map, converting the result into
a float, using the default value if the the conversion fails.
|
static <K> float |
getFloatValue(java.util.Map<? super K,?> map,
K key)
Gets a float from a Map in a null-safe manner.
|
static <K> float |
getFloatValue(java.util.Map<? super K,?> map,
K key,
float defaultValue)
Gets a float from a Map in a null-safe manner,
using the default value if the the conversion fails.
|
static <K> java.lang.Integer |
getInteger(java.util.Map<? super K,?> map,
K key)
Gets a Integer from a Map in a null-safe manner.
|
static <K> java.lang.Integer |
getInteger(java.util.Map<? super K,?> map,
K key,
java.lang.Integer defaultValue)
Looks up the given key in the given map, converting the result into
an integer, using the default value if the the conversion fails.
|
static <K> int |
getIntValue(java.util.Map<? super K,?> map,
K key)
Gets an int from a Map in a null-safe manner.
|
static <K> int |
getIntValue(java.util.Map<? super K,?> map,
K key,
int defaultValue)
Gets an int from a Map in a null-safe manner,
using the default value if the the conversion fails.
|
static <K> java.lang.Long |
getLong(java.util.Map<? super K,?> map,
K key)
Gets a Long from a Map in a null-safe manner.
|
static <K> java.lang.Long |
getLong(java.util.Map<? super K,?> map,
K key,
java.lang.Long defaultValue)
Looks up the given key in the given map, converting the result into
a long, using the default value if the the conversion fails.
|
static <K> long |
getLongValue(java.util.Map<? super K,?> map,
K key)
Gets a long from a Map in a null-safe manner.
|
static <K> long |
getLongValue(java.util.Map<? super K,?> map,
K key,
long defaultValue)
Gets a long from a Map in a null-safe manner,
using the default value if the the conversion fails.
|
static <K> java.util.Map<?,?> |
getMap(java.util.Map<? super K,?> map,
K key)
Gets a Map from a Map in a null-safe manner.
|
static <K> java.util.Map<?,?> |
getMap(java.util.Map<? super K,?> map,
K key,
java.util.Map<?,?> defaultValue)
Looks up the given key in the given map, converting the result into
a map, using the default value if the the conversion fails.
|
static <K> java.lang.Number |
getNumber(java.util.Map<? super K,?> map,
K key)
Gets a Number from a Map in a null-safe manner.
|
static <K> java.lang.Number |
getNumber(java.util.Map<? super K,?> map,
K key,
java.lang.Number defaultValue)
Looks up the given key in the given map, converting the result into
a number, using the default value if the the conversion fails.
|
static <K,V> V |
getObject(java.util.Map<? super K,V> map,
K key)
Gets from a Map in a null-safe manner.
|
static <K,V> V |
getObject(java.util.Map<K,V> map,
K key,
V defaultValue)
Looks up the given key in the given map, converting null into the
given default value.
|
static <K> java.lang.Short |
getShort(java.util.Map<? super K,?> map,
K key)
Gets a Short from a Map in a null-safe manner.
|
static <K> java.lang.Short |
getShort(java.util.Map<? super K,?> map,
K key,
java.lang.Short defaultValue)
Looks up the given key in the given map, converting the result into
a short, using the default value if the the conversion fails.
|
static <K> short |
getShortValue(java.util.Map<? super K,?> map,
K key)
Gets a short from a Map in a null-safe manner.
|
static <K> short |
getShortValue(java.util.Map<? super K,?> map,
K key,
short defaultValue)
Gets a short from a Map in a null-safe manner,
using the default value if the the conversion fails.
|
static <K> java.lang.String |
getString(java.util.Map<? super K,?> map,
K key)
Gets a String from a Map in a null-safe manner.
|
static <K> java.lang.String |
getString(java.util.Map<? super K,?> map,
K key,
java.lang.String defaultValue)
Looks up the given key in the given map, converting the result into
a string, using the default value if the the conversion fails.
|
static <K,V> java.util.Map<V,K> |
invertMap(java.util.Map<K,V> map)
Inverts the supplied map returning a new HashMap such that the keys of
the input are swapped with the values.
|
static boolean |
isEmpty(java.util.Map<?,?> map)
Null-safe check if the specified map is empty.
|
static boolean |
isNotEmpty(java.util.Map<?,?> map)
Null-safe check if the specified map is not empty.
|
static <K,V> IterableMap<K,V> |
iterableMap(java.util.Map<K,V> map)
Get the specified
Map as an IterableMap . |
static <K,V> IterableSortedMap<K,V> |
iterableSortedMap(java.util.SortedMap<K,V> sortedMap)
Get the specified
SortedMap as an IterableSortedMap . |
static <K,V> IterableMap<K,V> |
lazyMap(java.util.Map<K,V> map,
Factory<? extends V> factory)
Returns a "lazy" map whose values will be created on demand.
|
static <K,V> IterableMap<K,V> |
lazyMap(java.util.Map<K,V> map,
Transformer<? super K,? extends V> transformerFactory)
Returns a "lazy" map whose values will be created on demand.
|
static <K,V> java.util.SortedMap<K,V> |
lazySortedMap(java.util.SortedMap<K,V> map,
Factory<? extends V> factory)
Returns a "lazy" sorted map whose values will be created on demand.
|
static <K,V> java.util.SortedMap<K,V> |
lazySortedMap(java.util.SortedMap<K,V> map,
Transformer<? super K,? extends V> transformerFactory)
Returns a "lazy" sorted map whose values will be created on demand.
|
static <K,V> MultiValueMap<K,V> |
multiValueMap(java.util.Map<K,? super java.util.Collection<V>> map)
Deprecated.
since 4.1, use
MultiValuedMap instead |
static <K,V,C extends java.util.Collection<V>> |
multiValueMap(java.util.Map<K,C> map,
java.lang.Class<C> collectionClass)
Deprecated.
since 4.1, use
MultiValuedMap instead |
static <K,V,C extends java.util.Collection<V>> |
multiValueMap(java.util.Map<K,C> map,
Factory<C> collectionFactory)
Deprecated.
since 4.1, use
MultiValuedMap instead |
static <K,V> OrderedMap<K,V> |
orderedMap(java.util.Map<K,V> map)
Returns a map that maintains the order of keys that are added
backed by the given map.
|
static <K,V,E> void |
populateMap(java.util.Map<K,V> map,
java.lang.Iterable<? extends E> elements,
Transformer<E,K> keyTransformer,
Transformer<E,V> valueTransformer)
Populates a Map using the supplied
Transformer s to transform the elements
into keys and values. |
static <K,V> void |
populateMap(java.util.Map<K,V> map,
java.lang.Iterable<? extends V> elements,
Transformer<V,K> keyTransformer)
Populates a Map using the supplied
Transformer to transform the elements
into keys, using the unaltered element as the value in the Map . |
static <K,V,E> void |
populateMap(MultiMap<K,V> map,
java.lang.Iterable<? extends E> elements,
Transformer<E,K> keyTransformer,
Transformer<E,V> valueTransformer)
Populates a MultiMap using the supplied
Transformer s to transform the elements
into keys and values. |
static <K,V> void |
populateMap(MultiMap<K,V> map,
java.lang.Iterable<? extends V> elements,
Transformer<V,K> keyTransformer)
Populates a MultiMap using the supplied
Transformer to transform the elements
into keys, using the unaltered element as the value in the MultiMap . |
static <K,V> IterableMap<K,V> |
predicatedMap(java.util.Map<K,V> map,
Predicate<? super K> keyPred,
Predicate<? super V> valuePred)
Returns a predicated (validating) map backed by the given map.
|
static <K,V> java.util.SortedMap<K,V> |
predicatedSortedMap(java.util.SortedMap<K,V> map,
Predicate<? super K> keyPred,
Predicate<? super V> valuePred)
Returns a predicated (validating) sorted map backed by the given map.
|
static <K,V> java.util.Map<K,V> |
putAll(java.util.Map<K,V> map,
java.lang.Object[] array)
Puts all the keys and values from the specified array into the map.
|
static <K> void |
safeAddToMap(java.util.Map<? super K,java.lang.Object> map,
K key,
java.lang.Object value)
Protects against adding null values to a map.
|
static <K,V> java.util.Map<K,V> |
synchronizedMap(java.util.Map<K,V> map)
Returns a synchronized map backed by the given map.
|
static <K,V> java.util.SortedMap<K,V> |
synchronizedSortedMap(java.util.SortedMap<K,V> map)
Returns a synchronized sorted map backed by the given sorted map.
|
static java.util.Map<java.lang.String,java.lang.Object> |
toMap(java.util.ResourceBundle resourceBundle)
Creates a new HashMap using data copied from a ResourceBundle.
|
static <K,V> java.util.Properties |
toProperties(java.util.Map<K,V> map)
Gets a new Properties object initialised with the values from a Map.
|
static <K,V> IterableMap<K,V> |
transformedMap(java.util.Map<K,V> map,
Transformer<? super K,? extends K> keyTransformer,
Transformer<? super V,? extends V> valueTransformer)
Returns a transformed map backed by the given map.
|
static <K,V> java.util.SortedMap<K,V> |
transformedSortedMap(java.util.SortedMap<K,V> map,
Transformer<? super K,? extends K> keyTransformer,
Transformer<? super V,? extends V> valueTransformer)
Returns a transformed sorted map backed by the given map.
|
static <K,V> java.util.Map<K,V> |
unmodifiableMap(java.util.Map<? extends K,? extends V> map)
Returns an unmodifiable map backed by the given map.
|
static <K,V> java.util.SortedMap<K,V> |
unmodifiableSortedMap(java.util.SortedMap<K,? extends V> map)
Returns an unmodifiable sorted map backed by the given sorted map.
|
static void |
verbosePrint(java.io.PrintStream out,
java.lang.Object label,
java.util.Map<?,?> map)
Prints the given map with nice line breaks.
|
public static final java.util.SortedMap EMPTY_SORTED_MAP
public static <K,V> V getObject(java.util.Map<? super K,V> map, K key)
K
- the key typeV
- the value typemap
- the map to usekey
- the key to look upnull
if null map inputpublic static <K> java.lang.String getString(java.util.Map<? super K,?> map, K key)
The String is obtained via toString
.
K
- the key typemap
- the map to usekey
- the key to look upnull
if null map inputpublic static <K> java.lang.Boolean getBoolean(java.util.Map<? super K,?> map, K key)
If the value is a Boolean
it is returned directly.
If the value is a String
and it equals 'true' ignoring case
then true
is returned, otherwise false
.
If the value is a Number
an integer zero value returns
false
and non-zero returns true
.
Otherwise, null
is returned.
K
- the key typemap
- the map to usekey
- the key to look upnull
if null map inputpublic static <K> java.lang.Number getNumber(java.util.Map<? super K,?> map, K key)
If the value is a Number
it is returned directly.
If the value is a String
it is converted using
NumberFormat.parse(String)
on the system default formatter
returning null
if the conversion fails.
Otherwise, null
is returned.
K
- the key typemap
- the map to usekey
- the key to look upnull
if null map inputpublic static <K> java.lang.Byte getByte(java.util.Map<? super K,?> map, K key)
The Byte is obtained from the results of getNumber(Map,Object)
.
K
- the key typemap
- the map to usekey
- the key to look upnull
if null map inputpublic static <K> java.lang.Short getShort(java.util.Map<? super K,?> map, K key)
The Short is obtained from the results of getNumber(Map,Object)
.
K
- the key typemap
- the map to usekey
- the key to look upnull
if null map inputpublic static <K> java.lang.Integer getInteger(java.util.Map<? super K,?> map, K key)
The Integer is obtained from the results of getNumber(Map,Object)
.
K
- the key typemap
- the map to usekey
- the key to look upnull
if null map inputpublic static <K> java.lang.Long getLong(java.util.Map<? super K,?> map, K key)
The Long is obtained from the results of getNumber(Map,Object)
.
K
- the key typemap
- the map to usekey
- the key to look upnull
if null map inputpublic static <K> java.lang.Float getFloat(java.util.Map<? super K,?> map, K key)
The Float is obtained from the results of getNumber(Map,Object)
.
K
- the key typemap
- the map to usekey
- the key to look upnull
if null map inputpublic static <K> java.lang.Double getDouble(java.util.Map<? super K,?> map, K key)
The Double is obtained from the results of getNumber(Map,Object)
.
K
- the key typemap
- the map to usekey
- the key to look upnull
if null map inputpublic static <K> java.util.Map<?,?> getMap(java.util.Map<? super K,?> map, K key)
If the value returned from the specified map is not a Map then
null
is returned.
K
- the key typemap
- the map to usekey
- the key to look upnull
if null map inputpublic static <K,V> V getObject(java.util.Map<K,V> map, K key, V defaultValue)
K
- the key typeV
- the value typemap
- the map whose value to look upkey
- the key of the value to look up in that mapdefaultValue
- what to return if the value is nullpublic static <K> java.lang.String getString(java.util.Map<? super K,?> map, K key, java.lang.String defaultValue)
K
- the key typemap
- the map whose value to look upkey
- the key of the value to look up in that mapdefaultValue
- what to return if the value is null or if the
conversion failspublic static <K> java.lang.Boolean getBoolean(java.util.Map<? super K,?> map, K key, java.lang.Boolean defaultValue)
K
- the key typemap
- the map whose value to look upkey
- the key of the value to look up in that mapdefaultValue
- what to return if the value is null or if the
conversion failspublic static <K> java.lang.Number getNumber(java.util.Map<? super K,?> map, K key, java.lang.Number defaultValue)
K
- the key typemap
- the map whose value to look upkey
- the key of the value to look up in that mapdefaultValue
- what to return if the value is null or if the
conversion failspublic static <K> java.lang.Byte getByte(java.util.Map<? super K,?> map, K key, java.lang.Byte defaultValue)
K
- the key typemap
- the map whose value to look upkey
- the key of the value to look up in that mapdefaultValue
- what to return if the value is null or if the
conversion failspublic static <K> java.lang.Short getShort(java.util.Map<? super K,?> map, K key, java.lang.Short defaultValue)
K
- the key typemap
- the map whose value to look upkey
- the key of the value to look up in that mapdefaultValue
- what to return if the value is null or if the
conversion failspublic static <K> java.lang.Integer getInteger(java.util.Map<? super K,?> map, K key, java.lang.Integer defaultValue)
K
- the key typemap
- the map whose value to look upkey
- the key of the value to look up in that mapdefaultValue
- what to return if the value is null or if the
conversion failspublic static <K> java.lang.Long getLong(java.util.Map<? super K,?> map, K key, java.lang.Long defaultValue)
K
- the key typemap
- the map whose value to look upkey
- the key of the value to look up in that mapdefaultValue
- what to return if the value is null or if the
conversion failspublic static <K> java.lang.Float getFloat(java.util.Map<? super K,?> map, K key, java.lang.Float defaultValue)
K
- the key typemap
- the map whose value to look upkey
- the key of the value to look up in that mapdefaultValue
- what to return if the value is null or if the
conversion failspublic static <K> java.lang.Double getDouble(java.util.Map<? super K,?> map, K key, java.lang.Double defaultValue)
K
- the key typemap
- the map whose value to look upkey
- the key of the value to look up in that mapdefaultValue
- what to return if the value is null or if the
conversion failspublic static <K> java.util.Map<?,?> getMap(java.util.Map<? super K,?> map, K key, java.util.Map<?,?> defaultValue)
K
- the key typemap
- the map whose value to look upkey
- the key of the value to look up in that mapdefaultValue
- what to return if the value is null or if the
conversion failspublic static <K> boolean getBooleanValue(java.util.Map<? super K,?> map, K key)
If the value is a Boolean
its value is returned.
If the value is a String
and it equals 'true' ignoring case
then true
is returned, otherwise false
.
If the value is a Number
an integer zero value returns
false
and non-zero returns true
.
Otherwise, false
is returned.
K
- the key typemap
- the map to usekey
- the key to look upfalse
if null map inputpublic static <K> byte getByteValue(java.util.Map<? super K,?> map, K key)
The byte is obtained from the results of getNumber(Map,Object)
.
K
- the key typemap
- the map to usekey
- the key to look up0
if null map inputpublic static <K> short getShortValue(java.util.Map<? super K,?> map, K key)
The short is obtained from the results of getNumber(Map,Object)
.
K
- the key typemap
- the map to usekey
- the key to look up0
if null map inputpublic static <K> int getIntValue(java.util.Map<? super K,?> map, K key)
The int is obtained from the results of getNumber(Map,Object)
.
K
- the key typemap
- the map to usekey
- the key to look up0
if null map inputpublic static <K> long getLongValue(java.util.Map<? super K,?> map, K key)
The long is obtained from the results of getNumber(Map,Object)
.
K
- the key typemap
- the map to usekey
- the key to look up0L
if null map inputpublic static <K> float getFloatValue(java.util.Map<? super K,?> map, K key)
The float is obtained from the results of getNumber(Map,Object)
.
K
- the key typemap
- the map to usekey
- the key to look up0.0F
if null map inputpublic static <K> double getDoubleValue(java.util.Map<? super K,?> map, K key)
The double is obtained from the results of getNumber(Map,Object)
.
K
- the key typemap
- the map to usekey
- the key to look up0.0
if null map inputpublic static <K> boolean getBooleanValue(java.util.Map<? super K,?> map, K key, boolean defaultValue)
If the value is a Boolean
its value is returned.
If the value is a String
and it equals 'true' ignoring case
then true
is returned, otherwise false
.
If the value is a Number
an integer zero value returns
false
and non-zero returns true
.
Otherwise, defaultValue
is returned.
K
- the key typemap
- the map to usekey
- the key to look updefaultValue
- return if the value is null or if the conversion failsdefaultValue
if null map inputpublic static <K> byte getByteValue(java.util.Map<? super K,?> map, K key, byte defaultValue)
The byte is obtained from the results of getNumber(Map,Object)
.
K
- the key typemap
- the map to usekey
- the key to look updefaultValue
- return if the value is null or if the conversion failsdefaultValue
if null map inputpublic static <K> short getShortValue(java.util.Map<? super K,?> map, K key, short defaultValue)
The short is obtained from the results of getNumber(Map,Object)
.
K
- the key typemap
- the map to usekey
- the key to look updefaultValue
- return if the value is null or if the conversion failsdefaultValue
if null map inputpublic static <K> int getIntValue(java.util.Map<? super K,?> map, K key, int defaultValue)
The int is obtained from the results of getNumber(Map,Object)
.
K
- the key typemap
- the map to usekey
- the key to look updefaultValue
- return if the value is null or if the conversion failsdefaultValue
if null map inputpublic static <K> long getLongValue(java.util.Map<? super K,?> map, K key, long defaultValue)
The long is obtained from the results of getNumber(Map,Object)
.
K
- the key typemap
- the map to usekey
- the key to look updefaultValue
- return if the value is null or if the conversion failsdefaultValue
if null map inputpublic static <K> float getFloatValue(java.util.Map<? super K,?> map, K key, float defaultValue)
The float is obtained from the results of getNumber(Map,Object)
.
K
- the key typemap
- the map to usekey
- the key to look updefaultValue
- return if the value is null or if the conversion failsdefaultValue
if null map inputpublic static <K> double getDoubleValue(java.util.Map<? super K,?> map, K key, double defaultValue)
The double is obtained from the results of getNumber(Map,Object)
.
K
- the key typemap
- the map to usekey
- the key to look updefaultValue
- return if the value is null or if the conversion failsdefaultValue
if null map inputpublic static <K,V> java.util.Properties toProperties(java.util.Map<K,V> map)
A Properties object may only store non-null keys and values, thus if
the provided map contains either a key or value which is null
,
a NullPointerException
will be thrown.
K
- the key typeV
- the value typemap
- the map to convert to a Properties objectjava.lang.NullPointerException
- if a key or value in the provided map is null
public static java.util.Map<java.lang.String,java.lang.Object> toMap(java.util.ResourceBundle resourceBundle)
resourceBundle
- the resource bundle to convert, may not be nulljava.lang.NullPointerException
- if the bundle is nullpublic static void verbosePrint(java.io.PrintStream out, java.lang.Object label, java.util.Map<?,?> map)
This method prints a nicely formatted String describing the Map. Each map entry will be printed with key and value. When the value is a Map, recursive behaviour occurs.
This method is NOT thread-safe in any special way. You must manually synchronize on either this class or the stream as required.
out
- the stream to print to, must not be nulllabel
- The label to be used, may be null
.
If null
, the label is not output.
It typically represents the name of the property in a bean or similar.map
- The map to print, may be null
.
If null
, the text 'null' is output.java.lang.NullPointerException
- if the stream is null
public static void debugPrint(java.io.PrintStream out, java.lang.Object label, java.util.Map<?,?> map)
This method prints a nicely formatted String describing the Map. Each map entry will be printed with key, value and value classname. When the value is a Map, recursive behaviour occurs.
This method is NOT thread-safe in any special way. You must manually synchronize on either this class or the stream as required.
out
- the stream to print to, must not be nulllabel
- The label to be used, may be null
.
If null
, the label is not output.
It typically represents the name of the property in a bean or similar.map
- The map to print, may be null
.
If null
, the text 'null' is output.java.lang.NullPointerException
- if the stream is null
public static <K,V> java.util.Map<V,K> invertMap(java.util.Map<K,V> map)
This operation assumes that the inverse mapping is well defined. If the input map had multiple entries with the same value mapped to different keys, the returned map will map one of those keys to the value, but the exact key which will be mapped is undefined.
K
- the key typeV
- the value typemap
- the map to invert, may not be nulljava.lang.NullPointerException
- if the map is nullpublic static <K> void safeAddToMap(java.util.Map<? super K,java.lang.Object> map, K key, java.lang.Object value) throws java.lang.NullPointerException
This method checks the value being added to the map, and if it is null it is replaced by an empty string.
This could be useful if the map does not accept null values, or for receiving data from a source that may provide null or empty string which should be held in the same way in the map.
Keys are not validated. Note that this method can be used to circumvent the map's value type at runtime.
K
- the key typemap
- the map to add to, may not be nullkey
- the keyvalue
- the value, null converted to ""java.lang.NullPointerException
- if the map is nullpublic static <K,V> java.util.Map<K,V> putAll(java.util.Map<K,V> map, java.lang.Object[] array)
This method is an alternative to the Map.putAll(java.util.Map)
method and constructors. It allows you to build a map from an object array
of various possible styles.
If the first entry in the object array implements Map.Entry
or KeyValue
then the key and value are added from that object.
If the first entry in the object array is an object array itself, then
it is assumed that index 0 in the sub-array is the key and index 1 is the value.
Otherwise, the array is treated as keys and values in alternate indices.
For example, to create a color map:
Map colorMap = MapUtils.putAll(new HashMap(), new String[][] { {"RED", "#FF0000"}, {"GREEN", "#00FF00"}, {"BLUE", "#0000FF"} });or:
Map colorMap = MapUtils.putAll(new HashMap(), new String[] { "RED", "#FF0000", "GREEN", "#00FF00", "BLUE", "#0000FF" });or:
Map colorMap = MapUtils.putAll(new HashMap(), new Map.Entry[] { new DefaultMapEntry("RED", "#FF0000"), new DefaultMapEntry("GREEN", "#00FF00"), new DefaultMapEntry("BLUE", "#0000FF") });
K
- the key typeV
- the value typemap
- the map to populate, must not be nullarray
- an array to populate from, null ignoredjava.lang.NullPointerException
- if map is nulljava.lang.IllegalArgumentException
- if sub-array or entry matching used and an entry is invalidjava.lang.ClassCastException
- if the array contents is mixedpublic static <K,V> java.util.Map<K,V> emptyIfNull(java.util.Map<K,V> map)
null
,
or the argument itself otherwise.K
- the key typeV
- the value typemap
- the map, possibly null
null
public static boolean isEmpty(java.util.Map<?,?> map)
Null returns true.
map
- the map to check, may be nullpublic static boolean isNotEmpty(java.util.Map<?,?> map)
Null returns false.
map
- the map to check, may be nullpublic static <K,V> java.util.Map<K,V> synchronizedMap(java.util.Map<K,V> map)
You must manually synchronize on the returned buffer's iterator to avoid non-deterministic behavior:
Map m = MapUtils.synchronizedMap(myMap); Set s = m.keySet(); // outside synchronized block synchronized (m) { // synchronized on MAP! Iterator i = s.iterator(); while (i.hasNext()) { process (i.next()); } }This method uses the implementation in
Collections
.K
- the key typeV
- the value typemap
- the map to synchronize, must not be nullpublic static <K,V> java.util.Map<K,V> unmodifiableMap(java.util.Map<? extends K,? extends V> map)
This method uses the implementation in the decorators subpackage.
K
- the key typeV
- the value typemap
- the map to make unmodifiable, must not be nulljava.lang.NullPointerException
- if the map is nullpublic static <K,V> IterableMap<K,V> predicatedMap(java.util.Map<K,V> map, Predicate<? super K> keyPred, Predicate<? super V> valuePred)
Only objects that pass the tests in the given predicates can be added to the map. Trying to add an invalid object results in an IllegalArgumentException. Keys must pass the key predicate, values must pass the value predicate. It is important not to use the original map after invoking this method, as it is a backdoor for adding invalid objects.
K
- the key typeV
- the value typemap
- the map to predicate, must not be nullkeyPred
- the predicate for keys, null means no checkvaluePred
- the predicate for values, null means no checkjava.lang.NullPointerException
- if the Map is nullpublic static <K,V> IterableMap<K,V> transformedMap(java.util.Map<K,V> map, Transformer<? super K,? extends K> keyTransformer, Transformer<? super V,? extends V> valueTransformer)
This method returns a new map (decorating the specified map) that
will transform any new entries added to it.
Existing entries in the specified map will not be transformed.
If you want that behaviour, see TransformedMap.transformedMap(java.util.Map<K, V>, org.apache.commons.collections4.Transformer<? super K, ? extends K>, org.apache.commons.collections4.Transformer<? super V, ? extends V>)
.
Each object is passed through the transformers as it is added to the Map. It is important not to use the original map after invoking this method, as it is a backdoor for adding untransformed objects.
If there are any elements already in the map being decorated, they are NOT transformed.
K
- the key typeV
- the value typemap
- the map to transform, must not be null, typically emptykeyTransformer
- the transformer for the map keys, null means no transformationvalueTransformer
- the transformer for the map values, null means no transformationjava.lang.NullPointerException
- if the Map is nullpublic static <K,V> IterableMap<K,V> fixedSizeMap(java.util.Map<K,V> map)
Map.put(Object,Object)
method).K
- the key typeV
- the value typemap
- the map whose size to fix, must not be nulljava.lang.NullPointerException
- if the Map is nullpublic static <K,V> IterableMap<K,V> lazyMap(java.util.Map<K,V> map, Factory<? extends V> factory)
When the key passed to the returned map's Map.get(Object)
method is not present in the map, then the factory will be used
to create a new object and that object will become the value
associated with that key.
For instance:
Factory factory = new Factory() { public Object create() { return new Date(); } } Map lazyMap = MapUtils.lazyMap(new HashMap(), factory); Object obj = lazyMap.get("test");After the above code is executed,
obj
will contain
a new Date
instance. Furthermore, that Date
instance is the value for the "test"
key in the map.K
- the key typeV
- the value typemap
- the map to make lazy, must not be nullfactory
- the factory for creating new objects, must not be nulljava.lang.NullPointerException
- if the Map or Factory is nullpublic static <K,V> IterableMap<K,V> lazyMap(java.util.Map<K,V> map, Transformer<? super K,? extends V> transformerFactory)
When the key passed to the returned map's Map.get(Object)
method is not present in the map, then the factory will be used
to create a new object and that object will become the value
associated with that key. The factory is a Transformer
that will be passed the key which it must transform into the value.
For instance:
Transformer factory = new Transformer() { public Object transform(Object mapKey) { return new File(mapKey); } } Map lazyMap = MapUtils.lazyMap(new HashMap(), factory); Object obj = lazyMap.get("C:/dev");After the above code is executed,
obj
will contain
a new File
instance for the C drive dev directory.
Furthermore, that File
instance is the value for the
"C:/dev"
key in the map.
If a lazy map is wrapped by a synchronized map, the result is a simple synchronized cache. When an object is not is the cache, the cache itself calls back to the factory Transformer to populate itself, all within the same synchronized block.
K
- the key typeV
- the value typemap
- the map to make lazy, must not be nulltransformerFactory
- the factory for creating new objects, must not be nulljava.lang.NullPointerException
- if the Map or Transformer is nullpublic static <K,V> OrderedMap<K,V> orderedMap(java.util.Map<K,V> map)
If a key is added twice, the order is determined by the first add. The order is observed through the keySet, values and entrySet.
K
- the key typeV
- the value typemap
- the map to order, must not be nulljava.lang.NullPointerException
- if the Map is null@Deprecated public static <K,V> MultiValueMap<K,V> multiValueMap(java.util.Map<K,? super java.util.Collection<V>> map)
MultiValuedMap
insteadK
- the key typeV
- the value typemap
- the map to decorateMultiValueMap
@Deprecated public static <K,V,C extends java.util.Collection<V>> MultiValueMap<K,V> multiValueMap(java.util.Map<K,C> map, java.lang.Class<C> collectionClass)
MultiValuedMap
insteadK
- the key typeV
- the value typeC
- the collection class typemap
- the map to decoratecollectionClass
- the type of collections to return from the map
(must contain public no-arg constructor and extend Collection)MultiValueMap
@Deprecated public static <K,V,C extends java.util.Collection<V>> MultiValueMap<K,V> multiValueMap(java.util.Map<K,C> map, Factory<C> collectionFactory)
MultiValuedMap
insteadK
- the key typeV
- the value typeC
- the collection class typemap
- the map to decoratecollectionFactory
- a factor which creates collection objectsMultiValueMap
public static <K,V> java.util.SortedMap<K,V> synchronizedSortedMap(java.util.SortedMap<K,V> map)
You must manually synchronize on the returned buffer's iterator to avoid non-deterministic behavior:
Map m = MapUtils.synchronizedSortedMap(myMap); Set s = m.keySet(); // outside synchronized block synchronized (m) { // synchronized on MAP! Iterator i = s.iterator(); while (i.hasNext()) { process (i.next()); } }This method uses the implementation in
Collections
.K
- the key typeV
- the value typemap
- the map to synchronize, must not be nulljava.lang.NullPointerException
- if the map is nullpublic static <K,V> java.util.SortedMap<K,V> unmodifiableSortedMap(java.util.SortedMap<K,? extends V> map)
This method uses the implementation in the decorators subpackage.
K
- the key typeV
- the value typemap
- the sorted map to make unmodifiable, must not be nulljava.lang.NullPointerException
- if the map is nullpublic static <K,V> java.util.SortedMap<K,V> predicatedSortedMap(java.util.SortedMap<K,V> map, Predicate<? super K> keyPred, Predicate<? super V> valuePred)
Only objects that pass the tests in the given predicates can be added to the map. Trying to add an invalid object results in an IllegalArgumentException. Keys must pass the key predicate, values must pass the value predicate. It is important not to use the original map after invoking this method, as it is a backdoor for adding invalid objects.
K
- the key typeV
- the value typemap
- the map to predicate, must not be nullkeyPred
- the predicate for keys, null means no checkvaluePred
- the predicate for values, null means no checkjava.lang.NullPointerException
- if the SortedMap is nullpublic static <K,V> java.util.SortedMap<K,V> transformedSortedMap(java.util.SortedMap<K,V> map, Transformer<? super K,? extends K> keyTransformer, Transformer<? super V,? extends V> valueTransformer)
This method returns a new sorted map (decorating the specified map) that
will transform any new entries added to it.
Existing entries in the specified map will not be transformed.
If you want that behaviour, see TransformedSortedMap.transformedSortedMap(java.util.SortedMap<K, V>, org.apache.commons.collections4.Transformer<? super K, ? extends K>, org.apache.commons.collections4.Transformer<? super V, ? extends V>)
.
Each object is passed through the transformers as it is added to the Map. It is important not to use the original map after invoking this method, as it is a backdoor for adding untransformed objects.
If there are any elements already in the map being decorated, they are NOT transformed.
K
- the key typeV
- the value typemap
- the map to transform, must not be null, typically emptykeyTransformer
- the transformer for the map keys, null means no transformationvalueTransformer
- the transformer for the map values, null means no transformationjava.lang.NullPointerException
- if the SortedMap is nullpublic static <K,V> java.util.SortedMap<K,V> fixedSizeSortedMap(java.util.SortedMap<K,V> map)
Map.put(Object,Object)
method).K
- the key typeV
- the value typemap
- the map whose size to fix, must not be nulljava.lang.NullPointerException
- if the SortedMap is nullpublic static <K,V> java.util.SortedMap<K,V> lazySortedMap(java.util.SortedMap<K,V> map, Factory<? extends V> factory)
When the key passed to the returned map's Map.get(Object)
method is not present in the map, then the factory will be used
to create a new object and that object will become the value
associated with that key.
For instance:
Factory factory = new Factory() { public Object create() { return new Date(); } } SortedMap lazy = MapUtils.lazySortedMap(new TreeMap(), factory); Object obj = lazy.get("test");After the above code is executed,
obj
will contain
a new Date
instance. Furthermore, that Date
instance is the value for the "test"
key.K
- the key typeV
- the value typemap
- the map to make lazy, must not be nullfactory
- the factory for creating new objects, must not be nulljava.lang.NullPointerException
- if the SortedMap or Factory is nullpublic static <K,V> java.util.SortedMap<K,V> lazySortedMap(java.util.SortedMap<K,V> map, Transformer<? super K,? extends V> transformerFactory)
When the key passed to the returned map's Map.get(Object)
method is not present in the map, then the factory will be used
to create a new object and that object will become the value
associated with that key. The factory is a Transformer
that will be passed the key which it must transform into the value.
For instance:
Transformer factory = new Transformer() { public Object transform(Object mapKey) { return new File(mapKey); } } SortedMap lazy = MapUtils.lazySortedMap(new TreeMap(), factory); Object obj = lazy.get("C:/dev");After the above code is executed,
obj
will contain
a new File
instance for the C drive dev directory.
Furthermore, that File
instance is the value for the
"C:/dev"
key in the map.
If a lazy map is wrapped by a synchronized map, the result is a simple synchronized cache. When an object is not is the cache, the cache itself calls back to the factory Transformer to populate itself, all within the same synchronized block.
K
- the key typeV
- the value typemap
- the map to make lazy, must not be nulltransformerFactory
- the factory for creating new objects, must not be nulljava.lang.NullPointerException
- if the Map or Transformer is nullpublic static <K,V> void populateMap(java.util.Map<K,V> map, java.lang.Iterable<? extends V> elements, Transformer<V,K> keyTransformer)
Transformer
to transform the elements
into keys, using the unaltered element as the value in the Map
.K
- the key typeV
- the value typemap
- the Map
to populate.elements
- the Iterable
containing the input values for the map.keyTransformer
- the Transformer
used to transform the element into a key valuejava.lang.NullPointerException
- if the map, elements or transformer are nullpublic static <K,V,E> void populateMap(java.util.Map<K,V> map, java.lang.Iterable<? extends E> elements, Transformer<E,K> keyTransformer, Transformer<E,V> valueTransformer)
Transformer
s to transform the elements
into keys and values.K
- the key typeV
- the value typeE
- the type of object contained in the Iterable
map
- the Map
to populate.elements
- the Iterable
containing the input values for the map.keyTransformer
- the Transformer
used to transform the element into a key valuevalueTransformer
- the Transformer
used to transform the element into a valuejava.lang.NullPointerException
- if the map, elements or transformers are nullpublic static <K,V> void populateMap(MultiMap<K,V> map, java.lang.Iterable<? extends V> elements, Transformer<V,K> keyTransformer)
Transformer
to transform the elements
into keys, using the unaltered element as the value in the MultiMap
.K
- the key typeV
- the value typemap
- the MultiMap
to populate.elements
- the Iterable
to use as input values for the map.keyTransformer
- the Transformer
used to transform the element into a key valuejava.lang.NullPointerException
- if the map, elements or transformer are nullpublic static <K,V,E> void populateMap(MultiMap<K,V> map, java.lang.Iterable<? extends E> elements, Transformer<E,K> keyTransformer, Transformer<E,V> valueTransformer)
Transformer
s to transform the elements
into keys and values.K
- the key typeV
- the value typeE
- the type of object contained in the Iterable
map
- the MultiMap
to populate.elements
- the Iterable
containing the input values for the map.keyTransformer
- the Transformer
used to transform the element into a key valuevalueTransformer
- the Transformer
used to transform the element into a valuejava.lang.NullPointerException
- if the map, collection or transformers are nullpublic static <K,V> IterableMap<K,V> iterableMap(java.util.Map<K,V> map)
Map
as an IterableMap
.K
- the key typeV
- the value typemap
- to wrap if necessary.java.lang.NullPointerException
- if map is nullpublic static <K,V> IterableSortedMap<K,V> iterableSortedMap(java.util.SortedMap<K,V> sortedMap)
SortedMap
as an IterableSortedMap
.K
- the key typeV
- the value typesortedMap
- to wrap if necessaryIterableSortedMap
java.lang.NullPointerException
- if sortedMap is null"Copyright © 2010 - 2020 Adobe Systems Incorporated. All Rights Reserved"