类 SET<Key extends java.lang.Comparable<Key>>

  • 类型参数:
    Key - the generic type of a key in this set
    所有已实现的接口:
    java.lang.Iterable<Key>

    public class SET<Key extends java.lang.Comparable<Key>>
    extends java.lang.Object
    implements java.lang.Iterable<Key>
    The SET class represents an ordered set of comparable keys. It supports the usual add, contains, and delete methods. It also provides ordered methods for finding the minimum, maximum, floor, and ceiling and set methods for union, intersection, and equality.

    Even though this implementation include the method equals(), it does not support the method hashCode() because sets are mutable.

    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 add, contains, delete, minimum, maximum, ceiling, and floor methods 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 in Java, 4th Edition by Robert Sedgewick and Kevin Wayne.

    • 构造器概要

      构造器 
      构造器 说明
      SET()
      Initializes an empty set.
      SET​(SET<Key> x)
      Initializes a new set that is an independent copy of the specified set.
    • 方法概要

      修饰符和类型 方法 说明
      void add​(Key key)
      Adds the key to this set (if it is not already present).
      Key ceiling​(Key key)
      Returns the smallest key in this set greater than or equal to key.
      boolean contains​(Key key)
      Returns true if this set contains the given key.
      void delete​(Key key)
      Removes the specified key from this set (if the set contains the specified key).
      boolean equals​(java.lang.Object other)
      Compares this set to the specified set.
      Key floor​(Key key)
      Returns the largest key in this set less than or equal to key.
      int hashCode()
      This operation is not supported because sets are mutable.
      SET<Key> intersects​(SET<Key> that)
      Returns the intersection of this set and that set.
      boolean isEmpty()
      Returns true if this set is empty.
      java.util.Iterator<Key> iterator()
      Returns all of the keys in this set, as an iterator.
      static void main​(java.lang.String[] args)
      Unit tests the SET data type.
      Key max()
      Returns the largest key in this set.
      Key min()
      Returns the smallest key in this set.
      int size()
      Returns the number of keys in this set.
      java.lang.String toString()
      Returns a string representation of this set.
      SET<Key> union​(SET<Key> that)
      Returns the union of this set and that set.
      • 从类继承的方法 java.lang.Object

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

        forEach, spliterator
    • 构造器详细资料

      • SET

        public SET()
        Initializes an empty set.
      • SET

        public SET​(SET<Key> x)
        Initializes a new set that is an independent copy of the specified set.
        参数:
        x - the set to copy
    • 方法详细资料

      • add

        public void add​(Key key)
        Adds the key to this set (if it is not already present).
        参数:
        key - the key to add
        抛出:
        java.lang.IllegalArgumentException - if key is null
      • contains

        public boolean contains​(Key key)
        Returns true if this set contains the given key.
        参数:
        key - the key
        返回:
        true if this set contains key; false otherwise
        抛出:
        java.lang.IllegalArgumentException - if key is null
      • delete

        public void delete​(Key key)
        Removes the specified key from this set (if the set contains the specified key).
        参数:
        key - the key
        抛出:
        java.lang.IllegalArgumentException - if key is null
      • size

        public int size()
        Returns the number of keys in this set.
        返回:
        the number of keys in this set
      • isEmpty

        public boolean isEmpty()
        Returns true if this set is empty.
        返回:
        true if this set is empty; false otherwise
      • iterator

        public java.util.Iterator<Key> iterator()
        Returns all of the keys in this set, as an iterator. To iterate over all of the keys in a set named set, use the foreach notation: for (Key key : set).
        指定者:
        iterator 在接口中 java.lang.Iterable<Key extends java.lang.Comparable<Key>>
        返回:
        an iterator to all of the keys in this set
      • max

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

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

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

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

        public SET<Key> union​(SET<Key> that)
        Returns the union of this set and that set.
        参数:
        that - the other set
        返回:
        the union of this set and that set
        抛出:
        java.lang.IllegalArgumentException - if that is null
      • intersects

        public SET<Key> intersects​(SET<Key> that)
        Returns the intersection of this set and that set.
        参数:
        that - the other set
        返回:
        the intersection of this set and that set
        抛出:
        java.lang.IllegalArgumentException - if that is null
      • equals

        public boolean equals​(java.lang.Object other)
        Compares this set to the specified set.

        Note that this method declares two empty sets to be equal even if they are parameterized by different generic types. This is consistent with the behavior of equals() within Java's Collections framework.

        覆盖:
        equals 在类中 java.lang.Object
        参数:
        other - the other set
        返回:
        true if this set equals other; false otherwise
      • hashCode

        public int hashCode()
        This operation is not supported because sets are mutable.
        覆盖:
        hashCode 在类中 java.lang.Object
        返回:
        does not return a value
        抛出:
        java.lang.UnsupportedOperationException - if called
      • toString

        public java.lang.String toString()
        Returns a string representation of this set.
        覆盖:
        toString 在类中 java.lang.Object
        返回:
        a string representation of this set, enclosed in curly braces, with adjacent keys separated by a comma and a space
      • main

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