public class TreeList<E>
extends java.util.AbstractList<E>
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:
boolean
addAll
(java.util.Collection<? extends
E
> c)
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.
clear
()
Clears the list, removing all entries.
boolean
contains
(java.lang.Object object)
Searches for the presence of an object in the list.
get
(int index)
Gets the element at the specified index.
indexOf
(java.lang.Object object)
Searches for the index of an object in the list.
java.util.Iterator<
E
>
iterator
()
Gets an iterator over the list.
java.util.ListIterator<
E
>
listIterator
()
Gets a ListIterator over the list.
java.util.ListIterator<
E
>
listIterator
(int fromIndex)
Gets a ListIterator over the list.
remove
(int index)
Removes the element at the specified index.
set
(int index,
E
obj)
Sets the element at the specified index.
size
()
Gets the current size of the list.
java.lang.Object[]
toArray
()
Converts the list into an array.
TreeList
public TreeList(java.util.Collection<? extends E> coll)
Constructs a new empty list that copies the specified collection.
Parameters:
coll
- the collection to copy
Throws:
java.lang.NullPointerException
- if the collection is null
size
in interface
java.util.Collection<
E
>
Specified by:
size
in interface
java.util.List<
E
>
Specified by:
size
in class
java.util.AbstractCollection<
E
>
Returns:
the current size
iterator
in interface
java.lang.Iterable<
E
>
Specified by:
iterator
in interface
java.util.Collection<
E
>
Specified by:
iterator
in interface
java.util.List<
E
>
Overrides:
iterator
in class
java.util.AbstractList<
E
>
Returns:
an iterator over the list
listIterator
public java.util.ListIterator<E> listIterator()
Gets a ListIterator over the list.
Specified by:
listIterator
in interface
java.util.List<
E
>
Overrides:
listIterator
in class
java.util.AbstractList<
E
>
Returns:
the new iterator
listIterator
public java.util.ListIterator<E> listIterator(int fromIndex)
Gets a ListIterator over the list.
Specified by:
listIterator
in interface
java.util.List<
E
>
Overrides:
listIterator
in class
java.util.AbstractList<
E
>
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:
indexOf
in interface
java.util.List<
E
>
Overrides:
indexOf
in class
java.util.AbstractList<
E
>
Parameters:
object
- the object to search
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:
contains
in interface
java.util.Collection<
E
>
Specified by:
contains
in interface
java.util.List<
E
>
Overrides:
contains
in class
java.util.AbstractCollection<
E
>
Parameters:
object
- the object to check
Returns:
true if the object is found
toArray
in interface
java.util.Collection<
E
>
Specified by:
toArray
in interface
java.util.List<
E
>
Overrides:
toArray
in class
java.util.AbstractCollection<
E
>
Returns:
the list as an array
addAll
public boolean addAll(java.util.Collection<? extends E> c)
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.
This method runs in O(n + log m) time, where m is
the size of this list and n is the size of
c
.
Specified by:
addAll
in interface
java.util.Collection<
E
>
Specified by:
addAll
in interface
java.util.List<
E
>
Overrides:
addAll
in class
java.util.AbstractCollection<
E
>
Parameters:
c
- the collection to be added to this list
Returns:
true
if this list changed as a result of the call
Throws:
java.lang.NullPointerException
obj
- 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
in interface
java.util.List<
E
>
Overrides:
remove
in class
java.util.AbstractList<
E
>
Parameters:
index
- the index to remove
Returns:
the previous object at that index
clear
in interface
java.util.Collection<
E
>
Specified by:
clear
in interface
java.util.List<
E
>
Overrides:
clear
in class
java.util.AbstractList<
E
>