Module MaterialFX

Class BindingsMap<K,V>

java.lang.Object
java.util.AbstractMap<K,V>
java.util.WeakHashMap<K,V>
io.github.palexdev.materialfx.bindings.BindingsMap<K,V>
All Implemented Interfaces:
Map<K,V>

public class BindingsMap<K,V> extends WeakHashMap<K,V>
A special WeakHashMap that allows to retrieve the keys ordered by insertion.

Writing a LinkedWeakHashMap would have been too much work and a very hard task. For this reason this Map simply uses a LinkedList to store the keys (wrapped in WeakReferences) by their insertion order.

Just like the WeakHashMap this list is cleared (all null references are removed) when major operations occur, such as: put, putAll, combine.

Allows to retrieve the first and the last keys, and also a copy of the actual LinkedList.
  • Constructor Details

    • BindingsMap

      public BindingsMap()
    • BindingsMap

      public BindingsMap(Map<? extends K,? extends V> m)
    • BindingsMap

      public BindingsMap(int initialCapacity)
    • BindingsMap

      public BindingsMap(int initialCapacity, float loadFactor)
  • Method Details

    • combine

      public void combine(BindingsMap<K,V> source)
      Allows to combine the given BindingsMap to this one.

      This method exists to ensure that insertion order is kept with the LinkedList but most importantly ensures that there are no duplicates in the list by using a LinkedHashSet.

    • put

      public V put(K key, V value)
      Adds the given key to the keys LinkedList, performs clearReferences() and then calls the super method.
      Specified by:
      put in interface Map<K,V>
      Overrides:
      put in class WeakHashMap<K,V>
    • putAll

      public void putAll(Map<? extends K,? extends V> m)
      Overridden to call putAll(Map.Entry[]).
      Specified by:
      putAll in interface Map<K,V>
      Overrides:
      putAll in class WeakHashMap<K,V>
    • putAll

      @SafeVarargs public final void putAll(Map.Entry<K,V>... entries)
      For each entry adds the key to the keys LinkedList, then calls the super method. At the end performs clearReferences().
    • remove

      public V remove(Object key)
      Removes the given key from the keys LinkedList and then calls the super method.
      Specified by:
      remove in interface Map<K,V>
      Overrides:
      remove in class WeakHashMap<K,V>
    • clear

      public void clear()
      Clears the keys LinkedList and then calls the super method.
      Specified by:
      clear in interface Map<K,V>
      Overrides:
      clear in class WeakHashMap<K,V>
    • replaceAll

      public void replaceAll(BiFunction<? super K,? super V,? extends V> function)
      UNSUPPORTED
      Specified by:
      replaceAll in interface Map<K,V>
      Overrides:
      replaceAll in class WeakHashMap<K,V>
    • unmodifiableKeysList

      public LinkedList<WeakReference<K>> unmodifiableKeysList()
      Returns:
      a copy of the LinkedList containing the Map's keys ordered by insertion
    • getFirstKey

      public K getFirstKey()
      Returns:
      the first inserted key
    • getLastKey

      public K getLastKey()
      Returns:
      the last inserted key