public class SortedList<E> extends Object implements List<E>, RandomAccess
ArrayList
as
the underlying collection so we can support RandomAccess. All mutations
create a new copy of the ArrayList
instance, so can be
expensive. This class is only intended for use on small, very rarely
written collections that expect highly concurrent reads.
Read operations are performed on a reference to the internal list at the time of invocation, so will not see any mutations to the collection during their operation. Iterating over list elements manually using the RandomAccess pattern involves multiple operations. For this to be safe get a reference to the internal list first using get().
If constructed with a Comparator
, the list will be sorted
using the comparator. Adding or changing an element using an index will
trigger a resort.
Iterators are read-only. They cannot be used to remove elements.
Modifier and Type | Field and Description |
---|---|
private Comparator<? super E> |
comparator |
private List<E> |
list |
Constructor and Description |
---|
SortedList(Collection<? extends E> c,
Comparator<? super E> comparator)
Constructs a list containing the elements of the given collection, in the
order returned by the collection's iterator, that will be sorted with the
given comparator.
|
SortedList(Comparator<? super E> comparator)
Constructs an empty list with the default initial capacity that will be
sorted using the given comparator.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(E e) |
void |
add(int index,
E element) |
boolean |
addAll(Collection<? extends E> c) |
boolean |
addAll(int index,
Collection<? extends E> c) |
void |
clear() |
boolean |
contains(Object o) |
boolean |
containsAll(Collection<?> c) |
List<E> |
get()
Returns a reference to the unmodifiable list currently backing the SortedList.
|
E |
get(int index) |
int |
indexOf(Object o) |
boolean |
isEmpty() |
Iterator<E> |
iterator() |
int |
lastIndexOf(Object o) |
ListIterator<E> |
listIterator() |
ListIterator<E> |
listIterator(int index) |
E |
remove(int index) |
boolean |
remove(Object o) |
boolean |
removeAll(Collection<?> c) |
boolean |
retainAll(Collection<?> c) |
E |
set(int index,
E element) |
int |
size() |
List<E> |
subList(int fromIndex,
int toIndex) |
Object[] |
toArray() |
<T> T[] |
toArray(T[] a) |
private final Comparator<? super E> comparator
public SortedList(Comparator<? super E> comparator)
comparator
- the comparatorpublic SortedList(Collection<? extends E> c, Comparator<? super E> comparator)
c
- the collectioncomparator
- the comparatorpublic List<E> get()
public int size()
public boolean isEmpty()
public boolean contains(Object o)
public Object[] toArray()
public <T> T[] toArray(T[] a)
public boolean add(E e)
public boolean remove(Object o)
public boolean containsAll(Collection<?> c)
containsAll
in interface Collection<E>
containsAll
in interface List<E>
public boolean addAll(Collection<? extends E> c)
public boolean addAll(int index, Collection<? extends E> c)
public boolean removeAll(Collection<?> c)
public boolean retainAll(Collection<?> c)
public void clear()
public int lastIndexOf(Object o)
lastIndexOf
in interface List<E>
public ListIterator<E> listIterator()
listIterator
in interface List<E>
public ListIterator<E> listIterator(int index)
listIterator
in interface List<E>
Copyright © 2007–2019 The Apache Software Foundation. All rights reserved.