Mega Code Archive

 
Categories / Java Book / 005 Collection
 

0323 SortedSet

SortedSet, whose generic type is SortedSet<E>, extends Set. A sorted set maintains its elements in order. The sequence is controled by comparator that is supplied when the sorted set is created. Sorted sets are described by the SortedSet interface. TreeSet is an example of a sorted set. The following code demonstrates a sorted set based on a tree set. import java.util.SortedSet; import java.util.TreeSet; public class Main { public static void main(String[] args) { SortedSet<String> sortedSet = new TreeSet<String>(); String[] strings = { "a", "p", "t", "b"}; System.out.println("Array size = " + strings.length); for (String s : strings){ sortedSet.add(s); } dump("sortedSet:", sortedSet); System.out.println("Sorted set size = " + sortedSet.size()); System.out.println("First element = " + sortedSet.first()); System.out.println("Last element = " + sortedSet.last()); System.out.println("Comparator = " + sortedSet.comparator()); dump("headSet:", sortedSet.headSet("n")); dump("tailSet:", sortedSet.tailSet("n")); System.out.println("Count = " + sortedSet.subSet("p", "q").size()); System.out.println("Incorrect count = " + sortedSet.subSet("c", "b").size()); System.out.println("Correct count = " + sortedSet.subSet("c", "t").size()); } static void dump(String title, SortedSet<String> sss) { for (String s : sss){ System.out.print(s + " "); } System.out.println(); } } A custom Employee class not implementing Comparable import java.util.SortedSet; import java.util.TreeSet; public class Main { public static void main(String[] args) { SortedSet<Employee> sse = new TreeSet<Employee>(); sse.add(new Employee("S")); sse.add(new Employee("B")); // ClassCastException thrown here sse.add(new Employee("J")); System.out.println(sse); } } class Employee { private String name; Employee(String name) { this.name = name; } @Override public String toString() { return name; } } Exception in thread "main" java.lang.ClassCastException: Employee cannot be cast to java.lang.Comparable at java.util.TreeMap.put(TreeMap.java:542) at java.util.TreeSet.add(TreeSet.java:238) at Main.main(Main.java:8) The ClassCastException instance is thrown during the second add() method call. The sorted set is unable to call the second Employee element's compareTo() method. The following code makes Employee elements comparable. import java.util.SortedSet; import java.util.TreeSet; public class Main { public static void main(String[] args) { SortedSet<Employee> sse = new TreeSet<Employee>(); sse.add(new Employee("S")); sse.add(new Employee("B")); Employee e1 = new Employee("J"); Employee e2 = new Employee("J"); sse.add(e1); sse.add(e2); System.out.println(sse); System.out.println(e1.equals(e2)); } } class Employee implements Comparable<Employee> { private String name; Employee(String name) { this.name = name; } @Override public int compareTo(Employee e) { return name.compareTo(e.name); } @Override public String toString() { return name; } } [B, J, S] false