Interface ICommonsMap<KEYTYPE,VALUETYPE>
- Type Parameters:
KEYTYPE- Map key typeVALUETYPE- Map value type
- All Superinterfaces:
ICloneable<ICommonsMap<KEYTYPE,,VALUETYPE>> Map<KEYTYPE,VALUETYPE>
- All Known Subinterfaces:
IAttributeContainer<KEYTYPE,,VALUETYPE> IAttributeContainerAny<KEYTYPE>,ICommonsNavigableMap<KEYTYPE,,VALUETYPE> ICommonsOrderedMap<KEYTYPE,,VALUETYPE> ICommonsSortedMap<KEYTYPE,,VALUETYPE> IStringMap
- All Known Implementing Classes:
AbstractSoftMap,AttributeContainer,AttributeContainerAny,AttributeContainerAnyConcurrent,AttributeContainerConcurrent,CommonsConcurrentHashMap,CommonsEnumMap,CommonsHashMap,CommonsLinkedHashMap,CommonsTreeMap,CommonsWeakHashMap,LoggingLRUMap,LRUMap,NonBlockingProperties,SoftHashMap,SoftLinkedHashMap,StringMap
public interface ICommonsMap<KEYTYPE,VALUETYPE>
extends Map<KEYTYPE,VALUETYPE>, ICloneable<ICommonsMap<KEYTYPE,VALUETYPE>>
A special
Map interface with extended functionality- Author:
- Philip Helger
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptiondefault voidAdd all provided values.default booleancontainsAnyEntry(Predicate<? super Map.Entry<KEYTYPE, VALUETYPE>> aFilter) Check if at least one entry matches the passed filter.default booleancontainsAnyKey(Predicate<? super KEYTYPE> aFilter) Check if at least one key matches the passed filter.default booleancontainsAnyValue(Predicate<? super VALUETYPE> aFilter) Check if at least one value matches the passed filter.default ICommonsSet<Map.Entry<KEYTYPE, VALUETYPE>> default ICommonsSet<KEYTYPE> default ICommonsSet<KEYTYPE> copyOfKeySet(Predicate<? super KEYTYPE> aFilter) Create a copy of all values matching the passed filter.default ICommonsList<VALUETYPE> default ICommonsList<VALUETYPE> copyOfValues(Predicate<? super VALUETYPE> aFilter) Create a copy of all values matching the passed filter.default <DSTTYPE> ICommonsList<DSTTYPE> copyOfValuesMapped(Function<? super VALUETYPE, ? extends DSTTYPE> aMapper) Create a copy of all values after applying the passed mapping function.default <DSTTYPE> ICommonsList<DSTTYPE> copyOfValuesMapped(Predicate<? super VALUETYPE> aFilter, Function<? super VALUETYPE, ? extends DSTTYPE> aMapper) Create a copy of all values matching the passed filter which are then converted using the provided function.default <K,V> ICommonsMap <K, V> Create a new empty map.findFirstEntry(Predicate<? super Map.Entry<KEYTYPE, VALUETYPE>> aFilter) Find the first entry that matches the passed filter.default KEYTYPEfindFirstKey(Predicate<? super Map.Entry<KEYTYPE, VALUETYPE>> aFilter) Find the first key that matches the passed filter.default VALUETYPEfindFirstValue(Predicate<? super Map.Entry<KEYTYPE, VALUETYPE>> aFilter) Find the first value that matches the passed filter.default voidforEach(BiPredicate<? super KEYTYPE, ? super VALUETYPE> aFilter, BiConsumer<? super KEYTYPE, ? super VALUETYPE> aConsumer) Invoke the provided consumer on each entry (pair of key and value) that matches the provided filter.default voidforEachKey(Consumer<? super KEYTYPE> aConsumer) Invoke the provided consumer on each key.default voidforEachKey(Predicate<? super KEYTYPE> aFilter, Consumer<? super KEYTYPE> aConsumer) Invoke the provided consumer on each key that matches the provided filter.default voidforEachValue(Consumer<? super VALUETYPE> aConsumer) Invoke the provided consumer on each value.default voidforEachValue(Predicate<? super VALUETYPE> aFilter, Consumer<? super VALUETYPE> aConsumer) Invoke the provided consumer on each value that matches the provided filter.Get the first element of this map ornull.getFirstEntry(Map.Entry<KEYTYPE, VALUETYPE> aDefault) Get the first element of this map or the provided default value.default KEYTYPEGet the first key of this map ornull.default KEYTYPEgetFirstKey(KEYTYPE aDefault) Get the first key of this map or the provided default value.default VALUETYPEGet the first value of this map ornull.default VALUETYPEgetFirstValue(VALUETYPE aDefault) Get the first value of this map or the provided default value.default ICommonsOrderedMap<KEYTYPE, VALUETYPE> getSortedByKey(Comparator<? super KEYTYPE> aKeyComparator) Get the map sorted by its keys.default ICommonsOrderedMap<KEYTYPE, VALUETYPE> getSortedByValue(Comparator<? super VALUETYPE> aValueComparator) Get the map sorted by its values.default ICommonsMap<VALUETYPE, KEYTYPE> Get a map where keys and values are exchanged.default booleandefault VALUETYPESpecial put overload that takes an entry and should simplify copying from other maps.default voidAdd all passed entries to this map.default voidputAll(Map<KEYTYPE, VALUETYPE> aMap, Predicate<? super Map.Entry<? extends KEYTYPE, ? extends VALUETYPE>> aFilter) Add all items from the passed map that match the filter to this map.default <ELEMENTTYPE>
voidputAllMapped(ELEMENTTYPE[] aElements, Function<? super ELEMENTTYPE, ? extends KEYTYPE> aKeyMapper, Function<? super ELEMENTTYPE, ? extends VALUETYPE> aValueMapper) Add all items from the passed array to this map using the provided key and value mapper.default <ELEMENTTYPE>
voidputAllMapped(Iterable<? extends ELEMENTTYPE> aElements, Function<? super ELEMENTTYPE, ? extends KEYTYPE> aKeyMapper, Function<? super ELEMENTTYPE, ? extends VALUETYPE> aValueMapper) Add all items from the passed iterable to this map using the provided key and value mapper.default <SRCKEYTYPE,SRCVALUETYPE>
voidputAllMapped(Map<? extends SRCKEYTYPE, ? extends SRCVALUETYPE> aMap, Function<? super SRCKEYTYPE, ? extends KEYTYPE> aKeyMapper, Function<? super SRCVALUETYPE, ? extends VALUETYPE> aValueMapper) Add all items from the passed map to this map using the provided key and value mapper.default voidPut the passed value into the map if the provided predicate is fulfilled.default voidputIfNotNull(KEYTYPE aKey, VALUETYPE aValue) Put the passed value into the map if it is notnull.default EChangeRemove all elements from this collection.default EChangedefault EChangeremoveIfKey(Predicate<? super KEYTYPE> aFilter) default EChangeremoveIfValue(Predicate<? super VALUETYPE> aFilter) default EChangeremoveObject(KEYTYPE aKey) Remove the object with the passed key from this map.default EChangeClear and add all provided values.Methods inherited from interface com.helger.commons.lang.ICloneable
getCloneMethods inherited from interface java.util.Map
clear, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, equals, forEach, get, getOrDefault, hashCode, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
-
Method Details
-
createInstance
Create a new empty map. Overwrite this if you don't want to useCommonsHashMap.- Type Parameters:
K- Map key typeV- Map value type- Returns:
- A new empty map. Never
null.
-
copyOfKeySet
- Returns:
- A new non-
nullset with all keys. - See Also:
-
copyOfKeySet
@Nonnull @ReturnsMutableCopy default ICommonsSet<KEYTYPE> copyOfKeySet(@Nullable Predicate<? super KEYTYPE> aFilter) Create a copy of all values matching the passed filter.- Parameters:
aFilter- The filter to be applied. May benull.- Returns:
- A new non-
nullset with all matching keys. - See Also:
-
copyOfValues
- Returns:
- A new non-
nullset with all values. - See Also:
-
copyOfValues
@Nonnull @ReturnsMutableCopy default ICommonsList<VALUETYPE> copyOfValues(@Nullable Predicate<? super VALUETYPE> aFilter) Create a copy of all values matching the passed filter.- Parameters:
aFilter- The filter to be applied. May benull.- Returns:
- A new list with all matching values. If no filter is provided the
returned value is identical as of
copyOfValues() - See Also:
-
copyOfValuesMapped
@Nonnull @ReturnsMutableCopy default <DSTTYPE> ICommonsList<DSTTYPE> copyOfValuesMapped(@Nonnull Function<? super VALUETYPE, ? extends DSTTYPE> aMapper) Create a copy of all values after applying the passed mapping function.- Type Parameters:
DSTTYPE- The destination type to be mapped to- Parameters:
aMapper- The mapping function to be applied. May not benull.- Returns:
- A new list with all mapped values.
- See Also:
-
copyOfValuesMapped
@Nonnull @ReturnsMutableCopy default <DSTTYPE> ICommonsList<DSTTYPE> copyOfValuesMapped(@Nullable Predicate<? super VALUETYPE> aFilter, @Nonnull Function<? super VALUETYPE, ? extends DSTTYPE> aMapper) Create a copy of all values matching the passed filter which are then converted using the provided function.- Type Parameters:
DSTTYPE- The destination type to be mapped to- Parameters:
aFilter- The filter to be applied. May benull.aMapper- The mapping function to be applied. May not benull.- Returns:
- A new list with all matching converted values. If no filter is
provided the returned value is identical as of
copyOfValuesMapped(Function) - See Also:
-
copyOfEntrySet
- Returns:
- A new non-
nullcopy of the entry set.
-
isNotEmpty
default boolean isNotEmpty()- Returns:
trueif the map is not empty,falseotherwise.
-
getFirstEntry
Get the first element of this map ornull.- Returns:
nullif the map is empty, the first element otherwise.- See Also:
-
getFirstEntry
@Nullable default Map.Entry<KEYTYPE,VALUETYPE> getFirstEntry(@Nullable Map.Entry<KEYTYPE, VALUETYPE> aDefault) Get the first element of this map or the provided default value.- Parameters:
aDefault- The default value to be returned if this map is empty. May benull.- Returns:
- The provided default value if the map is empty, the first entry otherwise.
- See Also:
-
getFirstKey
Get the first key of this map ornull.- Returns:
nullif the map is empty, the first key otherwise.- See Also:
-
getFirstKey
Get the first key of this map or the provided default value.- Parameters:
aDefault- The default value to be returned if this map is empty. May benull.- Returns:
- The provided default value if the map is empty, the first key otherwise.
- See Also:
-
getFirstValue
Get the first value of this map ornull.- Returns:
nullif the map is empty, the first value otherwise.- See Also:
-
getFirstValue
Get the first value of this map or the provided default value.- Parameters:
aDefault- The default value to be returned if this map is empty. May benull.- Returns:
- The provided default value if the map is empty, the first value otherwise.
- See Also:
-
findFirstEntry
@Nullable default Map.Entry<KEYTYPE,VALUETYPE> findFirstEntry(@Nullable Predicate<? super Map.Entry<KEYTYPE, VALUETYPE>> aFilter) Find the first entry that matches the passed filter.- Parameters:
aFilter- The filter to be applied. May benull.- Returns:
nullif no matching element was found. I no filter was provided, the result is the same asgetFirstEntry().
-
findFirstKey
@Nullable default KEYTYPE findFirstKey(@Nullable Predicate<? super Map.Entry<KEYTYPE, VALUETYPE>> aFilter) Find the first key that matches the passed filter.- Parameters:
aFilter- The filter to be applied. May benull.- Returns:
nullif no matching element was found. I no filter was provided, the result is the same asgetFirstKey().
-
findFirstValue
@Nullable default VALUETYPE findFirstValue(@Nullable Predicate<? super Map.Entry<KEYTYPE, VALUETYPE>> aFilter) Find the first value that matches the passed filter.- Parameters:
aFilter- The filter to be applied. May benull.- Returns:
nullif no matching element was found. I no filter was provided, the result is the same asgetFirstValue().
-
containsAnyEntry
Check if at least one entry matches the passed filter.- Parameters:
aFilter- The filter to be applied. May benull.- Returns:
trueif the map is not empty and contains at least one element matching the filter,falseotherwise. If no filter is provided the return value is identical toisNotEmpty().
-
containsAnyKey
Check if at least one key matches the passed filter.- Parameters:
aFilter- The filter to be applied. May benull.- Returns:
trueif the map is not empty and contains at least one key matching the filter,falseotherwise. If no filter is provided the return value is identical toisNotEmpty().
-
containsAnyValue
Check if at least one value matches the passed filter.- Parameters:
aFilter- The filter to be applied. May benull.- Returns:
trueif the map is not empty and contains at least one value matching the filter,falseotherwise. If no filter is provided the return value is identical toisNotEmpty().
-
forEachKey
Invoke the provided consumer on each key.- Parameters:
aConsumer- The consumer to be invoked. May not benull.
-
forEachValue
Invoke the provided consumer on each value.- Parameters:
aConsumer- The consumer to be invoked. May not benull.
-
forEach
default void forEach(@Nullable BiPredicate<? super KEYTYPE, ? super VALUETYPE> aFilter, @Nonnull BiConsumer<? super KEYTYPE, ? super VALUETYPE> aConsumer) Invoke the provided consumer on each entry (pair of key and value) that matches the provided filter.- Parameters:
aFilter- The filter to be applied. May benull.aConsumer- The consumer to be invoked. May not benull.- See Also:
-
forEachKey
default void forEachKey(@Nullable Predicate<? super KEYTYPE> aFilter, @Nonnull Consumer<? super KEYTYPE> aConsumer) Invoke the provided consumer on each key that matches the provided filter.- Parameters:
aFilter- The filter to be applied. May benull.aConsumer- The consumer to be invoked. May not benull.- See Also:
-
forEachValue
default void forEachValue(@Nullable Predicate<? super VALUETYPE> aFilter, @Nonnull Consumer<? super VALUETYPE> aConsumer) Invoke the provided consumer on each value that matches the provided filter.- Parameters:
aFilter- The filter to be applied. May benull.aConsumer- The consumer to be invoked. May not benull.- See Also:
-
getSortedByKey
@Nonnull @ReturnsMutableCopy default ICommonsOrderedMap<KEYTYPE,VALUETYPE> getSortedByKey(@Nonnull Comparator<? super KEYTYPE> aKeyComparator) Get the map sorted by its keys. The comparison order is defined by the passed comparator object.- Parameters:
aKeyComparator- The comparator to be used. May not benull.- Returns:
- the sorted map and never
null.
-
getSortedByValue
@Nonnull @ReturnsMutableCopy default ICommonsOrderedMap<KEYTYPE,VALUETYPE> getSortedByValue(@Nonnull Comparator<? super VALUETYPE> aValueComparator) Get the map sorted by its values. The comparison order is defined by the passed comparator object.- Parameters:
aValueComparator- The comparator to be used. May not benull.- Returns:
- the sorted map and never
null.
-
getSwappedKeyValues
Get a map where keys and values are exchanged.- Returns:
- The swapped hash map based on the type returned by
createInstance().
-
put
Special put overload that takes an entry and should simplify copying from other maps.- Parameters:
aEntry- Entry to be added. May not benull.- Returns:
- the return value of
Map.put(Object, Object). May benull. - Since:
- 9.0.0
-
putIf
default void putIf(@Nonnull KEYTYPE aKey, @Nullable VALUETYPE aValue, @Nonnull Predicate<? super VALUETYPE> aFilter) Put the passed value into the map if the provided predicate is fulfilled.- Parameters:
aKey- Key to use. May not benullfor certain map types.aValue- The value to be added. May benullin which case nothing happens.aFilter- The value predicate to be checked before insertion. May not benull.
-
putIfNotNull
Put the passed value into the map if it is notnull.- Parameters:
aKey- Key to use. May not benullfor certain map types.aValue- The value to be added. May benullin which case nothing happens.
-
putAll
Add all passed entries to this map.- Parameters:
aIterable- Source map entries. May benull.- Since:
- 8.5.5
-
putAll
default void putAll(@Nullable Map<KEYTYPE, VALUETYPE> aMap, @Nullable Predicate<? super Map.Entry<? extends KEYTYPE, ? extends VALUETYPE>> aFilter) Add all items from the passed map that match the filter to this map.- Parameters:
aMap- Source Map. May benull.aFilter- The filter to use. May benull.- Since:
- 8.5.5
-
putAllMapped
default <ELEMENTTYPE> void putAllMapped(@Nullable ELEMENTTYPE[] aElements, @Nonnull Function<? super ELEMENTTYPE, ? extends KEYTYPE> aKeyMapper, @Nonnull Function<? super ELEMENTTYPE, ? extends VALUETYPE> aValueMapper) Add all items from the passed array to this map using the provided key and value mapper.- Type Parameters:
ELEMENTTYPE- Array element type- Parameters:
aElements- Source collection. May benull.aKeyMapper- The key mapper. May not benull.aValueMapper- The value mapper. May not benull.- Since:
- 8.5.5
-
putAllMapped
default <ELEMENTTYPE> void putAllMapped(@Nullable Iterable<? extends ELEMENTTYPE> aElements, @Nonnull Function<? super ELEMENTTYPE, ? extends KEYTYPE> aKeyMapper, @Nonnull Function<? super ELEMENTTYPE, ? extends VALUETYPE> aValueMapper) Add all items from the passed iterable to this map using the provided key and value mapper.- Type Parameters:
ELEMENTTYPE- Collection element type- Parameters:
aElements- Source collection. May benull.aKeyMapper- The key mapper. May not benull.aValueMapper- The value mapper. May not benull.- Since:
- 8.5.5
-
putAllMapped
default <SRCKEYTYPE,SRCVALUETYPE> void putAllMapped(@Nullable Map<? extends SRCKEYTYPE, ? extends SRCVALUETYPE> aMap, @Nonnull Function<? super SRCKEYTYPE, ? extends KEYTYPE> aKeyMapper, @Nonnull Function<? super SRCVALUETYPE, ? extends VALUETYPE> aValueMapper) Add all items from the passed map to this map using the provided key and value mapper.- Type Parameters:
SRCKEYTYPE- Source map key typeSRCVALUETYPE- Source map value type- Parameters:
aMap- Source map. May benull.aKeyMapper- The key mapper. May not benull.aValueMapper- The value mapper. May not benull.- Since:
- 8.5.5
-
addAll
Add all provided values.- Parameters:
aValues- The values to be added. May benull.
-
setAll
Clear and add all provided values.- Parameters:
aValues- The values to be added. May benull.- Returns:
EChange
-
removeAll
Remove all elements from this collection. This is similar toMap.clear()but it returns a different value whether something was cleared or not.- Returns:
EChange.CHANGEDif the collection was not empty and something was removed,EChange.UNCHANGEDotherwise.- See Also:
-
removeObject
Remove the object with the passed key from this map.
Note: this method returnsEChange.UNCHANGEDeven if removal was successful if the value wasnull.- Parameters:
aKey- The key to be removed. May benull.- Returns:
EChange.CHANGEDif the removal was successful,EChange.UNCHANGEDif removal fails.- See Also:
-
removeIf
-
removeIfKey
-
removeIfValue
-
getAsUnmodifiable
- Returns:
- An unmodifiable version of this map. Never
null. - See Also:
-