E
- type of elements stored in the listpublic class GapList<E> extends IList<E>
ArrayList
and LinkedList
.
It is implemented to offer both efficient random access to elements
by index (as ArrayList does) and at the same time efficient adding
and removing elements to and from beginning and end (as LinkedList does).
It also exploits the locality of reference often seen in applications
to further improve performance, e.g. for iterating over the list.
The class can be used as drop-in replacement for ArrayList.
It is also source compatible to LinkedList/Deque as it implements all needed methods.
It cannot implement Deque
however, use IList.asDeque()
to get a view implementing it.
Note that this implementation is not synchronized.
Note that the iterators provided are not fail-fast.
List
,
ArrayList
,
Serialized FormModifier and Type | Class and Description |
---|---|
protected static class |
GapList.ReadOnlyList<E>
A read-only version of
GapList . |
IList.IListableFromArray<E>, IList.IListableFromCollection<E>, IList.IListableFromList<E>, IList.IListableFromMult<E>
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_CAPACITY
Default capacity for list
|
Modifier | Constructor and Description |
---|---|
|
GapList()
Construct a list with the default initial capacity.
|
protected |
GapList(boolean copy,
GapList<E> that)
Constructor used internally, e.g. for ImmutableGapList.
|
|
GapList(java.util.Collection<? extends E> coll)
Construct a list to contain the specified elements.
|
|
GapList(int capacity)
Construct a list with specified initial capacity.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(E elem) |
void |
add(int index,
E elem)
Inserts the specified element at the specified position in this list.
|
<K> int |
binarySearch(int index,
int len,
K key,
java.util.Comparator<? super K> comparator)
Searches the specified range for an object using the binary search algorithm.
|
int |
capacity()
Returns capacity of this list.
|
GapList<E> |
clone()
Returns a shallow copy of this list.
|
GapList<E> |
copy()
Returns a shallow copy of this list.
|
static <E> GapList<E> |
create()
Create new list.
|
static <E> GapList<E> |
create(java.util.Collection<? extends E> coll)
Create new list with specified elements.
|
static <E> GapList<E> |
create(E... elems)
Create new list with specified elements.
|
GapList<E> |
crop()
Returns a copy this list but without elements.
|
protected boolean |
doAdd(int index,
E elem)
Helper method for adding an element to the list.
|
protected void |
doAssign(IList<E> that)
Assign this list the content of the that list.
|
protected void |
doClear() |
protected void |
doClone(IList<E> that)
Initialize this object after the bitwise copy has been made by Object.clone().
|
GapList<E> |
doCreate(int capacity)
Create list with specified capacity.
|
protected void |
doEnsureCapacity(int minCapacity)
Increases the capacity of this list instance, if
necessary, to ensure that it can hold at least the number of elements
specified by the minimum capacity argument.
|
protected E |
doGet(int index)
Helper method for getting an element from the list.
|
protected <T> void |
doGetAll(T[] array,
int index,
int len)
Helper method to fill the specified elements in an array.
|
protected E |
doRemove(int index)
Helper method to remove an element.
|
protected void |
doRemoveAll(int index,
int len)
Remove specified range of elements from list.
|
protected boolean |
doRemoveAllFast(int index,
int len)
Remove specified range of elements from list as specialized fast operation.
|
protected E |
doReSet(int index,
E elem)
Sets an element at specified position.
|
protected E |
doSet(int index,
E elem)
Helper method for setting an element in the list.
|
static <EE> GapList<EE> |
EMPTY() |
E |
get(int index)
Return element at specified position
|
GapList<E> |
getAll(E elem)
Returns all elements in the list equal to the specified element.
|
GapList<E> |
getAll(int index,
int len)
Returns specified range of elements from list.
|
E |
getDefaultElem() |
static <E> GapList<E> |
immutable(java.util.Collection<? extends E> coll)
Create new immutable list with specified elements.
|
static <E> GapList<E> |
immutable(E... elems)
Create new immutable list with specified elements.
|
GapList<E> |
immutableList()
Returns an immutable copy of this list.
|
boolean |
isReadOnly()
Returns true if this list is either unmodifiable or immutable, false otherwise.
|
<R> GapList<R> |
map(java.util.function.Function<E,R> mapper)
Create a new list by applying the specified mapping function to all elements.
|
E |
remove(int index) |
int |
size()
Return size of list
|
void |
sort(int index,
int len,
java.util.Comparator<? super E> comparator)
Sort specified elements in the list using the specified comparator.
|
void |
trimToSize()
Trims the capacity of this GapList instance to be the list's current size.
|
GapList<E> |
unmodifiableList()
Returns an unmodifiable view of this list.
|
addAll, addAll, addAll, addAll, addArray, addArray, addArray, addArray, addFirst, addIfAbsent, addLast, addMult, addMult, asDeque, asIListable, binarySearch, checkIndex, checkIndexAdd, checkLength, checkLengths, checkNonNull, checkRange, clear, contains, containsAll, containsAny, containsIf, copy, count, countIf, descendingIterator, doAddAll, doCreateArray, doIndexOf, doIndexOfIf, doInitAll, doLastIndexOf, doLastIndexOfIf, doModify, doPutAll, doReplace, doRotate, drag, element, ensureCapacity, equals, extract, extractIf, fill, filter, filterMap, flatMap, getDistinct, getFirst, getFirstOrNull, getIf, getLast, getLastOrNull, getSingle, getSingleOrNull, hashCode, indexOf, indexOf, indexOfIf, indexOfIf, initAll, initAll, initArray, initMult, isEmpty, iterator, lastIndexOf, lastIndexOf, lastIndexOfIf, lastIndexOfIf, listIterator, listIterator, mapFilter, move, offer, offerFirst, offerLast, peek, peekFirst, peekLast, poll, pollFirst, pollLast, pop, push, put, putAll, putAll, putArray, putMult, remove, remove, remove, removeAll, removeAll, removeAll, removeFirst, removeFirstOccurrence, removeIf, removeLast, removeLastOccurrence, replace, replaceAll, replaceAll, replaceArray, replaceMult, resize, retain, retainAll, retainAll, retainIf, reverse, reverse, rotate, rotate, set, setAll, setAll, setArray, setArray, setMult, sort, swap, toArray, toArray, toArray, toArray, toArray, toArray, toString, transferCopy, transferMove, transferRemove, transferSwap, transform, transformedList
public static final int DEFAULT_CAPACITY
protected GapList(boolean copy, GapList<E> that)
copy
- true to copy all instance values from source, if false nothing is donethat
- list to copypublic GapList()
public GapList(int capacity)
capacity
- capacitypublic GapList(java.util.Collection<? extends E> coll)
coll
- collection with elementspublic static <EE> GapList<EE> EMPTY()
public static <E> GapList<E> create()
E
- type of elements stored in the listpublic static <E> GapList<E> create(java.util.Collection<? extends E> coll)
E
- type of elements stored in the listcoll
- collection with elements@SafeVarargs public static <E> GapList<E> create(E... elems)
E
- type of elements stored in the listelems
- array with elementspublic static <E> GapList<E> immutable(java.util.Collection<? extends E> coll)
E
- type of elements stored in the listcoll
- collection with elements@SafeVarargs public static <E> GapList<E> immutable(E... elems)
E
- type of elements stored in the listelems
- array with elementsprotected void doAssign(IList<E> that)
IList
public E getDefaultElem()
getDefaultElem
in class IList<E>
public GapList<E> crop()
IList
public GapList<E> copy()
IList
public GapList<E> clone()
IList
IList.copy()
to .public boolean isReadOnly()
IList
isReadOnly
in class IList<E>
public GapList<E> unmodifiableList()
IList
IList.immutableList()
to get an immutable copy of a list.unmodifiableList
in class IList<E>
public GapList<E> immutableList()
IList
IList.unmodifiableList()
to get unmodifiable view of a list.immutableList
in class IList<E>
protected void doClone(IList<E> that)
IList
public int size()
IListable
public int capacity()
IList
public E get(int index)
IListable
protected E doGet(int index)
IList
protected E doSet(int index, E elem)
IList
protected E doReSet(int index, E elem)
IList
public boolean add(E elem)
public void add(int index, E elem)
IList
public GapList<E> getAll(int index, int len)
IList
public GapList<E> getAll(E elem)
IList
public <R> GapList<R> map(java.util.function.Function<E,R> mapper)
ICollection
protected boolean doAdd(int index, E elem)
IList
public E remove(int index)
protected E doRemove(int index)
IList
protected void doEnsureCapacity(int minCapacity)
IList
doEnsureCapacity
in class IList<E>
minCapacity
- the desired minimum capacitypublic void trimToSize()
trimToSize
in class IList<E>
protected <T> void doGetAll(T[] array, int index, int len)
IList
public GapList<E> doCreate(int capacity)
IList
protected void doRemoveAll(int index, int len)
IList
doRemoveAll
in class IList<E>
index
- index of first element to removelen
- number of elements to removeprotected boolean doRemoveAllFast(int index, int len)
index
- index of first element to removelen
- number of elements to removepublic void sort(int index, int len, java.util.Comparator<? super E> comparator)
IList
public <K> int binarySearch(int index, int len, K key, java.util.Comparator<? super K> comparator)
IList
Note that the method is defined to work with an arbitrary type <K>. This allows to search directly for a key field in the object without the need to construct an object containing the key:
persons.binarySearch("john", new SearchByName());
class SearchByName implements Comparator<Object> {
public int compare(Object o1, Object o2) {
String s1 = (o1 instanceof String) ? (String) o1 : ((Name) o1).getName();
String s2 = (o2 instanceof String) ? (String) o2 : ((Name) o2).getName();
return s1.compareTo(s2);
}
}
/
binarySearch
in class IList<E>
index
- index of first element to searchlen
- number of elements to searchkey
- the value to be searched forcomparator
- the comparator by which the list is ordered.
A null value indicates that the elements'
natural ordering should be used.Arrays.binarySearch(long[], long)