Package dsa

Class Set<T extends Comparable<T>>

java.lang.Object
dsa.Set<T>
Type Parameters:
T - the type of items in the set.
All Implemented Interfaces:
Iterable<T>

public class Set<T extends Comparable<T>> extends Object implements Iterable<T>
An iterable data type to represent an ordered set of items.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Set()
    Constructs an empty set.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(T item)
    Adds item to this set, if it is not already present.
    boolean
    contains(T item)
    Returns true if this set contains item, and false otherwise.
    void
    delete(T item)
    Deletes item from this set.
    boolean
    Returns true if this set is empty, and false otherwise.
    Returns an iterator to iterate over the items in this set in sorted order.
    static void
    main(String[] args)
    Unit tests the data type.
    int
    Returns the number of items in this set.
    Returns a string representation of this set.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Constructor Details

    • Set

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

    • 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 items in this set.
      Returns:
      the number of items in this set.
    • add

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

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

      public void delete(T item)
      Deletes item from this set.
      Parameters:
      item - the item.
    • iterator

      public Iterator<T> iterator()
      Returns an iterator to iterate over the items in this set in sorted order.
      Specified by:
      iterator in interface Iterable<T extends Comparable<T>>
      Returns:
      an iterator to iterate over the items 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.