Class MapBuilder<K,V>

java.lang.Object
me.croabeast.common.map.MapBuilder<K,V>
Type Parameters:
K - the type of keys in the map
V - the type of values in the map
All Implemented Interfaces:
Iterable<Map.Entry<K,V>>

public class MapBuilder<K,V> extends Object implements Iterable<Map.Entry<K,V>>
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

    Constructors
    Constructor
    Description
    Creates an empty MapBuilder.
    MapBuilder(Collection<Map.Entry<? extends K,? extends V>> collection)
    Creates a MapBuilder initialized with the entries from the given collection.
    MapBuilder(Map<? extends K,? extends V> map)
    Creates a MapBuilder initialized with the entries of the given map.
  • Method Summary

    Modifier and Type
    Method
    Description
    <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.
    Builds and returns a new LinkedHashMap containing the current entries.
    void
    Removes all mappings.
    boolean
    Checks if the specified key is present.
    boolean
    Checks if the specified value is present.
    Returns a List of all entries, preserving insertion order.
    filter(BiPredicate<K,V> predicate)
    Retains only those entries for which the given bi-predicate returns true.
    filterByKey(Predicate<K> predicate)
    Retains only those entries whose keys match the given predicate.
    Retains only those entries whose values match the given predicate.
    findFirstKey(Predicate<K> predicate)
    Finds the first key matching the given predicate, or null if none.
    findFirstKey(Predicate<K> predicate, K def)
    Finds the first key matching the given predicate, or returns the default if none.
    Finds the first value matching the given predicate, or null if none.
    findFirstValue(Predicate<V> predicate, V def)
    Finds the first value matching the given predicate, or returns the default if none.
    void
    forEach(BiConsumer<K,V> consumer)
    Performs the given action for each entry in this builder.
    fromValue(V value)
    Finds the first key associated with the given value, or null if none.
    fromValue(V value, K def)
    Finds the first key associated with the given value, or returns the default if none.
    get(K key)
    Returns the value to which the specified key is mapped, or null if none.
    get(K key, V def)
    Returns the value to which the specified key is mapped, or the default if none.
    boolean
    Checks whether this builder contains no entries.
    @NotNull Iterator<Map.Entry<K,V>>
    Returns an iterator over the entries in this builder.
    Returns a List of all keys, preserving insertion order.
    <A, B> MapBuilder<A,B>
    map(Function<K,A> keyFunction, Function<V,B> valueFunction)
    Transforms both keys and values using the given functions.
    static <K, V> Map<K,V>
    mapOf(Collection<K> keys, Collection<V> values)
    Creates a Map by pairing elements from two collections: one of keys and one of values.
    static <K, V> Map<K,V>
    mapOf(K[] keys, V... values)
    Creates a Map by pairing elements from two arrays: one of keys and one of values.
    put(Map.Entry<? extends K,? extends V> entry)
    Adds the given entry to this builder.
    put(K key, V value)
    Associates the specified value with the specified key in this builder.
    putAll(Map<? extends K,? extends V> map)
    Copies all entries from the given map into this builder.
    putIfAbsent(Map.Entry<? extends K,? extends V> entry)
    Adds the given entry only if its key is not already present.
    putIfAbsent(K key, V value)
    Associates the specified value with the specified key if the key is not already present.
    remove(K key)
    Removes the mapping for the specified key if present.
    remove(K key, V value)
    Removes the entry for the specified key only if it is currently mapped to the given value.
    removeAllByKey(K key, int counting)
    Removes up to counting entries matching the given key.
    removeAllByValue(V value, int counting)
    Removes up to counting entries 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.
    int
    Returns the number of entries.
    Returns a string representation of the internal map.
    Returns a List of all values, preserving insertion order.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Constructor Details

    • MapBuilder

      public MapBuilder()
      Creates an empty MapBuilder.
    • MapBuilder

      public MapBuilder(Map<? extends K,? extends V> map)
      Creates a MapBuilder initialized with the entries of the given map.
      Parameters:
      map - an existing map whose entries will be copied; may be null
    • MapBuilder

      public MapBuilder(Collection<Map.Entry<? extends K,? extends V>> collection)
      Creates a MapBuilder initialized with the entries from the given collection.
      Parameters:
      collection - a collection of map entries to copy; may be null
  • Method Details

    • put

      public MapBuilder<K,V> put(K key, V value)
      Associates the specified value with the specified key in this builder.
      Parameters:
      key - the key to add
      value - the value to associate
      Returns:
      this builder (for chaining)
    • put

      public MapBuilder<K,V> put(Map.Entry<? extends K,? extends V> entry)
      Adds the given entry to this builder.
      Parameters:
      entry - the entry whose key and value to add
      Returns:
      this builder (for chaining)
    • putIfAbsent

      public MapBuilder<K,V> putIfAbsent(K key, V value)
      Associates the specified value with the specified key if the key is not already present.
      Parameters:
      key - the key to add
      value - the value to associate if absent
      Returns:
      this builder (for chaining)
    • putIfAbsent

      public MapBuilder<K,V> putIfAbsent(Map.Entry<? extends K,? extends V> entry)
      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

      public MapBuilder<K,V> putAll(Map<? extends K,? extends V> map)
      Copies all entries from the given map into this builder.
      Parameters:
      map - the map whose entries to add (must not be null)
      Returns:
      this builder (for chaining)
    • remove

      public MapBuilder<K,V> remove(K key)
      Removes the mapping for the specified key if present.
      Parameters:
      key - the key to remove
      Returns:
      this builder (for chaining)
    • remove

      public MapBuilder<K,V> remove(K key, V value)
      Removes the entry for the specified key only if it is currently mapped to the given value.
      Parameters:
      key - the key whose mapping to remove
      value - the value expected to be associated with the key
      Returns:
      this builder (for chaining)
    • removeAllByKey

      public MapBuilder<K,V> removeAllByKey(K key, int counting)
      Removes up to counting entries matching the given key. If counting is negative, all matching entries are removed.
      Parameters:
      key - the key to remove
      counting - the maximum number of entries to remove, or negative for unlimited
      Returns:
      this builder (for chaining)
    • removeAllByValue

      public MapBuilder<K,V> removeAllByValue(V value, int counting)
      Removes up to counting entries matching the given value. If counting is negative, all matching entries are removed.
      Parameters:
      value - the value to remove
      counting - the maximum number of entries to remove, or negative for unlimited
      Returns:
      this builder (for chaining)
    • filterByKey

      public MapBuilder<K,V> filterByKey(Predicate<K> predicate)
      Retains only those entries whose keys match the given predicate.
      Parameters:
      predicate - a predicate to test keys
      Returns:
      this builder (for chaining)
    • filterByValue

      public MapBuilder<K,V> filterByValue(Predicate<V> predicate)
      Retains only those entries whose values match the given predicate.
      Parameters:
      predicate - a predicate to test values
      Returns:
      this builder (for chaining)
    • filter

      public MapBuilder<K,V> filter(BiPredicate<K,V> predicate)
      Retains only those entries for which the given bi-predicate returns true.
      Parameters:
      predicate - a bi-predicate to test key and value pairs
      Returns:
      this builder (for chaining)
    • applyByKey

      public <A> MapBuilder<A,V> applyByKey(Function<K,A> function)
      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 MapBuilder with transformed keys
    • applyByValue

      public <B> MapBuilder<K,B> applyByValue(Function<V,B> function)
      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 MapBuilder with transformed values
    • map

      public <A, B> MapBuilder<A,B> map(Function<K,A> keyFunction, Function<V,B> valueFunction)
      Transforms both keys and values using the given functions.
      Type Parameters:
      A - the new key type
      B - the new value type
      Parameters:
      keyFunction - a function to apply to each key
      valueFunction - a function to apply to each value
      Returns:
      a new MapBuilder with transformed entries
    • containsKey

      public boolean containsKey(K key)
      Checks if the specified key is present.
      Parameters:
      key - the key to check
      Returns:
      true if present, false otherwise
    • containsValue

      public boolean containsValue(V value)
      Checks if the specified value is present.
      Parameters:
      value - the value to check
      Returns:
      true if present, false otherwise
    • get

      public V get(K key, V def)
      Returns the value to which the specified key is mapped, or the default if none.
      Parameters:
      key - the key
      def - the default value to return if key is absent
      Returns:
      the mapped or default value
    • get

      public V get(K key)
      Returns the value to which the specified key is mapped, or null if 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

      public K fromValue(V value, K def)
      Finds the first key associated with the given value, or returns the default if none.
      Parameters:
      value - the value to search for
      def - the default key if not found
      Returns:
      the found key or def
    • fromValue

      @Nullable public K fromValue(V value)
      Finds the first key associated with the given value, or null if none.
      Parameters:
      value - the value to search for
      Returns:
      the found key or null
    • findFirstKey

      public K findFirstKey(Predicate<K> predicate, K def)
      Finds the first key matching the given predicate, or returns the default if none.
      Parameters:
      predicate - a predicate to test keys
      def - the default key if not found
      Returns:
      the found key or def
    • findFirstKey

      @Nullable public K findFirstKey(Predicate<K> predicate)
      Finds the first key matching the given predicate, or null if none.
      Parameters:
      predicate - a predicate to test keys
      Returns:
      the found key or null
    • findFirstValue

      public V findFirstValue(Predicate<V> predicate, V def)
      Finds the first value matching the given predicate, or returns the default if none.
      Parameters:
      predicate - a predicate to test values
      def - the default value if not found
      Returns:
      the found value or def
    • findFirstValue

      @Nullable public V findFirstValue(Predicate<V> predicate)
      Finds the first value matching the given predicate, or null if none.
      Parameters:
      predicate - a predicate to test values
      Returns:
      the found value or null
    • keys

      public List<K> keys()
      Returns a List of all keys, preserving insertion order.
      Returns:
      a list of keys
    • values

      public List<V> values()
      Returns a List of all values, preserving insertion order.
      Returns:
      a list of values
    • entries

      public List<Map.Entry<K,V>> entries()
      Returns a List of all entries, preserving insertion order.
      Returns:
      a list of map entries
    • iterator

      @NotNull public @NotNull Iterator<Map.Entry<K,V>> iterator()
      Returns an iterator over the entries in this builder.
      Specified by:
      iterator in interface Iterable<K>
      Returns:
      an iterator of map entries
    • forEach

      public void forEach(BiConsumer<K,V> consumer)
      Performs the given action for each entry in this builder.
      Parameters:
      consumer - a bi-consumer accepting key and value
    • build

      public Map<K,V> build()
      Builds and returns a new LinkedHashMap containing the current entries.
      Returns:
      an immutable copy of the internal map
    • isEmpty

      public boolean isEmpty()
      Checks whether this builder contains no entries.
      Returns:
      true if empty, false otherwise
    • toString

      public String toString()
      Returns a string representation of the internal map.
      Overrides:
      toString in class Object
      Returns:
      a string representation of the internal map
    • singleton

      public static <K, V> Map<K,V> singleton(K key, V value)
      Creates a singleton map containing the given key-value pair.
      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      key - the single key
      value - the single value
      Returns:
      a map with exactly one entry
    • mapOf

      public static <K, V> Map<K,V> mapOf(Collection<K> keys, Collection<V> values)
      Creates a Map by pairing elements from two collections: one of keys and one of values.

      Each element in the keys collection is associated with the element at the same index in the values collection. The iteration order of the returned map follows the iteration order of the keys collection.

      Type Parameters:
      K - the type of the map keys
      V - the type of the map values
      Parameters:
      keys - the collection of keys (must not be null)
      values - the collection of values (must not be null), must have the same size as keys
      Returns:
      a new LinkedHashMap containing each key mapped to its corresponding value
      Throws:
      NullPointerException - if either keys or values is null
      IndexOutOfBoundsException - if keys.size() != values.size()
    • mapOf

      @SafeVarargs public static <K, V> Map<K,V> mapOf(K[] keys, V... values)
      Creates a Map by pairing elements from two arrays: one of keys and one of values.

      Each element in the keys array is associated with the element at the same index in the values array. The iteration order of the returned map follows the order of the keys array.

      Type Parameters:
      K - the type of the map keys
      V - the type of the map values
      Parameters:
      keys - the array of keys (must not be null)
      values - the array of values (must not be null), must have the same length as keys
      Returns:
      a new LinkedHashMap containing each key mapped to its corresponding value
      Throws:
      NullPointerException - if either keys or values is null
      IndexOutOfBoundsException - if keys.length != values.length
      See Also: