java.lang.Object | +--org.apache.commons.collections.MapUtils
static Map | An empty unmodifiable map. |
static SortedMap | An empty unmodifiable sorted map. |
MapUtils() MapUtils should not normally be instantiated. |
static void | debugPrint(final PrintStream out, final Object label, final Map map) Prints the given map with nice line breaks. |
static Map | fixedSizeMap(Map map) Returns a fixed-sized map backed by the given map. |
static SortedMap | fixedSizeSortedMap(SortedMap map) Returns a fixed-sized sorted map backed by the given sorted map. |
static Boolean | getBoolean(final Map map, final Object key) Gets a Boolean from a Map in a null-safe manner. |
static Boolean | getBoolean(Map map, Object key, 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 boolean | getBooleanValue(final Map map, final Object key) Gets a boolean from a Map in a null-safe manner. |
static boolean | getBooleanValue(final Map map, final Object key, boolean defaultValue) Gets a boolean from a Map in a null-safe manner, using the default value if the the conversion fails. |
static Byte | getByte(final Map map, final Object key) Gets a Byte from a Map in a null-safe manner. |
static Byte | getByte(Map map, Object key, 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 byte | getByteValue(final Map map, final Object key) Gets a byte from a Map in a null-safe manner. |
static byte | getByteValue(final Map map, final Object key, byte defaultValue) Gets a byte from a Map in a null-safe manner, using the default value if the the conversion fails. |
static Double | getDouble(final Map map, final Object key) Gets a Double from a Map in a null-safe manner. |
static Double | getDouble(Map map, Object key, 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 double | getDoubleValue(final Map map, final Object key) Gets a double from a Map in a null-safe manner. |
static double | getDoubleValue(final Map map, final Object key, double defaultValue) Gets a double from a Map in a null-safe manner, using the default value if the the conversion fails. |
static Float | getFloat(final Map map, final Object key) Gets a Float from a Map in a null-safe manner. |
static Float | getFloat(Map map, Object key, 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 float | getFloatValue(final Map map, final Object key) Gets a float from a Map in a null-safe manner. |
static float | getFloatValue(final Map map, final Object key, float defaultValue) Gets a float from a Map in a null-safe manner, using the default value if the the conversion fails. |
static Integer | getInteger(final Map map, final Object key) Gets a Integer from a Map in a null-safe manner. |
static Integer | getInteger(Map map, Object key, 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 int | getIntValue(final Map map, final Object key) Gets an int from a Map in a null-safe manner. |
static int | getIntValue(final Map map, final Object key, int defaultValue) Gets an int from a Map in a null-safe manner, using the default value if the the conversion fails. |
static Long | getLong(final Map map, final Object key) Gets a Long from a Map in a null-safe manner. |
static Long | getLong(Map map, Object key, 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 long | getLongValue(final Map map, final Object key) Gets a long from a Map in a null-safe manner. |
static long | getLongValue(final Map map, final Object key, long defaultValue) Gets a long from a Map in a null-safe manner, using the default value if the the conversion fails. |
static Map | getMap(final Map map, final Object key) Gets a Map from a Map in a null-safe manner. |
static Map | getMap(Map map, Object key, 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 Number | getNumber(final Map map, final Object key) Gets a Number from a Map in a null-safe manner. |
static Number | getNumber(Map map, Object key, 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 Object | getObject(final Map map, final Object key) Gets from a Map in a null-safe manner. |
static Object | getObject(Map map, Object key, Object defaultValue) Looks up the given key in the given map, converting null into the given default value. |
static Short | getShort(final Map map, final Object key) Gets a Short from a Map in a null-safe manner. |
static Short | getShort(Map map, Object key, 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 short | getShortValue(final Map map, final Object key) Gets a short from a Map in a null-safe manner. |
static short | getShortValue(final Map map, final Object key, short defaultValue) Gets a short from a Map in a null-safe manner, using the default value if the the conversion fails. |
static String | getString(final Map map, final Object key) Gets a String from a Map in a null-safe manner. |
static String | getString(Map map, Object key, 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 Map | invertMap(Map map) Inverts the supplied map returning a new HashMap such that the keys of the input are swapped with the values. |
static Map | lazyMap(Map map, Factory factory) Returns a "lazy" map whose values will be created on demand. |
static Map | lazyMap(Map map, Transformer transformerFactory) Returns a "lazy" map whose values will be created on demand. |
static SortedMap | lazySortedMap(SortedMap map, Factory factory) Returns a "lazy" sorted map whose values will be created on demand. |
static SortedMap | lazySortedMap(SortedMap map, Transformer transformerFactory) Returns a "lazy" sorted map whose values will be created on demand. |
static void | logInfo(final Exception ex) Logs the given exception to System.out. |
static Map | orderedMap(Map map) Returns a map that maintains the order of keys that are added backed by the given map. |
static Map | predicatedMap(Map map, Predicate keyPred, Predicate valuePred) Returns a predicated map backed by the given map. |
static SortedMap | predicatedSortedMap(SortedMap map, Predicate keyPred, Predicate valuePred) Returns a predicated sorted map backed by the given map. |
static void | safeAddToMap(Map map, Object key, Object value) Nice method for adding data to a map in such a way as to not get NPE's. |
static Map | synchronizedMap(Map map) Returns a synchronized map backed by the given map. |
static Map | synchronizedSortedMap(SortedMap map) Returns a synchronized sorted map backed by the given sorted map. |
static Map | toMap(final ResourceBundle resourceBundle) Creates a new HashMap using data copied from a ResourceBundle. |
static Properties | toProperties(final Map map) Gets a new Properties object initialised with the values from a Map. |
static Map | transformedMap(Map map, Transformer keyTransformer, Transformer valueTransformer) Returns a transformed map backed by the given map. |
static SortedMap | transformedSortedMap(SortedMap map, Transformer keyTransformer, Transformer valueTransformer) Returns a transformed sorted map backed by the given map. |
static Map | typedMap(Map map, Class keyType, Class valueType) Returns a typed map backed by the given map. |
static SortedMap | typedSortedMap(SortedMap map, Class keyType, Class valueType) Returns a typed sorted map backed by the given map. |
static Map | unmodifiableMap(Map map) Returns an unmodifiable map backed by the given map. |
static Map | unmodifiableSortedMap(SortedMap map) Returns an unmodifiable sorted map backed by the given sorted map. |
static void | verbosePrint(final PrintStream out, final Object label, final Map map) Prints the given map with nice line breaks. |
public static final Map EMPTY_MAP
public static final SortedMap EMPTY_SORTED_MAP
public MapUtils()
MapUtils
should not normally be instantiated.
public static void debugPrint(final PrintStream out, final Object label, final Map map)
- if the stream is nullpublic static Map fixedSizeMap(Map map)
- if the Map is nullpublic static SortedMap fixedSizeSortedMap(SortedMap map)
- if the SortedMap is nullpublic static Boolean getBoolean(final Map map, final Object key)
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.
public static Boolean getBoolean(Map map, Object key, Boolean defaultValue)
public static boolean getBooleanValue(final Map map, final Object key, boolean defaultValue)
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.
public static boolean getBooleanValue(final Map map, final Object key)
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.
public static Byte getByte(final Map map, final Object key)
public static Byte getByte(Map map, Object key, Byte defaultValue)
public static byte getByteValue(final Map map, final Object key, byte defaultValue)
public static byte getByteValue(final Map map, final Object key)
public static Double getDouble(final Map map, final Object key)
public static Double getDouble(Map map, Object key, Double defaultValue)
public static double getDoubleValue(final Map map, final Object key, double defaultValue)
public static double getDoubleValue(final Map map, final Object key)
public static Float getFloat(final Map map, final Object key)
public static Float getFloat(Map map, Object key, Float defaultValue)
public static float getFloatValue(final Map map, final Object key, float defaultValue)
public static float getFloatValue(final Map map, final Object key)
public static Integer getInteger(final Map map, final Object key)
public static Integer getInteger(Map map, Object key, Integer defaultValue)
public static int getIntValue(final Map map, final Object key, int defaultValue)
public static int getIntValue(final Map map, final Object key)
public static Long getLong(final Map map, final Object key)
public static Long getLong(Map map, Object key, Long defaultValue)
public static long getLongValue(final Map map, final Object key, long defaultValue)
public static long getLongValue(final Map map, final Object key)
public static Map getMap(final Map map, final Object key)
null
is returned.
public static Map getMap(Map map, Object key, Map defaultValue)
public static Number getNumber(final Map map, final Object key)
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.
public static Number getNumber(Map map, Object key, Number defaultValue)
public static Object getObject(final Map map, final Object key)
public static Object getObject(Map map, Object key, Object defaultValue)
public static Short getShort(final Map map, final Object key)
public static Short getShort(Map map, Object key, Short defaultValue)
public static short getShortValue(final Map map, final Object key, short defaultValue)
public static short getShortValue(final Map map, final Object key)
public static String getString(final Map map, final Object key)
toString
.
public static String getString(Map map, Object key, String defaultValue)
public static Map invertMap(Map map)
- if the map is nullDoubleOrderedMap
public static Map lazyMap(Map map, Factory factory)
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.
- if the Map or Factory is nullpublic static Map lazyMap(Map map, Transformer transformerFactory)
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.
- if the Map or Transformer is nullpublic static SortedMap lazySortedMap(SortedMap map, Factory factory)
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.
- if the SortedMap or Factory is nullpublic static SortedMap lazySortedMap(SortedMap map, Transformer transformerFactory)
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.
- if the Map or Transformer is nullprotected static void logInfo(final Exception ex)
System.out
.
This method exists as Jakarta Collections does not depend on logging.
public static Map orderedMap(Map map)
- if the Map is nullpublic static Map predicatedMap(Map map, Predicate keyPred, Predicate valuePred)
- if the Map is nullpublic static SortedMap predicatedSortedMap(SortedMap map, Predicate keyPred, Predicate valuePred)
- if the SortedMap is nullpublic static void safeAddToMap(Map map, Object key, Object value)
- if the map is nullpublic static Map synchronizedMap(Map map)
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 java.util.Collections Collections.
- if the map is nullpublic static Map synchronizedSortedMap(SortedMap map)
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 java.util.Collections Collections.
- if the map is nullpublic static Map toMap(final ResourceBundle resourceBundle)
- if the bundle is nullpublic static Properties toProperties(final Map map)
public static Map transformedMap(Map map, Transformer keyTransformer, Transformer valueTransformer)
- if the Map is nullpublic static SortedMap transformedSortedMap(SortedMap map, Transformer keyTransformer, Transformer valueTransformer)
- if the SortedMap is nullpublic static Map typedMap(Map map, Class keyType, Class valueType)
- if the Map or Class is nullpublic static SortedMap typedSortedMap(SortedMap map, Class keyType, Class valueType)
public static Map unmodifiableMap(Map map)
- if the map is nullpublic static Map unmodifiableSortedMap(SortedMap map)
- if the map is nullpublic static void verbosePrint(final PrintStream out, final Object label, final Map map)
- if the stream is null
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)typedMap(Map, Class, Class)typedSortedMap(SortedMap, Class, Class)