Class OptionalMap<K,V>
java.lang.Object
io.github.bakedlibs.dough.collections.OptionalMap<K,V>
- Type Parameters:
K- The type of keys for this MapV- 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 Summary
ConstructorsConstructorDescriptionOptionalMap(Supplier<? extends Map<K, V>> constructor) An OptionalMap allows you to directly obtain Optionals from a Map. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()computeIfAbsent(K key, Function<? super K, ? extends V> function) computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> function) booleancontainsKey(K key) get(Object)should be preferred.booleancontainsValue(V value) entrySet()booleanvoidforEach(BiConsumer<? super K, ? super V> consumer) This method gives you anOptionaldescribing the value mapped to the given key.getOrDefault(K key, V defaultValue) inthashCode()voidvoidbooleanisEmpty()This method returns whether our Map is empty.iterator()keySet()voidputIfAbsent(K key, V value) intsize()This method returns the size of this Map.stream()values()Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
OptionalMap
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
This method gives you anOptionaldescribing the value mapped to the given key. If no mapping was found thenOptional.empty()will be returned.- Parameters:
key- The key to our Value- Returns:
- An Optional describing the result, empty if no mapping was found
-
containsKey
get(Object)should be preferred.- Parameters:
key- The key to our Value- Returns:
- Whether the key is present in our Map
-
containsValue
-
ifPresent
-
ifAbsent
-
put
-
remove
-
putAll
-
clear
public void clear() -
keySet
-
values
-
entrySet
-
equals
-
hashCode
public int hashCode() -
getOrDefault
-
forEach
-
putIfAbsent
-
computeIfAbsent
-
computeIfPresent
-
compute
-
merge
-
iterator
-
stream
- Specified by:
streamin interfaceStreamable<K>
-
getInternalMap
-