public class TreeList<E> extends AbstractList<E>
List implementation that is optimised for fast insertions and removals at any index in the list.
This list implementation utilises a tree structure internally to ensure that all insertions and removals are O(log n). This provides much faster performance than both an ArrayList and a LinkedList where elements are inserted and removed repeatedly from anywhere in the list.
The following relative performance statistics are indicative of this class:
get add insert iterate remove
TreeList 3 5 1 2 1
ArrayList 1 1 40 1 40
LinkedList 5800 1 350 2 325
ArrayList is a good general purpose list implementation. It is faster than
TreeList for most operations except inserting and removing in the middle of the list.
ArrayList also uses less memory as
TreeList uses one object per entry.
LinkedList is rarely a good choice of implementation. TreeList is almost always a good replacement for it, although it does use slightly more memory.
modCount| Constructor and Description |
|---|
TreeList()
Constructs a new empty list.
|
TreeList(Collection
Constructs a new empty list that copies the specified collection.
|
| Modifier and Type | Method and Description |
|---|---|
void |
add(int index, E obj)
Adds a new element to the list.
|
boolean |
addAll(Collection
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator.
|
void |
clear()
Clears the list, removing all entries.
|
boolean |
contains(Object
Searches for the presence of an object in the list.
|
E |
get(int index)
Gets the element at the specified index.
|
int |
indexOf(Object
Searches for the index of an object in the list.
|
Iterator |
iterator()
Gets an iterator over the list.
|
ListIterator |
listIterator()
Gets a ListIterator over the list.
|
ListIterator |
listIterator(int fromIndex)
Gets a ListIterator over the list.
|
E |
remove(int index)
Removes the element at the specified index.
|
E |
set(int index, E obj)
Sets the element at the specified index.
|
int |
size()
Gets the current size of the list.
|
Object |
toArray()
Converts the list into an array.
|
add, addAll, equals, hashCode, lastIndexOf, removeRange, subListcontainsAll, isEmpty, remove, removeAll, retainAll, toArray, toStringclone, finalize, getClass, notify, notifyAll, wait, wait, waitcontainsAll, isEmpty, remove, removeAll, replaceAll, retainAll, sort, spliterator, toArrayparallelStream, removeIf, streampublic TreeList()
public TreeList(Collection<? extends E> coll)
coll - the collection to copy
NullPointerException - if the collection is null
public E get(int index)
public int size()
size in interface
Collection<E>
size in interface
List<E>
size in class
AbstractCollection<E>
public ListIterator<E> listIterator()
listIterator in interface
List<E>
listIterator in class
AbstractList<E>
public ListIterator<E> listIterator(int fromIndex)
listIterator in interface
List<E>
listIterator in class
AbstractList<E>
fromIndex - the index to start from
public int indexOf(Objectobject)
public boolean contains(Objectobject)
contains in interface
Collection<E>
contains in interface
List<E>
contains in class
AbstractCollection<E>
object - the object to check
public Object[] toArray()
toArray in interface
Collection<E>
toArray in interface
List<E>
toArray in class
AbstractCollection<E>
public void add(int index,
E obj)
public boolean addAll(Collection<? extends E> c)
This method runs in O(n + log m) time, where m is the size of this list and n is the size of c.
addAll in interface
Collection<E>
addAll in interface
List<E>
addAll in class
AbstractCollection<E>
c - the collection to be added to this list
true if this list changed as a result of the call
NullPointerException -
public E set(int index, E obj)
set in interface
List<E>
set in class
AbstractList<E>
index - the index to set
obj - the object to store at the specified index
IndexOutOfBoundsException - if the index is invalid
public E remove(int index)
public void clear()