Package dsa

Class Set<Key extends Comparable<Key>>

  • All Implemented Interfaces:
    Iterable<Key>

    public class Set<Key extends Comparable<Key>>
    extends Object
    implements Iterable<Key>
    An iterable data type to represent an ordered set.
    • Constructor Detail

      • Set

        public Set()
        Constructs an empty set.
    • Method Detail

      • isEmpty

        public boolean isEmpty()
        Returns true if this set is empty, and false otherwise.
        Returns:
        true if this set is empty, and false otherwise.
      • size

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

        public void add​(Key key)
        Adds key to this set, if it is not already present.
        Parameters:
        key - the key.
      • contains

        public boolean contains​(Key key)
        Returns true if this set contains key, and false otherwise.
        Parameters:
        key - the key.
        Returns:
        true if this set contains key, and false otherwise.
      • delete

        public void delete​(Key key)
        Deletes key from this set.
        Parameters:
        key - the key.
      • iterator

        public Iterator<Key> iterator()
        Returns an iterator to iterate over the keys in this set in sorted order.
        Specified by:
        iterator in interface Iterable<Key extends Comparable<Key>>
        Returns:
        an iterator to iterate over the keys in this set in sorted order.
      • toString

        public String toString()
        Returns a string representation of this set.
        Overrides:
        toString in class Object
        Returns:
        a string representation of this set.
      • main

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