org.apache.commons.collections.list

Class TreeList


public class TreeList
extends AbstractList

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 sligtly more memory.

Version:
$Revision: 1.3 $ $Date: 2004/05/26 21:56:05 $
Authors:
Joerg Schmuecker
Stephen Colebourne
Since:
Commons Collections 3.1

Constructor Summary

TreeList()
Constructs a new empty list.
TreeList(Collection coll)
Constructs a new empty list that copies the specified list.

Method Summary

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.

Constructor Details

TreeList

public TreeList()
Constructs a new empty list.

TreeList

public TreeList(Collection coll)
Constructs a new empty list that copies the specified list.
Parameters:
coll - the collection to copy

Method Details

add

public void add(int index,
                Object obj)
Adds a new element to the list.
Parameters:
index - the index to add before
obj - the element to add

clear

public void clear()
Clears the list, removing all entries.

contains

public boolean contains(Object object)
Searches for the presence of an object in the list.
Returns:
true if the object is found

get

public Object get(int index)
Gets the element at the specified index.
Parameters:
index - the index to retrieve
Returns:
the element at the specified index

indexOf

public int indexOf(Object object)
Searches for the index of an object in the list.
Returns:
the index of the object, -1 if not found

iterator

public Iterator iterator()
Gets an iterator over the list.
Returns:
an iterator over the list

listIterator

public ListIterator listIterator()
Gets a ListIterator over the list.
Returns:
the new iterator

listIterator

public ListIterator listIterator(int fromIndex)
Gets a ListIterator over the list.
Parameters:
fromIndex - the index to start from
Returns:
the new iterator

remove

public Object remove(int index)
Removes the element at the specified index.
Parameters:
index - the index to remove
Returns:
the previous object at that index

set

public Object set(int index,
                  Object obj)
Sets the element at the specified index.
Parameters:
index - the index to set
obj - the object to store at the specified index
Returns:
the previous object at that index

size

public int size()
Gets the current size of the list.
Returns:
the current size

toArray

public Object[] toArray()
Converts the list into an array.
Returns:
the list as an array

Copyright © 2001-2006 Apache Software Foundation. All Rights Reserved.