fc.util
Class SortedCollection
java.lang.Object
java.util.AbstractCollection
fc.util.SortedCollection
- All Implemented Interfaces:
- java.lang.Iterable, java.util.Collection
public class SortedCollection
- extends java.util.AbstractCollection
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 Summary |
SortedCollection()
Constructs a new, empty SortedCollection, sorted according to the keys' natural
order. |
SortedCollection(java.util.Collection c)
Constructs a sorted list containing the elements of the specified
collection. |
SortedCollection(java.util.Comparator c)
Constructs a new, empty SortedCollection, sorted according to the given comparator. |
Method Summary |
boolean |
add(java.lang.Object obj)
|
java.util.Iterator |
iterator()
|
static void |
main(java.lang.String[] args)
|
int |
size()
|
java.lang.String |
toString()
|
Methods inherited from class java.util.AbstractCollection |
addAll, clear, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray |
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Methods inherited from interface java.util.Collection |
equals, hashCode |
SortedCollection
public 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
public SortedCollection(java.util.Collection c)
- 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:
java.lang.NullPointerException
- if the specified collection is null.
SortedCollection
public SortedCollection(java.util.Comparator c)
- 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.
add
public boolean add(java.lang.Object obj)
- Specified by:
add
in interface java.util.Collection
- Overrides:
add
in class java.util.AbstractCollection
iterator
public java.util.Iterator iterator()
- Specified by:
iterator
in interface java.lang.Iterable
- Specified by:
iterator
in interface java.util.Collection
- Specified by:
iterator
in class java.util.AbstractCollection
size
public int size()
- Specified by:
size
in interface java.util.Collection
- Specified by:
size
in class java.util.AbstractCollection
toString
public java.lang.String toString()
- Overrides:
toString
in class java.util.AbstractCollection
main
public static void main(java.lang.String[] args)
throws java.lang.Exception
- Throws:
java.lang.Exception