Package dsa
Class Set<Key extends Comparable<Key>>
- java.lang.Object
-
- dsa.Set<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 Summary
Constructors Constructor Description Set()
Constructs an empty set.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(Key key)
Addskey
to this set, if it is not already present.boolean
contains(Key key)
Returnstrue
if this set containskey
, andfalse
otherwise.void
delete(Key key)
Deleteskey
from this set.boolean
isEmpty()
Returnstrue
if this set is empty, andfalse
otherwise.Iterator<Key>
iterator()
Returns an iterator to iterate over the keys in this set in sorted order.static void
main(String[] args)
Unit tests the data type.int
size()
Returns the number of keys in this set.String
toString()
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
-
-
-
-
Method Detail
-
isEmpty
public boolean isEmpty()
Returnstrue
if this set is empty, andfalse
otherwise.- Returns:
true
if this set is empty, andfalse
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)
Addskey
to this set, if it is not already present.- Parameters:
key
- the key.
-
contains
public boolean contains(Key key)
Returnstrue
if this set containskey
, andfalse
otherwise.- Parameters:
key
- the key.- Returns:
true
if this set containskey
, andfalse
otherwise.
-
delete
public void delete(Key key)
Deleteskey
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 interfaceIterable<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.
-
main
public static void main(String[] args)
Unit tests the data type.- Parameters:
args
- the command-line arguments.
-
-