Class OptionalMap<K,V>

java.lang.Object
io.github.bakedlibs.dough.collections.OptionalMap<K,V>
Type Parameters:
K - The type of keys for this Map
V - The Type of values for this Maps
All Implemented Interfaces:
Streamable<Map.Entry<K,V>>, Iterable<Map.Entry<K,V>>
Direct Known Subclasses:
KeyMap

public class OptionalMap<K,V> extends Object implements Iterable<Map.Entry<K,V>>, Streamable<Map.Entry<K,V>>
This Class functions similar to Map but returns an Optional when calling get(Object). This way you can save yourself some Map.containsKey(Object) calls and also benefit from the methods that Optional implements.
Author:
TheBusyBiscuit
  • Constructor Details

    • OptionalMap

      public OptionalMap(@Nonnull Supplier<? extends Map<K,V>> constructor)
      An OptionalMap allows you to directly obtain Optionals from a Map. The Map implementation is up to you, you can pass in the constructor of any class that implements the Map interface. OptionalMap>String, String< map = new OptionalMap><(HashMap::new);
      Parameters:
      constructor - A Constructor reference to an existing Map implementation
  • Method Details

    • size

      public int size()
      This method returns the size of this Map.
      Returns:
      The size of our Map
    • isEmpty

      public boolean isEmpty()
      This method returns whether our Map is empty.
      Returns:
      Whether out Map is empty
    • get

      public Optional<V> get(K key)
      This method gives you an Optional describing the value mapped to the given key. If no mapping was found then Optional.empty() will be returned.
      Parameters:
      key - The key to our Value
      Returns:
      An Optional describing the result, empty if no mapping was found
    • containsKey

      public boolean containsKey(K key)
      get(Object) should be preferred.
      Parameters:
      key - The key to our Value
      Returns:
      Whether the key is present in our Map
    • containsValue

      public boolean containsValue(V value)
    • ifPresent

      public void ifPresent(K key, Consumer<? super V> consumer)
    • ifAbsent

      public void ifAbsent(K key, Consumer<Void> consumer)
    • put

      public Optional<V> put(K key, V value)
    • remove

      public Optional<V> remove(K key)
    • putAll

      public void putAll(Map<? extends K,? extends V> map)
    • clear

      public void clear()
    • keySet

      public Set<K> keySet()
    • values

      public Collection<V> values()
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • getOrDefault

      public V getOrDefault(K key, V defaultValue)
    • forEach

      public void forEach(BiConsumer<? super K,? super V> consumer)
    • putIfAbsent

      public V putIfAbsent(K key, V value)
    • computeIfAbsent

      public V computeIfAbsent(K key, Function<? super K,? extends V> function)
    • computeIfPresent

      public V computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> function)
    • compute

      public V compute(K key, BiFunction<? super K,? super V,? extends V> function)
    • merge

      public V merge(K key, V value, BiFunction<? super V,? super V,? extends V> function)
    • iterator

      public Iterator<Map.Entry<K,V>> iterator()
      Specified by:
      iterator in interface Iterable<K>
    • stream

      public Stream<Map.Entry<K,V>> stream()
      Specified by:
      stream in interface Streamable<K>
    • getInternalMap

      @Nonnull public Map<K,V> getInternalMap()