Class SortedCollection

All Implemented Interfaces:
Iterable, Collection

Implements a Collection that keeps it's elements in Sorted order. This class is internally backed by a LinkedList, therefore iteration is strongly preferred (and much faster) than random access of it's elements. This class has no equivalent in JDK 1.4 and should be replaced by a future JDK equivalent if available. Currently java.util only provides a sorted Set not a sorted Collection (like this one) that allows duplicate elements.

Note that this implementation is not synchronized. If multiple threads access a list concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list. If no such object exists, the list should be "wrapped" using the Collections.synchronizedList method. This is best done at creation time, to prevent accidental unsynchronized access to the list:

    List list = Collections.synchronizedList(new LinkedList(...));
 
  • Constructor Details

    • SortedCollection

      Constructs a new, empty SortedCollection, sorted according to the keys' natural order. All keys inserted into the map must implement the Comparable interface. Furthermore, all such keys must be mutually comparable: k1.compareTo(k2) must not throw a ClassCastException for any elements k1 and k2 in the map. If the user attempts to put a key into the map that violates this constraint, methods that add elements will throw a ClassCastException.
    • SortedCollection

      Constructs a sorted list containing the elements of the specified collection. The elements are sorted according to the elements' natural order
      Parameters:
      c - the collection whose elements are to be placed into this list.
      Throws:
      NullPointerException - if the specified collection is null.
    • SortedCollection

      Constructs a new, empty SortedCollection, sorted according to the given comparator. All keys inserted into the map must be mutually comparable by the given comparator: comparator.compare(k1, k2) must not throw a ClassCastException for any keys k1 and k2 in the map. If the user attempts to put a key into the map that violates this constraint, methods that add elements will throw a ClassCastException.
      Parameters:
      c - the comparator that will be used to sort this map. A null value indicates that the keys' natural ordering should be used.
  • Method Details