Java Reference
In-Depth Information
Thissametechniquecanbeappliedwhereveryouneedtoforman open range or open
interval (neither endpoint is included). For example, ss.subSet("carrot\0",
"cucumber") doesnotinclude carrot becauseitislessthan carrot\0 .Further-
more, it does not include high endpoint cucumber .
Note When you want to create closed and open ranges for elements created from
your own classes, you need to provide some form of predecessor() and suc-
cessor() methods that return an element's predecessor and successor.
You need to be careful when designing classes that work with sorted sets. For ex-
ample, the class must implement Comparable when you plan to store the class's in-
ing. Consider Listing 5-10 .
Listing 5-10. A custom Employee class not implementing Comparable
import java.util.SortedSet;
import java.util.TreeSet;
class CustomClassAndSortedSet
public static void main(String[] args)
SortedSet<Employee> sse = new TreeSet<>();
sse.add(new Employee("sally doe"));
sse.add(new Employee("bob doe")); // ClassCastExcep-
tion thrown here
sse.add(new Employee("john doe"));
class Employee
private String name;
Employee(String name)
{ = name;
Search WWH ::

Custom Search