Java TreeSet class

Java TreeSet class implements the Set interface that uses a tree for storage. It inherits AbstractSet class and implements NavigableSet interface. The objects of TreeSet class are stored in ascending order.

The important points about Java TreeSet class are:

  • Contains unique elements only like HashSet.
  • Access and retrieval times are quiet fast.
  • Maintains ascending order.
  • TreeSet class declaration

    public class TreeSet extends AbstractSet implements NavigableSet, Cloneable, Serializable

    Constructors

    TreeSet() It is used to construct an empty tree set that will be sorted in an ascending order according to the natural order of the tree set.

    TreeSet(Collection c) It is used to build a new tree set that contains the elements of the collection c.

    TreeSet(Comparator comp) It is used to construct an empty tree set that will be sorted according to given comparator.

    TreeSet(SortedSet ss) It is used to build a TreeSet that contains the elements of the given SortedSet.

    Method

    boolean addAll(Collection c) It is used to add all of the elements in the specified collection to this set.

    boolean contains(Object o) It is used to return true if this set contains the specified element.

    boolean isEmpty() It is used to return true if this set contains no elements.

    boolean remove(Object o) It is used to remove the specified element from this set if it is present.

    void add(Object o) It is used to add the specified element to this set if it is not already present.

    void clear() It is used to remove all of the elements from this set.

    Object clone() It is used to return a shallow copy of this TreeSet instance.

    EXAMPLE

    Flash Soon



    Advertisement

    © Sapster 2017