Top Description Inners Fields Constructors Methods
org.apache.commons.collections4.list

public Class TreeList<E>

extends AbstractList<E>
Class Inheritance
Imports
java.util.AbstractList, .ArrayDeque, .Collection, .ConcurrentModificationException, .Deque, .Iterator, .ListIterator, .NoSuchElementException, org.apache.commons.collections4.OrderedIterator

A 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.

Since
3.1

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
TreeList.AVLNode<E>

Implements an AVLNode which keeps the offset updated.

pack-priv static class
TreeList.TreeListIterator<E>

A list iterator over the linked list.

Field Summary

Modifier and TypeField and Description
private TreeList.AVLNode<E>
root

The root node in the AVL tree

private int
size

The current size of the list

Inherited from java.util.AbstractList:
modCount

Constructor Summary

AccessConstructor and Description
public
TreeList()

Constructs a new empty list.

public
TreeList(final Collection<? extends E>
the collection to copy
coll
)

Constructs a new empty list that copies the specified collection.

Method Summary

Modifier and TypeMethod and Description
public void
add(final int
the index to add before
index
,
final E
the element to add
obj
)

Overrides java.util.AbstractList.add.

Implements java.util.List.add.

Adds a new element to the list.
public boolean

Returns:

true if this list changed as a result of the call
addAll
(final Collection<? extends E>
the collection to be added to this list
c
)

Overrides java.util.AbstractCollection.addAll.

Implements java.util.List.addAll, java.util.Collection.addAll.

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.
private void
checkInterval(final int
the index to check
index
,
final int
the first allowed index
startIndex
,
final int
the last allowed index
endIndex
)

Checks whether the index is valid.

public void
clear()

Overrides java.util.AbstractList.clear.

Implements java.util.List.clear, java.util.Collection.clear.

Clears the list, removing all entries.
public boolean

Returns:

true if the object is found
contains
(final Object
the object to check
object
)

Overrides java.util.AbstractCollection.contains.

Implements java.util.List.contains, java.util.Collection.contains.

Searches for the presence of an object in the list.
public E

Returns:

the element at the specified index
get
(final int
the index to retrieve
index
)

Implements abstract java.util.AbstractList.get.

Implements java.util.List.get.

Gets the element at the specified index.
public int

Returns:

the index of the object, -1 if not found
indexOf
(final Object
the object to search
object
)

Overrides java.util.AbstractList.indexOf.

Implements java.util.List.indexOf.

Searches for the index of an object in the list.
public Iterator<E>

Returns:

an iterator over the list
iterator
()

Overrides java.util.AbstractList.iterator.

Implements java.util.List.iterator, java.util.Collection.iterator.

Gets an iterator over the list.
public ListIterator<E>

Returns:

the new iterator
listIterator
()

Overrides java.util.AbstractList.listIterator.

Implements java.util.List.listIterator.

Gets a ListIterator over the list.
public ListIterator<E>

Returns:

the new iterator
listIterator
(final int
the index to start from
fromIndex
)

Overrides java.util.AbstractList.listIterator.

Implements java.util.List.listIterator.

Gets a ListIterator over the list.
public E

Returns:

the previous object at that index
remove
(final int
the index to remove
index
)

Overrides java.util.AbstractList.remove.

Implements java.util.List.remove.

Removes the element at the specified index.
public E

Returns:

the previous object at that index
set
(final int
the index to set
index
,
final E
the object to store at the specified index
obj
)

Overrides java.util.AbstractList.set.

Implements java.util.List.set.

Sets the element at the specified index.
public int

Returns:

the current size
size
()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.List.size, java.util.Collection.size.

Gets the current size of the list.
public Object[]

Returns:

the list as an array
toArray
()

Overrides java.util.AbstractCollection.toArray.

Implements java.util.List.toArray, java.util.Collection.toArray.

Converts the list into an array.
Inherited from java.util.AbstractList:
addaddAllequalshashCodelastIndexOfremoveRangesubList