Class MapExtras

java.lang.Object
dev.bannmann.labs.core.MapExtras

public final class MapExtras extends Object
  • 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 an Optional, calling code can use chained method calls instead of separate if blocks.

      Returns:
      a present Optional if the given map contains a non-null value for the given key.
    • 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 Optional of the value associated with the given key, 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-null value for the given key.