Java TreeSetTutorial with Example program



Class TreeSet

Type Parameters:
    E - the type of elements maintained by this set
    
All Implemented Interfaces:

    Serializable, Cloneable, Iterable, Collection, NavigableSet, Set, SortedSet

public class TreeSet
extends AbstractSet
implements NavigableSet, Cloneable, Serializable
{

//
}


Since:
    1.2
    
See Also:
    Collection, Set, HashSet, Comparable, Comparator, TreeMap, Serialized Form
    
Constructors summary

TreeSet()
    Constructs a new, empty tree set, sorted according to the natural ordering of its elements.
    
TreeSet(Collection c)
    Constructs a new tree set containing the elements in the specified collection, sorted according to the natural ordering of its elements.
    
TreeSet(Comparator comparator)
    Constructs a new, empty tree set, sorted according to the specified comparator.
    
TreeSet(SortedSet s)
    Constructs a new tree set containing the same elements and using the same ordering as the specified sorted set.
    
Methods summary

boolean    add(E e)
    Adds the specified element to this set if it is not already present.
    
boolean    addAll(Collection c)
    Adds all of the elements in the specified collection to this set.
    
E    ceiling(E e)
    Returns the least element in this set greater than or equal to the given element, or null if there is no such element.
    
void    clear()
    Removes all of the elements from this set.
    
Object    clone()
    Returns a shallow copy of this TreeSet instance.
    
Comparator    comparator()
    Returns the comparator used to order the elements in this set, or null if this set uses the natural ordering of its elements.
    
boolean    contains(Object o)
    Returns true if this set contains the specified element.
    
Iterator    descendingIterator()
    Returns an iterator over the elements in this set in descending order.
    
NavigableSet    descendingSet()
    Returns a reverse order view of the elements contained in this set.
    
E    first()
    Returns the first (lowest) element currently in this set.
    
E    floor(E e)
    Returns the greatest element in this set less than or equal to the given element, or null if there is no such element.
    
SortedSet    headSet(E toElement)
    Returns a view of the portion of this set whose elements are strictly less than toElement.
    
NavigableSet    headSet(E toElement, boolean inclusive)
    Returns a view of the portion of this set whose elements are less than (or equal to, if inclusive is true) toElement.
    
E    higher(E e)
    Returns the least element in this set strictly greater than the given element, or null if there is no such element.
    
boolean    isEmpty()
    Returns true if this set contains no elements.
    
Iterator    iterator()
    Returns an iterator over the elements in this set in ascending order.
    
E    last()
    Returns the last (highest) element currently in this set.
    
E    lower(E e)
    Returns the greatest element in this set strictly less than the given element, or null if there is no such element.
    
E    pollFirst()
    Retrieves and removes the first (lowest) element, or returns null if this set is empty.
    
E    pollLast()
    Retrieves and removes the last (highest) element, or returns null if this set is empty.
    
boolean    remove(Object o)
    Removes the specified element from this set if it is present.
    
int    size()
    Returns the number of elements in this set (its cardinality).
    
NavigableSet    subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
    Returns a view of the portion of this set whose elements range from fromElement to toElement.
    
SortedSet    subSet(E fromElement, E toElement)
    Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.
    
SortedSet    tailSet(E fromElement)
    Returns a view of the portion of this set whose elements are greater than or equal to fromElement.
    
NavigableSet    tailSet(E fromElement, boolean inclusive)
    Returns a view of the portion of this set whose elements are greater than (or equal to, if inclusive is true) fromElement.
    
Methods inherited from class java.util.AbstractSet


public boolean removeAll(Collection c)
    Removes from this set all of its elements that are contained in the specified collection (optional operation). If the specified collection is also a set, this operation effectively modifies this set so that its value is the asymmetric set difference of the two sets.
    This implementation determines which is the smaller of this set and the specified collection, by invoking the size method on each. If this set has fewer elements, then the implementation iterates over this set, checking each element returned by the iterator in turn to see if it is contained in the specified collection. If it is so contained, it is removed from this set with the iterator's remove method. If the specified collection has fewer elements, then the implementation iterates over the specified collection, removing from this set each element returned by the iterator, using this set's remove method.

Methods inherited from class java.util.AbstractCollection

public boolean containsAll(Collection c)
    Returns true if this collection contains all of the elements in the specified collection.
    This implementation iterates over the specified collection, checking each element returned by the iterator in turn to see if it's contained in this collection. If all elements are so contained true is returned, otherwise false.

public boolean retainAll(Collection c)
    Retains only the elements in this collection that are contained in the specified collection (optional operation). In other words, removes from this collection all of its elements that are not contained in the specified collection.

public Object[] toArray()
    Returns an array containing all of the elements in this collection. If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.
    The returned array will be "safe" in that no references to it are maintained by this collection. (In other words, this method must allocate a new array even if this collection is backed by an array). The caller is thus free to modify the returned array.
    This method acts as bridge between array-based and collection-based APIs.
    
public T[] toArray(T[] a)
    Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this collection.

Methods inherited from interface java.util.Set

public boolean containsAll(Collection c)
    Returns true if this collection contains all of the elements in the specified collection.
    This implementation iterates over the specified collection, checking each element returned by the iterator in turn to see if it's contained in this collection. If all elements are so contained true is returned, otherwise false.

Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait,equals,hashCode,toString



Related Post

Comments


©candidjava.com