Wednesday, May 18, 2011

TreeSet class constructors in java


  • Set interface implemented as a tree
  • Iterator return elements in natural order (see later)

Constructors of TreeSet Class

TreeSet implements the Set and SortedSet interface methods. TreeSet is implemented with an underlying TreeMap (balanced binary tree). If the element type has a natural order (eg, String) elements will be ordered by that, but often you will supply a Comparator object that tells how two elements compare. It has the following constructors.

Result Constructor Description
tset = new TreeSet() Creates new TreeSet. Elements sorted by natural order.
tset = new TreeSet(comp) Creates new TreeSet using Comparator comp to sort elements.
tset = new TreeSet(coll) Creates new TreeSet from Collection coll using natural ordering.
tset = new TreeSet(sset) Creates new TreeSet from SortedSet smp using element ordering from sset.

No comments:

Post a Comment