Home
Cloud
Big Data
CI
Install
Samples
Java
Ubuntu
Maven
Archive
Java
|
TreeSet
Notes
Example
Example: Comparator
Notes
Objects must implement java.lang.Comparable<T> (Exception: cannot be cast to java.lang.Comparable)
Sorted collection
null elements not allowed
Duplicates elements not allowed (hash code)
Example
TreeSet<String> ts = new TreeSet<>(); // ts.add(null); // java.lang.NullPointerException ts.add("c"); ts.add("bb"); ts.add("bb"); ts.add("aaa"); ts.add("aaa"); ts.add("aaa"); ts.forEach(System.out::println);
Output:
aaa bb c
Example: Comparator
TreeSet<String> ts = new TreeSet<>(Comparator.comparing(String::length)); // ts.add(null); // java.lang.NullPointerException ts.add("c"); ts.add("bb"); ts.add("bb"); ts.add("aaa"); ts.add("aaa"); ts.add("aaa"); ts.forEach(System.out::println);
Output:
c bb aaa
© 2010-2022
mti
tek