Class KiwiMaps
Map instances-
Method Summary
Modifier and TypeMethodDescriptionstatic <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 <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:
trueif map is 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:
trueif map is 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
-