Class KiwiMaps
Map instances-
Method Summary
Modifier and TypeMethodDescriptionstatic @Nullable StringgetAsString(Map<?, ?> map, Object key, @Nullable String defaultValue) Returns the value associated with the specified key as a String, or the provided default value if the key doesn't exist or the value is null.static @Nullable StringgetAsStringOrNull(Map<?, ?> map, Object key) Returns the value associated with the specified key as a String, or null if the key doesn't exist, or the value is null.static <T> @Nullable TgetConvertedValue(Map<?, ?> map, Object key, Function<Object, T> converter) Returns the value associated with the specified key, converted using the provided converter function.static <T> @Nullable TgetConvertedValueWithFallback(Map<?, ?> map, Object key, Function<Object, T> converter, BiFunction<Object, Exception, T> fallbackConverter) Returns the value associated with the specified key, converted using the provided converter function.static <K,V> V getOrThrow(Map<K, V> map, K key) Returns the value to which the specified key is mapped.static <K,V, E extends RuntimeException>
VgetOrThrow(Map<K, V> map, K key, Supplier<E> exceptionSupplier) Returns the value to which the specified key is mapped.static <K,V, E extends Exception>
VgetOrThrowChecked(Map<K, V> map, K key, Supplier<E> exceptionSupplier) Returns the value to which the specified key is mapped.static <T> @Nullable Collection<T>getTypedCollection(Map<?, ?> map, Object key, Class<T> valueType, @Nullable Collection<T> defaultValue) Returns the value associated with the specified key as a Collection of the specified type, or the provided default value if the key doesn't exist, or the value is null.static <T> Collection<T>getTypedCollectionOrEmpty(Map<?, ?> map, Object key, Class<T> valueType) Returns the value associated with the specified key as a Collection of the specified type, or an empty Collection if the key doesn't exist, or the value is null.static <T> @Nullable Collection<T>getTypedCollectionOrNull(Map<?, ?> map, Object key, Class<T> valueType) Returns the value associated with the specified key as a Collection of the specified type, or null if the key doesn't exist, or the value is null.static <T> @Nullable List<T>getTypedList(Map<?, ?> map, Object key, Class<T> valueType, @Nullable List<T> defaultValue) Returns the value associated with the specified key as a List of the specified type, or the provided default value if the key doesn't exist, or the value is null.static <T> List<T>getTypedListOrEmpty(Map<?, ?> map, Object key, Class<T> valueType) Returns the value associated with the specified key as a List of the specified type, or an empty List if the key doesn't exist, or the value is null.static <T> @Nullable List<T>getTypedListOrNull(Map<?, ?> map, Object key, Class<T> valueType) Returns the value associated with the specified key as a List of the specified type, or null if the key doesn't exist, or the value is null.static <K,V> @Nullable Map<K, V> getTypedMap(Map<?, ?> map, Object key, Class<K> keyType, Class<V> valueType, @Nullable Map<K, V> defaultValue) Returns the value associated with the specified key as a Map with keys and values of the specified types, or the provided default value if the key doesn't exist, or the value is null.static <K,V> Map<K, V> getTypedMapOrEmpty(Map<?, ?> map, Object key, Class<K> keyType, Class<V> valueType) Returns the value associated with the specified key as a Map with keys and values of the specified types, or an empty Map if the key doesn't exist, or the value is null.static <K,V> @Nullable Map<K, V> getTypedMapOrNull(Map<?, ?> map, Object key, Class<K> keyType, Class<V> valueType) Returns the value associated with the specified key as a Map with keys and values of the specified types, or null if the key doesn't exist, or the value is null.static <T> @Nullable Set<T>getTypedSet(Map<?, ?> map, Object key, Class<T> valueType, @Nullable Set<T> defaultValue) Returns the value associated with the specified key as a Set of the specified type, or the provided default value if the key doesn't exist, or the value is null.static <T> Set<T>getTypedSetOrEmpty(Map<?, ?> map, Object key, Class<T> valueType) Returns the value associated with the specified key as a Set of the specified type, or an empty Set if the key doesn't exist, or the value is null.static <T> @Nullable Set<T>getTypedSetOrNull(Map<?, ?> map, Object key, Class<T> valueType) Returns the value associated with the specified key as a Set of the specified type, or null if the key doesn't exist, or the value is null.static <T> @Nullable TgetTypedValue(Map<?, ?> map, Object key, Class<T> valueType, @Nullable T defaultValue) Returns the value associated with the specified key, cast to the specified type, or the provided default value if the key doesn't exist or the value is null.static <T> @Nullable TgetTypedValueOrNull(Map<?, ?> map, Object key, Class<T> valueType) Returns the value associated with the specified key cast, to the specified type, or null if the key doesn't exist, or the value is null.static <K,V> boolean isNotNullOrEmpty(Map<K, V> map) Checks whether the specified map is neither null nor empty.static <K,V> boolean isNullOrEmpty(Map<K, V> map) Checks whether the specified map is null or empty.static <K,V> boolean keyExistsWithNonNullValue(Map<K, V> map, K key) Checks whether the given map contains a key whose value is not null.static <K,V> boolean keyExistsWithNullValue(Map<K, V> map, K key) Returnstrueif and only if (1)mapis not null or empty, (2)mapcontains the givenkey, and (3) the value associated with the given key isnull.static <K,V> ConcurrentMap<K, V> newConcurrentHashMap(Object... items) Creates a mutable,ConcurrentHashMapinstance containing key/value pairs as parsed in pairs from theitemsargument.static <K,V> Map<K, V> newHashMap(Object... items) Creates a mutable,HashMapinstance containing key/value pairs as parsed in pairs from theitemsargument.static <K,V> Map<K, V> newLinkedHashMap(Object... items) Creates a mutable,LinkedHashMapinstance containing key/value pairs as parsed in pairs from theitemsargument.static <K extends Comparable<? super K>,V>
SortedMap<K,V> newTreeMap(Object... items) Creates a mutable,TreeMapinstance containing key/value pairs as parsed in pairs from theitemsargument.static <K,V> Map<K, V> newUnmodifiableConcurrentHashMap(Object... items) Creates an unmodifiable,ConcurrentHashMapinstance containing key/value pairs as parsed in pairs from theitemsargument.static <K,V> Map<K, V> newUnmodifiableHashMap(Object... items) Creates an unmodifiableHashMapinstance containing key/value pairs as parsed in pairs from theitemsargument.static <K,V> Map<K, V> newUnmodifiableLinkedHashMap(Object... items) Creates an unmodifiable,LinkedHashMapinstance containing key/value pairs as parsed in pairs from theitemsargument.static <K extends Comparable<? super K>,V>
SortedMap<K,V> newUnmodifiableTreeMap(Object... items) Creates an unmodifiable,TreeMapinstance containing key/value pairs as parsed in pairs from theitemsargument.
-
Method Details
-
isNullOrEmpty
Checks whether the specified map is null or empty.- Type Parameters:
K- the type of the keys in the mapV- the type of the values in the map- Parameters:
map- the map- Returns:
trueifmapis null or empty;falseotherwise
-
isNotNullOrEmpty
Checks whether the specified map is neither null nor empty.- Type Parameters:
K- the type of the keys in the mapV- the type of the values in the map- Parameters:
map- the map- Returns:
trueifmapis neither null nor empty;falseotherwise
-
newUnmodifiableHashMap
Creates an unmodifiableHashMapinstance containing key/value pairs as parsed in pairs from theitemsargument. The items argument contains keys and values in the form:key-1, value-1, key-2, value-2, ... , key-N, value-N
Note that trying to cast the returned value to a
HashMapwill result in aClassCastExceptionbecause the actual type returned byCollections.unmodifiableMap(Map)is a private class that implementsMap. Since it is generally it is best to use the interface typeMapthis should not present a problem in most use cases.Unlike the factory methods in
Map, null keys and values are permitted.- Type Parameters:
K- the type of the keys in the mapV- the type of the values in the map- Parameters:
items- the items containing keys and values, in pairs- Returns:
- a new unmodifiable HashMap with data from items
- See Also:
- Implementation Note:
- Wraps result of
newHashMap(Object...)withCollections.unmodifiableMap(Map)
-
newHashMap
Creates a mutable,HashMapinstance containing key/value pairs as parsed in pairs from theitemsargument. The items argument contains keys and values in the form:key-1, value-1, key-2, value-2, ... , key-N, value-N
- Type Parameters:
K- the type of the keys in the mapV- the type of the values in the map- Parameters:
items- the items containing keys and values, in pairs- Returns:
- a new HashMap with data from items
-
newUnmodifiableLinkedHashMap
Creates an unmodifiable,LinkedHashMapinstance containing key/value pairs as parsed in pairs from theitemsargument. The items argument contains keys and values in the form:key-1, value-1, key-2, value-2, ... , key-N, value-N
Note that trying to cast the returned value to a
LinkedHashMapwill result in aClassCastExceptionbecause the actual type returned byCollections.unmodifiableMap(Map)is a private class that implementsMap. Since it is generally it is best to use the interface typeMapthis should not present a problem in most use cases.Unlike the factory methods in
Map, null keys and values are permitted.- Type Parameters:
K- the type of the keys in the mapV- the type of the values in the map- Parameters:
items- the items containing keys and values, in pairs- Returns:
- a new unmodifiable LinkedHashMap with data from items
- See Also:
- Implementation Note:
- Wraps result of
newLinkedHashMap(Object...)withCollections.unmodifiableMap(Map)
-
newLinkedHashMap
Creates a mutable,LinkedHashMapinstance containing key/value pairs as parsed in pairs from theitemsargument. The items argument contains keys and values in the form:key-1, value-1, key-2, value-2, ... , key-N, value-N
- Type Parameters:
K- the type of the keys in the mapV- the type of the values in the map- Parameters:
items- the items containing keys and values, in pairs- Returns:
- a new LinkedHashMap with data from items
-
newUnmodifiableTreeMap
public static <K extends Comparable<? super K>,V> SortedMap<K,V> newUnmodifiableTreeMap(Object... items) Creates an unmodifiable,TreeMapinstance containing key/value pairs as parsed in pairs from theitemsargument. The items argument contains keys and values in the form:key-1, value-1, key-2, value-2, ... , key-N, value-N
Note that trying to cast the returned value to a
TreeMapwill result in aClassCastExceptionbecause the actual type returned byCollections.unmodifiableMap(Map)is a private class that implementsMap. Since it is generally it is best to use the interface typeMapthis should not present a problem in most use cases.Like the factory methods in
Map, null keys and values are not permitted.- Type Parameters:
K- the type of the keys in the mapV- the type of the values in the map- Parameters:
items- the items containing keys and values, in pairs- Returns:
- a new unmodifiable TreeMap with data from items
- See Also:
- Implementation Note:
- Wraps result of
newTreeMap(Object...)withCollections.unmodifiableMap(Map)
-
newTreeMap
Creates a mutable,TreeMapinstance containing key/value pairs as parsed in pairs from theitemsargument. The items argument contains keys and values in the form:key-1, value-1, key-2, value-2, ... , key-N, value-N
- Type Parameters:
K- the type of the keys in the mapV- the type of the values in the map- Parameters:
items- the items containing keys and values, in pairs- Returns:
- a new TreeMap with data from items
-
newUnmodifiableConcurrentHashMap
Creates an unmodifiable,ConcurrentHashMapinstance containing key/value pairs as parsed in pairs from theitemsargument. The items argument contains keys and values in the form:key-1, value-1, key-2, value-2, ... , key-N, value-N
Note that, unlike
newConcurrentHashMap(Object...), this method has a return type ofMap. This is becauseCollections.unmodifiableMap(Map)returnsMapand trying to cast the returned instance to aConcurrentMapresults in aClassCastException. Since it is generally it is best to use the interface typeMapthis should not present a problem in most use cases.Like the factory methods in
Map, null keys and values are not permitted.- Type Parameters:
K- the type of the keys in the mapV- the type of the values in the map- Parameters:
items- the items containing keys and values, in pairs- Returns:
- a new unmodifiable ConcurrentHashMap with data from items
- See Also:
- Implementation Note:
- Wraps result of
newConcurrentHashMap(Object...)withCollections.unmodifiableMap(Map)
-
newConcurrentHashMap
Creates a mutable,ConcurrentHashMapinstance containing key/value pairs as parsed in pairs from theitemsargument. The items argument contains keys and values in the form:key-1, value-1, key-2, value-2, ... , key-N, value-N
- Type Parameters:
K- the type of the keys in the mapV- the type of the values in the map- Parameters:
items- the items containing keys and values, in pairs- Returns:
- a new ConcurrentHashMap with data from items
-
keyExistsWithNullValue
Returnstrueif and only if (1)mapis not null or empty, (2)mapcontains the givenkey, and (3) the value associated with the given key isnull.- Type Parameters:
K- the type of the keys in the mapV- the type of the values in the map- Parameters:
map- the mapkey- the key to check- Returns:
trueif and only if (1)mapis not null or empty, (2)mapcontains the givenkey, and (3) the value associated with the given key isnull
-
keyExistsWithNonNullValue
Checks whether the given map contains a key whose value is not null.- Type Parameters:
K- the type of the keys in the mapV- the type of the values in the map- Parameters:
map- the mapkey- the key to check- Returns:
trueif and only if (1)mapis not null or empty, (2)mapcontains the givenkey, and (3) the value associated with the given key is notnull
-
getOrThrow
Returns the value to which the specified key is mapped.If the map is null or empty, or it does not contain the specified key, or the value associated with the key is null, a
NoSuchElementExceptionis thrown.- Type Parameters:
K- the type of the keys in the mapV- the type of the values in the map- Parameters:
map- the mapkey- the key whose associated value is to be returned- Returns:
- the value to which the specified key is mapped
- Throws:
IllegalArgumentException- if either argument is nullNoSuchElementException- if the map is null or empty, does not contain the specified key, or the value associated with the key is null
-
getOrThrow
public static <K,V, V getOrThrowE extends RuntimeException> (Map<K, V> map, K key, Supplier<E> exceptionSupplier) Returns the value to which the specified key is mapped.If the map is null or empty, or it does not contain the specified key, or the value associated with the key is null, the exception provided by the given Supplier is thrown.
- Type Parameters:
K- the type of the keys in the mapV- the type of the values in the mapE- the type of RuntimeException- Parameters:
map- the mapkey- the key whose associated value is to be returnedexceptionSupplier- supplies a RuntimeException when there is no value to return- Returns:
- the value to which the specified key is mapped
- Throws:
IllegalArgumentException- if any of the arguments is nullE- if the map is null or empty, does not contain the specified key, or the value associated with the key is null
-
getOrThrowChecked
public static <K,V, V getOrThrowCheckedE extends Exception> (Map<K, V> map, K key, Supplier<E> exceptionSupplier) throws EReturns the value to which the specified key is mapped.If the map is null or empty, or it does not contain the specified key, or the value associated with the key is null, the exception provided by the given Supplier is thrown.
- Type Parameters:
K- the type of the keys in the mapV- the type of the values in the mapE- the type of Exception- Parameters:
map- the mapkey- the key whose associated value is to be returnedexceptionSupplier- supplies an Exception when there is no value to return- Returns:
- the value to which the specified key is mapped
- Throws:
IllegalArgumentException- if any of the arguments is nullE- if the map is null or empty, does not contain the specified key, or the value associated with the key is null
-
getAsStringOrNull
Returns the value associated with the specified key as a String, or null if the key doesn't exist, or the value is null.If the (non-null) value is not a String, it is converted using
Object.toString().- Parameters:
map- the mapkey- the key whose associated value is to be returned as a String- Returns:
- the value associated with the key as a String, or null if the key doesn't exist or the value is null
-
getAsString
Returns the value associated with the specified key as a String, or the provided default value if the key doesn't exist or the value is null.If the (non-null) value is not a String, it is converted using
Object.toString().- Parameters:
map- the mapkey- the key whose associated value is to be returned as a StringdefaultValue- the default value to return if the key doesn't exist or the value is null- Returns:
- the value associated with the key as a String, or the default value if the key doesn't exist or the value is null
-
getTypedValueOrNull
Returns the value associated with the specified key cast, to the specified type, or null if the key doesn't exist, or the value is null.- Type Parameters:
T- the type of the value to be returned- Parameters:
map- the mapkey- the key whose associated value is to be returnedvalueType- the class representing the type to cast the value to- Returns:
- the value associated with the key, cast to the specified type, or null if the key doesn't exist or the value is null
- Throws:
MapTypeMismatchException- if the value cannot be cast to the specified type
-
getTypedValue
public static <T> @Nullable T getTypedValue(Map<?, ?> map, Object key, Class<T> valueType, @Nullable T defaultValue) Returns the value associated with the specified key, cast to the specified type, or the provided default value if the key doesn't exist or the value is null.- Type Parameters:
T- the type of the value to be returned- Parameters:
map- the mapkey- the key whose associated value is to be returnedvalueType- the class representing the type to cast the value todefaultValue- the default value to return if the key doesn't exist or the value is null- Returns:
- the value associated with the key, cast to the specified type, or the default value if the key doesn't exist or the value is null
- Throws:
IllegalArgumentException- if the map, key, or valueType is nullMapTypeMismatchException- if the value cannot be cast to the specified type
-
getTypedCollectionOrNull
public static <T> @Nullable Collection<T> getTypedCollectionOrNull(Map<?, ?> map, Object key, Class<T> valueType) Returns the value associated with the specified key as a Collection of the specified type, or null if the key doesn't exist, or the value is null.- Type Parameters:
T- the type of elements in the collection- Parameters:
map- the mapkey- the key whose associated value is to be returnedvalueType- the class representing the type of elements in the collection- Returns:
- the value associated with the key as a Collection of the specified type, or null if the key doesn't exist or the value is null
- Throws:
IllegalArgumentException- if the map, key, or valueType is nullMapTypeMismatchException- if the value cannot be cast to a Collection- Implementation Note:
- see implementation note in
getTypedCollection(Map, Object, Class, Collection)
-
getTypedCollectionOrEmpty
public static <T> Collection<T> getTypedCollectionOrEmpty(Map<?, ?> map, Object key, Class<T> valueType) Returns the value associated with the specified key as a Collection of the specified type, or an empty Collection if the key doesn't exist, or the value is null.- Type Parameters:
T- the type of elements in the collection- Parameters:
map- the mapkey- the key whose associated value is to be returnedvalueType- the class representing the type of elements in the collection- Returns:
- the value associated with the key as a Collection of the specified type, or an empty Collection if the key doesn't exist or the value is null
- Throws:
IllegalArgumentException- if the map, key, or valueType is nullMapTypeMismatchException- if the value cannot be cast to a Collection- Implementation Note:
- see implementation note in
getTypedCollection(Map, Object, Class, Collection)
-
getTypedCollection
public static <T> @Nullable Collection<T> getTypedCollection(Map<?, ?> map, Object key, Class<T> valueType, @Nullable Collection<T> defaultValue) Returns the value associated with the specified key as a Collection of the specified type, or the provided default value if the key doesn't exist, or the value is null.- Type Parameters:
T- the type of elements in the collection- Parameters:
map- the mapkey- the key whose associated value is to be returnedvalueType- the class representing the type of elements in the collectiondefaultValue- the default value to return if the key doesn't exist or the value is null- Returns:
- the value associated with the key as a Collection of the specified type, or the default value if the key doesn't exist or the value is null
- Throws:
IllegalArgumentException- if the map, key, or valueType is nullMapTypeMismatchException- if the value cannot be cast to a Collection- Implementation Note:
- Internally this performs an unchecked cast of the value to
Collection<T>. If the values in the collection are not actually of typeT, aClassCastExceptionwill be thrown when the collection is accessed, which may be downstream from the original call site and therefore be challenging to diagnose the root cause.
-
getTypedListOrNull
public static <T> @Nullable List<T> getTypedListOrNull(Map<?, ?> map, Object key, Class<T> valueType) Returns the value associated with the specified key as a List of the specified type, or null if the key doesn't exist, or the value is null.- Type Parameters:
T- the type of elements in the list- Parameters:
map- the mapkey- the key whose associated value is to be returnedvalueType- the class representing the type of elements in the list- Returns:
- the value associated with the key as a List of the specified type, or null if the key doesn't exist or the value is null
- Throws:
IllegalArgumentException- if the map, key, or valueType is nullMapTypeMismatchException- if the value cannot be cast to a List- Implementation Note:
- see implementation note in
getTypedList(Map, Object, Class, List)
-
getTypedListOrEmpty
Returns the value associated with the specified key as a List of the specified type, or an empty List if the key doesn't exist, or the value is null.- Type Parameters:
T- the type of elements in the list- Parameters:
map- the mapkey- the key whose associated value is to be returnedvalueType- the class representing the type of elements in the list- Returns:
- the value associated with the key as a List of the specified type, or an empty List if the key doesn't exist or the value is null
- Throws:
IllegalArgumentException- if the map, key, or valueType is nullMapTypeMismatchException- if the value cannot be cast to a List- Implementation Note:
- see implementation note in
getTypedList(Map, Object, Class, List)
-
getTypedList
public static <T> @Nullable List<T> getTypedList(Map<?, ?> map, Object key, Class<T> valueType, @Nullable List<T> defaultValue) Returns the value associated with the specified key as a List of the specified type, or the provided default value if the key doesn't exist, or the value is null.- Type Parameters:
T- the type of elements in the list- Parameters:
map- the mapkey- the key whose associated value is to be returnedvalueType- the class representing the type of elements in the listdefaultValue- the default value to return if the key doesn't exist or the value is null- Returns:
- the value associated with the key as a List of the specified type, or the default value if the key doesn't exist or the value is null
- Throws:
IllegalArgumentException- if the map, key, or valueType is nullMapTypeMismatchException- if the value cannot be cast to a List- Implementation Note:
- Internally this performs an unchecked cast of the value to
List<T>. If the values in the list are not actually of typeT, aClassCastExceptionwill be thrown when the list is accessed, which may be downstream from the original call site and therefore be challenging to diagnose the root cause.
-
getTypedSetOrNull
Returns the value associated with the specified key as a Set of the specified type, or null if the key doesn't exist, or the value is null.- Type Parameters:
T- the type of elements in the set- Parameters:
map- the mapkey- the key whose associated value is to be returnedvalueType- the class representing the type of elements in the set- Returns:
- the value associated with the key as a Set of the specified type, or null if the key doesn't exist or the value is null
- Throws:
IllegalArgumentException- if the map, key, or valueType is nullMapTypeMismatchException- if the value cannot be cast to a Set- Implementation Note:
- see implementation note in
getTypedSet(Map, Object, Class, Set)
-
getTypedSetOrEmpty
Returns the value associated with the specified key as a Set of the specified type, or an empty Set if the key doesn't exist, or the value is null.- Type Parameters:
T- the type of elements in the set- Parameters:
map- the mapkey- the key whose associated value is to be returnedvalueType- the class representing the type of elements in the set- Returns:
- the value associated with the key as a Set of the specified type, or an empty Set if the key doesn't exist or the value is null
- Throws:
IllegalArgumentException- if the map, key, or valueType is nullMapTypeMismatchException- if the value cannot be cast to a Set- Implementation Note:
- see implementation note in
getTypedSet(Map, Object, Class, Set)
-
getTypedSet
public static <T> @Nullable Set<T> getTypedSet(Map<?, ?> map, Object key, Class<T> valueType, @Nullable Set<T> defaultValue) Returns the value associated with the specified key as a Set of the specified type, or the provided default value if the key doesn't exist, or the value is null.- Type Parameters:
T- the type of elements in the set- Parameters:
map- the mapkey- the key whose associated value is to be returnedvalueType- the class representing the type of elements in the setdefaultValue- the default value to return if the key doesn't exist or the value is null- Returns:
- the value associated with the key as a Set of the specified type, or the default value if the key doesn't exist or the value is null
- Throws:
IllegalArgumentException- if the map, key, or valueType is nullMapTypeMismatchException- if the value cannot be cast to a Set- Implementation Note:
- Internally this performs an unchecked cast of the value to
Set<T>. If the values in the list are not actually of typeT, aClassCastExceptionwill be thrown when the set is accessed, which may be downstream from the original call site and therefore be challenging to diagnose the root cause.
-
getTypedMapOrNull
public static <K,V> @Nullable Map<K,V> getTypedMapOrNull(Map<?, ?> map, Object key, Class<K> keyType, Class<V> valueType) Returns the value associated with the specified key as a Map with keys and values of the specified types, or null if the key doesn't exist, or the value is null.- Type Parameters:
K- the type of keys in the returned mapV- the type of values in the returned map- Parameters:
map- the mapkey- the key whose associated value is to be returnedkeyType- the class representing the type of keys in the returned mapvalueType- the class representing the type of values in the returned map- Returns:
- the value associated with the key as a Map with the specified key and value types, or null if the key doesn't exist or the value is null
- Throws:
IllegalArgumentException- if the map, key, keyType, or valueType is nullMapTypeMismatchException- if the value cannot be cast to a Map- Implementation Note:
- see implementation note in
getTypedMap(Map, Object, Class, Class, Map)
-
getTypedMapOrEmpty
public static <K,V> Map<K,V> getTypedMapOrEmpty(Map<?, ?> map, Object key, Class<K> keyType, Class<V> valueType) Returns the value associated with the specified key as a Map with keys and values of the specified types, or an empty Map if the key doesn't exist, or the value is null.- Type Parameters:
K- the type of keys in the returned mapV- the type of values in the returned map- Parameters:
map- the mapkey- the key whose associated value is to be returnedkeyType- the class representing the type of keys in the returned mapvalueType- the class representing the type of values in the returned map- Returns:
- the value associated with the key as a Map with the specified key and value types, or an empty Map if the key doesn't exist or the value is null
- Throws:
IllegalArgumentException- if the map, key, keyType, or valueType is nullMapTypeMismatchException- if the value cannot be cast to a Map- Implementation Note:
- see implementation note in
getTypedMap(Map, Object, Class, Class, Map)
-
getTypedMap
public static <K,V> @Nullable Map<K,V> getTypedMap(Map<?, ?> map, Object key, Class<K> keyType, Class<V> valueType, @Nullable Map<K, V> defaultValue) Returns the value associated with the specified key as a Map with keys and values of the specified types, or the provided default value if the key doesn't exist, or the value is null.- Type Parameters:
K- the type of keys in the returned mapV- the type of values in the returned map- Parameters:
map- the mapkey- the key whose associated value is to be returnedkeyType- the class representing the type of keys in the returned mapvalueType- the class representing the type of values in the returned mapdefaultValue- the default value to return if the key doesn't exist or the value is null- Returns:
- the value associated with the key as a Map with the specified key and value types, or the default value if the key doesn't exist or the value is null
- Throws:
IllegalArgumentException- if the map, key, keyType, or valueType is nullMapTypeMismatchException- if the value cannot be cast to a Map- Implementation Note:
- Internally this performs an unchecked cast of the value to
Map<K, V>. If the keys and values in the map are not actually of typeKandV, respectively, aClassCastExceptionwill be thrown when the map is accessed, which may be downstream from the original call site and therefore be challenging to diagnose the root cause.
-
getConvertedValue
@Beta public static <T> @Nullable T getConvertedValue(Map<?, ?> map, Object key, Function<Object, T> converter) Returns the value associated with the specified key, converted using the provided converter function.This method is marked as
Betaand may change in future releases.- Type Parameters:
T- the type of the value to be returned- Parameters:
map- the mapkey- the key whose associated value is to be returnedconverter- a function that converts the value to the desired type- Returns:
- the value associated with the key, converted using the provided converter function
- Throws:
IllegalArgumentException- if the map, key, or converter is null
-
getConvertedValueWithFallback
@Beta public static <T> @Nullable T getConvertedValueWithFallback(Map<?, ?> map, Object key, Function<Object, T> converter, BiFunction<Object, Exception, T> fallbackConverter) Returns the value associated with the specified key, converted using the provided converter function. If the conversion fails with an exception, the fallback converter is used to provide an alternative value.This method is marked as
Betaand may change in future releases.- Type Parameters:
T- the type of the value to be returned- Parameters:
map- the mapkey- the key whose associated value is to be returnedconverter- a function that converts the value to the desired typefallbackConverter- a function that provides a fallback value when the primary converter throws an exception- Returns:
- the value associated with the key, converted using the provided converter function, or the result of the fallback converter if the primary conversion fails
- Throws:
IllegalArgumentException- if the map, key, converter, or fallbackConverter is null
-