Class TreeList
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractList
-
- org.apache.commons.collections.list.TreeList
-
- All Implemented Interfaces:
java.lang.Iterable,java.util.Collection,java.util.List
public class TreeList extends java.util.AbstractListAListimplementation 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
ArrayListand aLinkedListwhere 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 325ArrayListis a good general purpose list implementation. It is faster thanTreeListfor most operations except inserting and removing in the middle of the list.ArrayListalso uses less memory asTreeListuses one object per entry.LinkedListis rarely a good choice of implementation.TreeListis almost always a good replacement for it, although it does use sligtly more memory.- Since:
- Commons Collections 3.1
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(int index, java.lang.Object obj)Adds a new element to the list.voidclear()Clears the list, removing all entries.booleancontains(java.lang.Object object)Searches for the presence of an object in the list.java.lang.Objectget(int index)Gets the element at the specified index.intindexOf(java.lang.Object object)Searches for the index of an object in the list.java.util.Iteratoriterator()Gets an iterator over the list.java.util.ListIteratorlistIterator()Gets a ListIterator over the list.java.util.ListIteratorlistIterator(int fromIndex)Gets a ListIterator over the list.java.lang.Objectremove(int index)Removes the element at the specified index.java.lang.Objectset(int index, java.lang.Object obj)Sets the element at the specified index.intsize()Gets the current size of the list.java.lang.Object[]toArray()Converts the list into an array.-
Methods inherited from class java.util.AbstractList
add, addAll, equals, hashCode, lastIndexOf, subList
-
-
-
-
Method Detail
-
get
public java.lang.Object get(int index)
Gets the element at the specified index.- Specified by:
getin interfacejava.util.List- Specified by:
getin classjava.util.AbstractList- Parameters:
index- the index to retrieve- Returns:
- the element at the specified index
-
size
public int size()
Gets the current size of the list.- Specified by:
sizein interfacejava.util.Collection- Specified by:
sizein interfacejava.util.List- Specified by:
sizein classjava.util.AbstractCollection- Returns:
- the current size
-
iterator
public java.util.Iterator iterator()
Gets an iterator over the list.- Specified by:
iteratorin interfacejava.util.Collection- Specified by:
iteratorin interfacejava.lang.Iterable- Specified by:
iteratorin interfacejava.util.List- Overrides:
iteratorin classjava.util.AbstractList- Returns:
- an iterator over the list
-
listIterator
public java.util.ListIterator listIterator()
Gets a ListIterator over the list.- Specified by:
listIteratorin interfacejava.util.List- Overrides:
listIteratorin classjava.util.AbstractList- Returns:
- the new iterator
-
listIterator
public java.util.ListIterator listIterator(int fromIndex)
Gets a ListIterator over the list.- Specified by:
listIteratorin interfacejava.util.List- Overrides:
listIteratorin classjava.util.AbstractList- Parameters:
fromIndex- the index to start from- Returns:
- the new iterator
-
indexOf
public int indexOf(java.lang.Object object)
Searches for the index of an object in the list.- Specified by:
indexOfin interfacejava.util.List- Overrides:
indexOfin classjava.util.AbstractList- Returns:
- the index of the object, -1 if not found
-
contains
public boolean contains(java.lang.Object object)
Searches for the presence of an object in the list.- Specified by:
containsin interfacejava.util.Collection- Specified by:
containsin interfacejava.util.List- Overrides:
containsin classjava.util.AbstractCollection- Returns:
- true if the object is found
-
toArray
public java.lang.Object[] toArray()
Converts the list into an array.- Specified by:
toArrayin interfacejava.util.Collection- Specified by:
toArrayin interfacejava.util.List- Overrides:
toArrayin classjava.util.AbstractCollection- Returns:
- the list as an array
-
add
public void add(int index, java.lang.Object obj)Adds a new element to the list.- Specified by:
addin interfacejava.util.List- Overrides:
addin classjava.util.AbstractList- Parameters:
index- the index to add beforeobj- the element to add
-
set
public java.lang.Object set(int index, java.lang.Object obj)Sets the element at the specified index.- Specified by:
setin interfacejava.util.List- Overrides:
setin classjava.util.AbstractList- Parameters:
index- the index to setobj- the object to store at the specified index- Returns:
- the previous object at that index
- Throws:
java.lang.IndexOutOfBoundsException- if the index is invalid
-
remove
public java.lang.Object remove(int index)
Removes the element at the specified index.- Specified by:
removein interfacejava.util.List- Overrides:
removein classjava.util.AbstractList- Parameters:
index- the index to remove- Returns:
- the previous object at that index
-
clear
public void clear()
Clears the list, removing all entries.- Specified by:
clearin interfacejava.util.Collection- Specified by:
clearin interfacejava.util.List- Overrides:
clearin classjava.util.AbstractList
-
-