Package dev.bannmann.labs.core
Class MapExtras
java.lang.Object
dev.bannmann.labs.core.MapExtras
-
Method Summary
Modifier and TypeMethodDescriptionGets the value to which the given key is mapped or throws an exception.Gets the value to which the given key is mapped, if existing.Removes a mapping for the given key, if one exists.
-
Method Details
-
tryGet
public static <K extends @Nullable Object,V extends @Nullable Object> Optional<@NonNull V> tryGet(Map<K, V> map, K key) Gets the value to which the given key is mapped, if existing.Unlike
Map.get(Object), with this method the compiler verifies the type of the key argument. Also, as it returns anOptional, calling code can use chained method calls instead of separateifblocks.- Returns:
- a present
Optionalif the given map contains a non-nullvalue for the givenkey.
-
tryRemove
public static <K extends @Nullable Object,V extends @Nullable Object> Optional<@NonNull V> tryRemove(Map<K, V> map, K key) Removes a mapping for the given key, if one exists.- Returns:
- a present
Optionalof the value associated with the givenkey, if one existed.
-
obtain
public static <K extends @Nullable Object,V extends @Nullable Object> @NonNull V obtain(Map<K, V> map, K key) Gets the value to which the given key is mapped or throws an exception.Unlike
Map.get(Object), with this method the compiler verifies type of the key argument.- Returns:
- the non-null value for the given key
- Throws:
IllegalArgumentException- if the given map does not contain a non-nullvalue for the givenkey.
-