Package adams.data
Class SortedList<T>
- java.lang.Object
-
- adams.data.SortedList<T>
-
- All Implemented Interfaces:
Serializable,Cloneable,Iterable<T>,Collection<T>,List<T>
public class SortedList<T> extends Object implements List<T>, Serializable, Cloneable
Provides an always sorted list. If list objects don't implement theComparableinterface, a customComparatormust be supplied.- Version:
- $Revision$
- Author:
- fracpete (fracpete at waikato dot ac dot nz)
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSortedList.DefaultComparatorDefault comparator that assumes that objects implement theComparableinterface.
-
Field Summary
Fields Modifier and Type Field Description protected Comparatorm_Comparatorthe comparator to use.protected static Comparatorm_DefaultComparatorthe default comparator.protected ArrayList<T>m_Listthe underlying list.
-
Constructor Summary
Constructors Constructor Description SortedList()Initializes the list.SortedList(int initialCapacity)Initializes the list.SortedList(int initialCapacity, Comparator comp)Initializes the list.SortedList(Collection<? extends T> c)Initializes the list.SortedList(Collection<? extends T> c, Comparator comp)Initializes the list.SortedList(Comparator comp)Initializes the list.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(int index, T element)Adds the element at the specified position.booleanadd(T e)Adds the element to the list.booleanaddAll(int index, Collection<? extends T> c)Adds all the elements of the collection starting at the specified index.booleanaddAll(Collection<? extends T> c)Adds all the elements of the collection to this list.voidclear()Empties the list.Objectclone()Creates a clone of this list.booleancontains(Object o)Checks whether the object is present.booleancontainsAll(Collection<?> c)Checks whether all items of the collected are contained in the list.Tget(int index)Returns the item at the specified position.ComparatorgetComparator()Returns the comparator in use.inthashCode()Returns the hashcode of the list.intindexOf(Object o)Returns the index of the object in the list.booleanisEmpty()Returns whether the list is empty.Iterator<T>iterator()Returns an iterator over the items in the list.intlastIndexOf(Object o)Returns the last index for the object in this list.ListIterator<T>listIterator()Returns a list iterator over the items.ListIterator<T>listIterator(int index)Returns a list iterator over the items starting at the position.Tremove(int index)Removes the item at the specified position.booleanremove(Object o)Removes the object from the list.booleanremoveAll(Collection<?> c)Removes all items from the collection in this list.booleanretainAll(Collection<?> c)Keeps all items present in the collection.Tset(int index, T element)Sets the item at the specified position.intsize()Returns the size of the list.protected voidsort()Sorts the list.List<T>subList(int fromIndex, int toIndex)Returns a sublist.Object[]toArray()Returns the list as array.<T> T[]toArray(T[] a)Returns the list as array.StringtoString()Returns the list as string.-
Methods inherited from class java.lang.Object
equals, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
-
Methods inherited from interface java.util.List
equals, replaceAll, sort, spliterator
-
-
-
-
Field Detail
-
m_Comparator
protected Comparator m_Comparator
the comparator to use.
-
m_DefaultComparator
protected static Comparator m_DefaultComparator
the default comparator.
-
-
Constructor Detail
-
SortedList
public SortedList()
Initializes the list. Uses theSortedList.DefaultComparator.
-
SortedList
public SortedList(Comparator comp)
Initializes the list. Uses the specifiedComparator.- Parameters:
comp- the comparator to use
-
SortedList
public SortedList(int initialCapacity)
Initializes the list. Uses theSortedList.DefaultComparator.- Parameters:
initialCapacity- the initial capacity of the list
-
SortedList
public SortedList(int initialCapacity, Comparator comp)Initializes the list. Uses the specifiedComparator.- Parameters:
initialCapacity- the initial capacity of the listcomp- the comparator to use
-
SortedList
public SortedList(Collection<? extends T> c)
Initializes the list. Uses theSortedList.DefaultComparator.- Parameters:
c- the data to populate the list with
-
SortedList
public SortedList(Collection<? extends T> c, Comparator comp)
Initializes the list. Uses the specifiedComparator.- Parameters:
c- the data to populate the list withcomp- the comparator to use
-
-
Method Detail
-
getComparator
public Comparator getComparator()
Returns the comparator in use.- Returns:
- the comparator
-
sort
protected void sort()
Sorts the list.
-
clone
public Object clone() throws CloneNotSupportedException
Creates a clone of this list.- Overrides:
clonein classObject- Returns:
- the clone
- Throws:
CloneNotSupportedException- does never occur
-
size
public int size()
Returns the size of the list.
-
isEmpty
public boolean isEmpty()
Returns whether the list is empty.
-
contains
public boolean contains(Object o)
Checks whether the object is present.
-
toArray
public Object[] toArray()
Returns the list as array.
-
toArray
public <T> T[] toArray(T[] a)
Returns the list as array.
-
add
public boolean add(T e)
Adds the element to the list.
-
remove
public boolean remove(Object o)
Removes the object from the list.
-
containsAll
public boolean containsAll(Collection<?> c)
Checks whether all items of the collected are contained in the list.- Specified by:
containsAllin interfaceCollection<T>- Specified by:
containsAllin interfaceList<T>- Parameters:
c- the collection to check- Returns:
- true if all contained
-
addAll
public boolean addAll(Collection<? extends T> c)
Adds all the elements of the collection to this list.
-
removeAll
public boolean removeAll(Collection<?> c)
Removes all items from the collection in this list.
-
retainAll
public boolean retainAll(Collection<?> c)
Keeps all items present in the collection.
-
clear
public void clear()
Empties the list.
-
addAll
public boolean addAll(int index, Collection<? extends T> c)Adds all the elements of the collection starting at the specified index. NB: triggers a complete sort!
-
get
public T get(int index)
Returns the item at the specified position.
-
set
public T set(int index, T element)
Sets the item at the specified position. NB: expensive as it triggers a complete sort!
-
add
public void add(int index, T element)Adds the element at the specified position. NB: triggers a complete sort!
-
remove
public T remove(int index)
Removes the item at the specified position.
-
indexOf
public int indexOf(Object o)
Returns the index of the object in the list. Uses fast binary search for locating the object.
-
lastIndexOf
public int lastIndexOf(Object o)
Returns the last index for the object in this list.- Specified by:
lastIndexOfin interfaceList<T>- Parameters:
o- the object to locate- Returns:
- the index, -1 if not found
-
listIterator
public ListIterator<T> listIterator()
Returns a list iterator over the items.- Specified by:
listIteratorin interfaceList<T>- Returns:
- the iterator
-
listIterator
public ListIterator<T> listIterator(int index)
Returns a list iterator over the items starting at the position.- Specified by:
listIteratorin interfaceList<T>- Parameters:
index- the starting index- Returns:
- the iterator
-
hashCode
public int hashCode()
Returns the hashcode of the list.- Specified by:
hashCodein interfaceCollection<T>- Specified by:
hashCodein interfaceList<T>- Overrides:
hashCodein classObject- Returns:
- the hashcode
- See Also:
ArrayList.hashCode()
-
toString
public String toString()
Returns the list as string.- Overrides:
toStringin classObject- Returns:
- the string
- See Also:
AbstractCollection.toString()
-
-