类 ST<Key extends java.lang.Comparable<Key>,​Value>

  • 类型参数:
    Key - the generic type of keys in this symbol table
    Value - the generic type of values in this symbol table
    所有已实现的接口:
    java.lang.Iterable<Key>

    public class ST<Key extends java.lang.Comparable<Key>,​Value>
    extends java.lang.Object
    implements java.lang.Iterable<Key>
    The ST class represents an ordered symbol table of generic key-value pairs. It supports the usual put, get, contains, delete, size, and is-empty methods. It also provides ordered methods for finding the minimum, maximum, floor, and ceiling. 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 balanced binary search tree. It requires that the key type implements the Comparable interface and calls the compareTo() and method to compare two keys. It does not call either equals() or hashCode(). The put, contains, remove, minimum, maximum, ceiling, and floor operations each take logarithmic time in the worst case. The size, and is-empty operations take constant time. Construction takes constant time.

    For additional documentation, see Section 3.5 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.

    • 构造器概要

      构造器 
      构造器 说明
      ST()
      Initializes an empty symbol table.
    • 方法概要

      修饰符和类型 方法 说明
      Key ceiling​(Key key)
      Returns the smallest key in this symbol table greater than or equal to key.
      boolean contains​(Key key)
      Returns true if this symbol table contain the given 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).
      Key floor​(Key key)
      Returns the largest key in this symbol table less than or equal to key.
      Value get​(Key key)
      Returns the value associated with the given key in this symbol table.
      boolean isEmpty()
      Returns true if this symbol table is empty.
      java.util.Iterator<Key> iterator()
      已过时。
      Replaced by keys().
      java.lang.Iterable<Key> keys()
      Returns all keys in this symbol table.
      static void main​(java.lang.String[] args)
      Unit tests the ST data type.
      Key max()
      Returns the largest key in this symbol table.
      Key min()
      Returns the smallest key in this symbol table.
      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.
      • 从类继承的方法 java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • 从接口继承的方法 java.lang.Iterable

        forEach, spliterator
    • 构造器详细资料

      • ST

        public ST()
        Initializes an empty symbol table.
    • 方法详细资料

      • get

        public Value get​(Key key)
        Returns the value associated with the given key in this symbol table.
        参数:
        key - the key
        返回:
        the value associated with the given key if the key is in this symbol table; null if the key is not in this symbol table
        抛出:
        java.lang.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.
        参数:
        key - the key
        val - the value
        抛出:
        java.lang.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).
        参数:
        key - the key
        抛出:
        java.lang.IllegalArgumentException - if key is null
      • contains

        public boolean contains​(Key key)
        Returns true if this symbol table contain the given key.
        参数:
        key - the key
        返回:
        true if this symbol table contains key and false otherwise
        抛出:
        java.lang.IllegalArgumentException - if key is null
      • size

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

        public boolean isEmpty()
        Returns true if this symbol table is empty.
        返回:
        true if this symbol table is empty and false otherwise
      • keys

        public java.lang.Iterable<Key> keys()
        Returns all keys in this symbol table.

        To iterate over all of the keys in the symbol table named st, use the foreach notation: for (Key key : st.keys()).

        返回:
        all keys in this symbol table
      • iterator

        @Deprecated
        public java.util.Iterator<Key> iterator()
        已过时。
        Replaced by keys().
        Returns all of the keys in this symbol table. To iterate over all of the keys in a symbol table named st, use the foreach notation: for (Key key : st).

        This method is provided for backward compatibility with the version from Introduction to Programming in Java: An Interdisciplinary Approach.

        指定者:
        iterator 在接口中 java.lang.Iterable<Key extends java.lang.Comparable<Key>>
        返回:
        an iterator to all of the keys in this symbol table
      • min

        public Key min()
        Returns the smallest key in this symbol table.
        返回:
        the smallest key in this symbol table
        抛出:
        java.util.NoSuchElementException - if this symbol table is empty
      • max

        public Key max()
        Returns the largest key in this symbol table.
        返回:
        the largest key in this symbol table
        抛出:
        java.util.NoSuchElementException - if this symbol table is empty
      • ceiling

        public Key ceiling​(Key key)
        Returns the smallest key in this symbol table greater than or equal to key.
        参数:
        key - the key
        返回:
        the smallest key in this symbol table greater than or equal to key
        抛出:
        java.util.NoSuchElementException - if there is no such key
        java.lang.IllegalArgumentException - if key is null
      • floor

        public Key floor​(Key key)
        Returns the largest key in this symbol table less than or equal to key.
        参数:
        key - the key
        返回:
        the largest key in this symbol table less than or equal to key
        抛出:
        java.util.NoSuchElementException - if there is no such key
        java.lang.IllegalArgumentException - if key is null
      • main

        public static void main​(java.lang.String[] args)
        Unit tests the ST data type.
        参数:
        args - the command-line arguments