Class ChainingHashMap<Key,Value>

java.lang.Object
dsa.lab05.solutions.ChainingHashMap<Key,Value>
Type Parameters:
Key - the key type
Value - the value type
All Implemented Interfaces:
Container<MapItem<Key,Value>>, Map<Key,Value>, Iterable<MapItem<Key,Value>>

public class ChainingHashMap<Key,Value> extends Object implements Map<Key,Value>
A chaining hash map.
  • Constructor Details

    • ChainingHashMap

      public ChainingHashMap()
      Construct an empty chaining hash map.
    • ChainingHashMap

      public ChainingHashMap(Iterable<MapItem<Key,Value>> items)
      Construct a chaining hash map containing the given items.
      Parameters:
      items - the items
    • ChainingHashMap

      public ChainingHashMap(Iterable<MapItem<Key,Value>> items, int size) throws IllegalArgumentException
      Construct a chaining hash map containing the given items more efficiently than ChainingHashMap(Iterable).
      Parameters:
      items - the items
      size - the number of items
      Throws:
      IllegalArgumentException - if size != n (where n is items's size)
    • ChainingHashMap

      @SafeVarargs public ChainingHashMap(MapItem<Key,Value>... items)
      Construct a chaining hash map containing the given items.
      Parameters:
      items - the items
  • Method Details

    • size

      public int size()
      Description copied from interface: Container
      Get the number of contained items.
      Specified by:
      size in interface Container<Key>
      Returns:
      the size
    • find

      public MapItem<Key,Value> find(Key key) throws NoSuchElementException
      Description copied from interface: Map
      Find the item with the given key.
      Specified by:
      find in interface Map<Key,Value>
      Parameters:
      key - the item's key
      Returns:
      the item
      Throws:
      NoSuchElementException - if no item has key key
    • insert

      public void insert(MapItem<Key,Value> item)
      Description copied from interface: Map
      Insert the given item.

      If there's already an item with the same key, that item is replaced with this one.

      This means that if the key was not already contained, the size is incremented, otherwise it isn't.

      Specified by:
      insert in interface Map<Key,Value>
      Parameters:
      item - the item
    • remove

      public MapItem<Key,Value> remove(Key key) throws NoSuchElementException
      Description copied from interface: Map
      Remove and return the item with the given key.

      Decrements the size (assuming the item was actually in the map).

      Specified by:
      remove in interface Map<Key,Value>
      Parameters:
      key - the item's key
      Returns:
      the item
      Throws:
      NoSuchElementException - if key is not contained
    • items

      public Iterable<MapItem<Key,Value>> items()
      Description copied from interface: Container
      Get an iterable that yields each item once.
      Specified by:
      items in interface Container<Key>
      Returns:
      an iterable over the items