Class ProbingHashMap<Key,Value>

java.lang.Object
dsa.lab06.exercises.ProbingHashMap<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 ProbingHashMap<Key,Value> extends Object implements Map<Key,Value>
A (linearly-)probing hash map.
  • Constructor Details

    • ProbingHashMap

      public ProbingHashMap()
      Construct an empty probing hash map.
    • ProbingHashMap

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

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

      @SafeVarargs public ProbingHashMap(MapItem<Key,Value>... items)
      Construct a probing 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
    • insert

      public void insert(MapItem<Key,Value> newItem)
      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:
      newItem - the item
    • 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
    • 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