public class TreeList extends AbstractList
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 sligtly more memory.
modCount
Constructor and Description |
---|
TreeList()
Constructs a new empty list.
|
TreeList(Collection coll)
Constructs a new empty list that copies the specified list.
|
Modifier and Type | Method and Description |
---|---|
void |
add(int index,
Object obj)
Adds a new element to the list.
|
void |
clear()
Clears the list, removing all entries.
|
boolean |
contains(Object object)
Searches for the presence of an object in the list.
|
Object |
get(int index)
Gets the element at the specified index.
|
int |
indexOf(Object 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.
|
Object |
remove(int index)
Removes the element at the specified index.
|
Object |
set(int index,
Object 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, subList
addAll, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toString
public TreeList()
public TreeList(Collection coll)
coll
- the collection to copyNullPointerException
- if the collection is nullpublic Object get(int index)
get
in interface List
get
in class AbstractList
index
- the index to retrievepublic int size()
size
in interface Collection
size
in interface List
size
in class AbstractCollection
public Iterator iterator()
iterator
in interface Iterable
iterator
in interface Collection
iterator
in interface List
iterator
in class AbstractList
public ListIterator listIterator()
listIterator
in interface List
listIterator
in class AbstractList
public ListIterator listIterator(int fromIndex)
listIterator
in interface List
listIterator
in class AbstractList
fromIndex
- the index to start frompublic int indexOf(Object object)
indexOf
in interface List
indexOf
in class AbstractList
public boolean contains(Object object)
contains
in interface Collection
contains
in interface List
contains
in class AbstractCollection
public Object[] toArray()
toArray
in interface Collection
toArray
in interface List
toArray
in class AbstractCollection
public void add(int index, Object obj)
add
in interface List
add
in class AbstractList
index
- the index to add beforeobj
- the element to addpublic Object set(int index, Object obj)
set
in interface List
set
in class AbstractList
index
- the index to setobj
- the object to store at the specified indexIndexOutOfBoundsException
- if the index is invalidpublic Object remove(int index)
remove
in interface List
remove
in class AbstractList
index
- the index to removepublic void clear()
clear
in interface Collection
clear
in interface List
clear
in class AbstractList
Copyright © 2001–2015 The Apache Software Foundation. All rights reserved.