Class LinearProbingHashST<Key,​Value>


  • public class LinearProbingHashST<Key,​Value>
    extends Object
    The LinearProbingHashST class represents a symbol table of generic key-value pairs. It supports the usual put, get, contains, delete, size, and is-empty methods. It also provides a keys method for iterating over all of the keys. A symbol table implements the associative array abstraction: when associating a value with a key that is already in the symbol table, the convention is to replace the old value with the new value. Unlike Map, this class uses the convention that values cannot be null—setting the value associated with a key to null is equivalent to deleting the key from the symbol table.

    This implementation uses a linear probing hash table. It requires that the key type overrides the equals() and hashCode() methods. The expected time per put, contains, or remove operation is constant, subject to the uniform hashing assumption. The size, and is-empty operations take constant time. Construction takes constant time.

    For additional documentation, see Section 3.4 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne. For other implementations, see ST, BinarySearchST, SequentialSearchST, BST, RedBlackBST, and SeparateChainingHashST,

    Author:
    Robert Sedgewick, Kevin Wayne
    • Constructor Summary

      Constructors 
      Constructor Description
      LinearProbingHashST()
      Initializes an empty symbol table.
      LinearProbingHashST​(int capacity)
      Initializes an empty symbol table with the specified initial capacity.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean contains​(Key key)
      Returns true if this symbol table contains the specified key.
      void delete​(Key key)
      Removes the specified key and its associated value from this symbol table (if the key is in this symbol table).
      Value get​(Key key)
      Returns the value associated with the specified key.
      boolean isEmpty()
      Returns true if this symbol table is empty.
      Iterable<Key> keys()
      Returns all keys in this symbol table as an Iterable.
      static void main​(String[] args)
      Unit tests the LinearProbingHashST data type.
      void put​(Key key, Value val)
      Inserts the specified key-value pair into the symbol table, overwriting the old value with the new value if the symbol table already contains the specified key.
      int size()
      Returns the number of key-value pairs in this symbol table.
    • Constructor Detail

      • LinearProbingHashST

        public LinearProbingHashST()
        Initializes an empty symbol table.
      • LinearProbingHashST

        public LinearProbingHashST​(int capacity)
        Initializes an empty symbol table with the specified initial capacity.
        Parameters:
        capacity - the initial capacity
    • Method Detail

      • size

        public int size()
        Returns the number of key-value pairs in this symbol table.
        Returns:
        the number of key-value pairs in this symbol table
      • isEmpty

        public boolean isEmpty()
        Returns true if this symbol table is empty.
        Returns:
        true if this symbol table is empty; false otherwise
      • contains

        public boolean contains​(Key key)
        Returns true if this symbol table contains the specified key.
        Parameters:
        key - the key
        Returns:
        true if this symbol table contains key; false otherwise
        Throws:
        IllegalArgumentException - if key is null
      • put

        public void put​(Key key,
                        Value val)
        Inserts the specified key-value pair into the symbol table, overwriting the old value with the new value if the symbol table already contains the specified key. Deletes the specified key (and its associated value) from this symbol table if the specified value is null.
        Parameters:
        key - the key
        val - the value
        Throws:
        IllegalArgumentException - if key is null
      • get

        public Value get​(Key key)
        Returns the value associated with the specified key.
        Parameters:
        key - the key
        Returns:
        the value associated with key; null if no such value
        Throws:
        IllegalArgumentException - if key is null
      • delete

        public void delete​(Key key)
        Removes the specified key and its associated value from this symbol table (if the key is in this symbol table).
        Parameters:
        key - the key
        Throws:
        IllegalArgumentException - if key is null
      • keys

        public Iterable<Key> keys()
        Returns all keys in this symbol table as an Iterable. To iterate over all of the keys in the symbol table named st, use the foreach notation: for (Key key : st.keys()).
        Returns:
        all keys in this symbol table
      • main

        public static void main​(String[] args)
        Unit tests the LinearProbingHashST data type.
        Parameters:
        args - the command-line arguments