Class StackedHashMap<K,​V>

  • Type Parameters:
    K - the type of keys maintained by this map
    V - the type of mapped values
    All Implemented Interfaces:
    Map<K,​V>

    public class StackedHashMap<K,​V>
    extends AbstractMap<K,​V>
    A stacked HashMap implementation.

    This implementation is useful if you want to add and/or remove mappings to a map, but also want to return to a previous state of the map easily.

    Since:
    2.0.0
    Author:
    Marcel Overdijk
    See Also:
    HashMap
    • Constructor Detail

      • StackedHashMap

        public StackedHashMap()
        Constructs a new StackedHashMap.
      • StackedHashMap

        public StackedHashMap​(Map<? extends K,​? extends V> m)
        Constructs a new StackedHashMap with the same mappings as the supplied Map.
        Parameters:
        m - the map whose mappings are to be placed in this stacked map
    • Method Detail

      • push

        public void push()
        Pushes a new element (HashMap) onto the stack.
      • pop

        public HashMap<K,​V> pop()
        Pops the top element (HashMap) from the stack. Note that the initial element cannot be popped from the stack.
        Returns:
        the top element of the stack
        Throws:
        IllegalStateException - if there is only 1 element on the stack
      • peek

        public HashMap<K,​V> peek()
        Retrieves, but does not remove, the top element (HashMap) from the stack.
        Returns:
        the top element of the stack
      • stackSize

        public int stackSize()
        Returns the number of elements (HashMaps) on the stack. There will always be at least {code 1} element on the stack.
        Returns:
        the number of elements on the stack
      • clearStack

        public void clearStack()
        Clears the stack. After clearing the stack there will be 1 element (HashMap) on the stack without any mappings.