Skip to main content

TreeSet

Learn TreeSet (Set Interface, Collection Framework) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.

What is a TreeSet? A TreeSet is a set that keeps its elements sorted at all times . Add forts in any order and they come out alphabetically; add numbers and they come out from small to large. It also never holds duplicates, and it can answer questions such as "which fort comes right after Raigad?" or "give me all forts between P and S". Package java.util . Implements NavigableSet (which extends SortedSet , which extends Set ). Sorted order: by the elements' natural order ( Comparable ) or by a Comparator you supply. No duplicates: "duplicate" means compareTo / compare returns 0, not equals() ....

More lessons in Set Interface