public class SortedCollection extends AbstractCollection
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 and Description |
---|
SortedCollection()
Constructs a new, empty SortedCollection, sorted according to the keys' natural
order.
|
SortedCollection(Collection c)
Constructs a sorted list containing the elements of the specified
collection.
|
SortedCollection(Comparator c)
Constructs a new, empty SortedCollection, sorted according to the given comparator.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(Object obj) |
Iterator |
iterator() |
static void |
main(String[] args) |
int |
size() |
String |
toString() |
addAll, clear, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
equals, hashCode, parallelStream, removeIf, spliterator, stream
public SortedCollection()
public SortedCollection(Collection c)
c
- the collection whose elements are to be placed into this list.NullPointerException
- if the specified collection is null.public SortedCollection(Comparator c)
c
- the comparator that will be used to sort this map. A null value
indicates that the keys' natural ordering should be used.public boolean add(Object obj)
add
in interface Collection
add
in class AbstractCollection
public Iterator iterator()
iterator
in interface Iterable
iterator
in interface Collection
iterator
in class AbstractCollection
public int size()
size
in interface Collection
size
in class AbstractCollection
public String toString()
toString
in class AbstractCollection