Java SE API
TreeSet
Sorted navigable set using natural order or a comparator. Basic membership operations are logarithmic; comparison ties define its membership equivalence.
Formal Syntax & Structure
java syntaxjava
class TreeSet<E>Working Example
Example Snippetjava
import java.util.*;public class Main { static final class LengthOrder implements Comparator<String> { public int compare(String a, String b) { return Integer.compare(a.length(), b.length()); } } public static void main(String[] args) { Set<String> byLength = new TreeSet<>(new LengthOrder()); System.out.println(byLength.add("cat")); System.out.println(byLength.add("dog")); System.out.println(byLength.contains("owl")); System.out.println(byLength); }}