Package me.croabeast.common.map
Class MapBuilder<K,V>
java.lang.Object
me.croabeast.common.map.MapBuilder<K,V>
- Type Parameters:
K- the type of keys in the mapV- the type of values in the map
A fluent builder for creating and transforming
LinkedHashMap instances.
The MapBuilder class wraps an internal LinkedHashMap and provides
methods for adding, removing, filtering, and mapping entries in a fluent style.
It also supports importing from existing maps or collections of map entries, and
producing immutable copies via build().
- Author:
- CroaBeast
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates an emptyMapBuilder.MapBuilder(Collection<Map.Entry<? extends K, ? extends V>> collection) Creates aMapBuilderinitialized with the entries from the given collection.MapBuilder(Map<? extends K, ? extends V> map) Creates aMapBuilderinitialized with the entries of the given map. -
Method Summary
Modifier and TypeMethodDescription<A> MapBuilder<A, V> applyByKey(Function<K, A> function) Transforms all keys using the given function, keeping values unchanged.<B> MapBuilder<K, B> applyByValue(Function<V, B> function) Transforms all values using the given function, keeping keys unchanged.build()Builds and returns a newLinkedHashMapcontaining the current entries.voidclear()Removes all mappings.booleancontainsKey(K key) Checks if the specified key is present.booleancontainsValue(V value) Checks if the specified value is present.entries()Returns aListof all entries, preserving insertion order.MapBuilder<K, V> filter(BiPredicate<K, V> predicate) Retains only those entries for which the given bi-predicate returnstrue.MapBuilder<K, V> filterByKey(Predicate<K> predicate) Retains only those entries whose keys match the given predicate.MapBuilder<K, V> filterByValue(Predicate<V> predicate) Retains only those entries whose values match the given predicate.findFirstKey(Predicate<K> predicate) Finds the first key matching the given predicate, ornullif none.findFirstKey(Predicate<K> predicate, K def) Finds the first key matching the given predicate, or returns the default if none.findFirstValue(Predicate<V> predicate) Finds the first value matching the given predicate, ornullif none.findFirstValue(Predicate<V> predicate, V def) Finds the first value matching the given predicate, or returns the default if none.voidforEach(BiConsumer<K, V> consumer) Performs the given action for each entry in this builder.Finds the first key associated with the given value, ornullif none.Finds the first key associated with the given value, or returns the default if none.Returns the value to which the specified key is mapped, ornullif none.Returns the value to which the specified key is mapped, or the default if none.booleanisEmpty()Checks whether this builder contains no entries.iterator()Returns an iterator over the entries in this builder.keys()Returns aListof all keys, preserving insertion order.<A,B> MapBuilder <A, B> Transforms both keys and values using the given functions.static <K,V> Map <K, V> mapOf(Collection<K> keys, Collection<V> values) Creates aMapby pairing elements from two collections: one of keys and one of values.static <K,V> Map <K, V> mapOf(K[] keys, V... values) Creates aMapby pairing elements from two arrays: one of keys and one of values.MapBuilder<K, V> Adds the given entry to this builder.MapBuilder<K, V> Associates the specified value with the specified key in this builder.MapBuilder<K, V> Copies all entries from the given map into this builder.MapBuilder<K, V> putIfAbsent(Map.Entry<? extends K, ? extends V> entry) Adds the given entry only if its key is not already present.MapBuilder<K, V> putIfAbsent(K key, V value) Associates the specified value with the specified key if the key is not already present.MapBuilder<K, V> Removes the mapping for the specified key if present.MapBuilder<K, V> Removes the entry for the specified key only if it is currently mapped to the given value.MapBuilder<K, V> removeAllByKey(K key, int counting) Removes up tocountingentries matching the given key.MapBuilder<K, V> removeAllByValue(V value, int counting) Removes up tocountingentries matching the given value.static <K,V> Map <K, V> singleton(K key, V value) Creates a singleton map containing the given key-value pair.intsize()Returns the number of entries.toString()Returns a string representation of the internal map.values()Returns aListof all values, preserving insertion order.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
MapBuilder
public MapBuilder()Creates an emptyMapBuilder. -
MapBuilder
Creates aMapBuilderinitialized with the entries of the given map.- Parameters:
map- an existing map whose entries will be copied; may benull
-
MapBuilder
Creates aMapBuilderinitialized with the entries from the given collection.- Parameters:
collection- a collection of map entries to copy; may benull
-
-
Method Details
-
put
Associates the specified value with the specified key in this builder.- Parameters:
key- the key to addvalue- the value to associate- Returns:
- this builder (for chaining)
-
put
Adds the given entry to this builder.- Parameters:
entry- the entry whose key and value to add- Returns:
- this builder (for chaining)
-
putIfAbsent
Associates the specified value with the specified key if the key is not already present.- Parameters:
key- the key to addvalue- the value to associate if absent- Returns:
- this builder (for chaining)
-
putIfAbsent
Adds the given entry only if its key is not already present.- Parameters:
entry- the entry whose key and value to add if absent- Returns:
- this builder (for chaining)
-
putAll
Copies all entries from the given map into this builder.- Parameters:
map- the map whose entries to add (must not benull)- Returns:
- this builder (for chaining)
-
remove
Removes the mapping for the specified key if present.- Parameters:
key- the key to remove- Returns:
- this builder (for chaining)
-
remove
Removes the entry for the specified key only if it is currently mapped to the given value.- Parameters:
key- the key whose mapping to removevalue- the value expected to be associated with the key- Returns:
- this builder (for chaining)
-
removeAllByKey
Removes up tocountingentries matching the given key. Ifcountingis negative, all matching entries are removed.- Parameters:
key- the key to removecounting- the maximum number of entries to remove, or negative for unlimited- Returns:
- this builder (for chaining)
-
removeAllByValue
Removes up tocountingentries matching the given value. Ifcountingis negative, all matching entries are removed.- Parameters:
value- the value to removecounting- the maximum number of entries to remove, or negative for unlimited- Returns:
- this builder (for chaining)
-
filterByKey
Retains only those entries whose keys match the given predicate.- Parameters:
predicate- a predicate to test keys- Returns:
- this builder (for chaining)
-
filterByValue
Retains only those entries whose values match the given predicate.- Parameters:
predicate- a predicate to test values- Returns:
- this builder (for chaining)
-
filter
Retains only those entries for which the given bi-predicate returnstrue.- Parameters:
predicate- a bi-predicate to test key and value pairs- Returns:
- this builder (for chaining)
-
applyByKey
Transforms all keys using the given function, keeping values unchanged.- Type Parameters:
A- the new key type- Parameters:
function- a function to apply to each key- Returns:
- a new
MapBuilderwith transformed keys
-
applyByValue
Transforms all values using the given function, keeping keys unchanged.- Type Parameters:
B- the new value type- Parameters:
function- a function to apply to each value- Returns:
- a new
MapBuilderwith transformed values
-
map
Transforms both keys and values using the given functions.- Type Parameters:
A- the new key typeB- the new value type- Parameters:
keyFunction- a function to apply to each keyvalueFunction- a function to apply to each value- Returns:
- a new
MapBuilderwith transformed entries
-
containsKey
Checks if the specified key is present.- Parameters:
key- the key to check- Returns:
trueif present,falseotherwise
-
containsValue
Checks if the specified value is present.- Parameters:
value- the value to check- Returns:
trueif present,falseotherwise
-
get
Returns the value to which the specified key is mapped, or the default if none.- Parameters:
key- the keydef- the default value to return if key is absent- Returns:
- the mapped or default value
-
get
Returns the value to which the specified key is mapped, ornullif none.- Parameters:
key- the key- Returns:
- the mapped value, or
null
-
clear
public void clear()Removes all mappings. -
size
public int size()Returns the number of entries.- Returns:
- the map size
-
fromValue
Finds the first key associated with the given value, or returns the default if none.- Parameters:
value- the value to search fordef- the default key if not found- Returns:
- the found key or
def
-
fromValue
Finds the first key associated with the given value, ornullif none.- Parameters:
value- the value to search for- Returns:
- the found key or
null
-
findFirstKey
Finds the first key matching the given predicate, or returns the default if none.- Parameters:
predicate- a predicate to test keysdef- the default key if not found- Returns:
- the found key or
def
-
findFirstKey
Finds the first key matching the given predicate, ornullif none.- Parameters:
predicate- a predicate to test keys- Returns:
- the found key or
null
-
findFirstValue
Finds the first value matching the given predicate, or returns the default if none.- Parameters:
predicate- a predicate to test valuesdef- the default value if not found- Returns:
- the found value or
def
-
findFirstValue
Finds the first value matching the given predicate, ornullif none.- Parameters:
predicate- a predicate to test values- Returns:
- the found value or
null
-
keys
Returns aListof all keys, preserving insertion order.- Returns:
- a list of keys
-
values
Returns aListof all values, preserving insertion order.- Returns:
- a list of values
-
entries
Returns aListof all entries, preserving insertion order.- Returns:
- a list of map entries
-
iterator
Returns an iterator over the entries in this builder. -
forEach
Performs the given action for each entry in this builder.- Parameters:
consumer- a bi-consumer accepting key and value
-
build
Builds and returns a newLinkedHashMapcontaining the current entries.- Returns:
- an immutable copy of the internal map
-
isEmpty
public boolean isEmpty()Checks whether this builder contains no entries.- Returns:
trueif empty,falseotherwise
-
toString
Returns a string representation of the internal map. -
singleton
Creates a singleton map containing the given key-value pair.- Type Parameters:
K- the key typeV- the value type- Parameters:
key- the single keyvalue- the single value- Returns:
- a map with exactly one entry
-
mapOf
Creates aMapby pairing elements from two collections: one of keys and one of values.Each element in the
keyscollection is associated with the element at the same index in thevaluescollection. The iteration order of the returned map follows the iteration order of thekeyscollection.- Type Parameters:
K- the type of the map keysV- the type of the map values- Parameters:
keys- the collection of keys (must not benull)values- the collection of values (must not benull), must have the same size askeys- Returns:
- a new
LinkedHashMapcontaining each key mapped to its corresponding value - Throws:
NullPointerException- if eitherkeysorvaluesisnullIndexOutOfBoundsException- ifkeys.size() != values.size()
-
mapOf
Creates aMapby pairing elements from two arrays: one of keys and one of values.Each element in the
keysarray is associated with the element at the same index in thevaluesarray. The iteration order of the returned map follows the order of thekeysarray.- Type Parameters:
K- the type of the map keysV- the type of the map values- Parameters:
keys- the array of keys (must not benull)values- the array of values (must not benull), must have the same length askeys- Returns:
- a new
LinkedHashMapcontaining each key mapped to its corresponding value - Throws:
NullPointerException- if eitherkeysorvaluesisnullIndexOutOfBoundsException- ifkeys.length != values.length- See Also:
-