SortedSet TreeSet
layout: default
title: SortedSet Interface and TreeSet Cla
SortedSet is a subinterface of Set, which guarantees that its elements will be ordered.

SortedSet Methods¶
SortedSet adds two important methods, since it is ordered.
E first()- returns the first (lowest) element currently in this set.E last()- returns the last (highest) element currently in this set.
TreeSet Implementation Class¶
TreeSet orders elements according to their natural ordering.
natural ordering¶
An ordering built into a class (in a method) so that instance A can compare itself to instance B to determine if it is less than, equal to, or greater than instance B.
For example, the natural ordering of
Integeris smallest to largest, because everyIntegerobject can look at anotherIntegerobject and determine whose number is larger.
Practice Exercise¶
TreeSetmust maintain ordering as objects are added, so it has to compare the new object to current objects to determine where the new one goes.These comparison operations cost time, so make sure you have a reason to order elements before choosing to use
TreeSet.