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 theComparable
interface, a customComparator
must 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 class
SortedList.DefaultComparator
Default comparator that assumes that objects implement theComparable
interface.
-
Field Summary
Fields Modifier and Type Field Description protected Comparator
m_Comparator
the comparator to use.protected static Comparator
m_DefaultComparator
the default comparator.protected ArrayList<T>
m_List
the 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 void
add(int index, T element)
Adds the element at the specified position.boolean
add(T e)
Adds the element to the list.boolean
addAll(int index, Collection<? extends T> c)
Adds all the elements of the collection starting at the specified index.boolean
addAll(Collection<? extends T> c)
Adds all the elements of the collection to this list.void
clear()
Empties the list.Object
clone()
Creates a clone of this list.boolean
contains(Object o)
Checks whether the object is present.boolean
containsAll(Collection<?> c)
Checks whether all items of the collected are contained in the list.T
get(int index)
Returns the item at the specified position.Comparator
getComparator()
Returns the comparator in use.int
hashCode()
Returns the hashcode of the list.int
indexOf(Object o)
Returns the index of the object in the list.boolean
isEmpty()
Returns whether the list is empty.Iterator<T>
iterator()
Returns an iterator over the items in the list.int
lastIndexOf(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.T
remove(int index)
Removes the item at the specified position.boolean
remove(Object o)
Removes the object from the list.boolean
removeAll(Collection<?> c)
Removes all items from the collection in this list.boolean
retainAll(Collection<?> c)
Keeps all items present in the collection.T
set(int index, T element)
Sets the item at the specified position.int
size()
Returns the size of the list.protected void
sort()
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.String
toString()
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:
clone
in 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:
containsAll
in interfaceCollection<T>
- Specified by:
containsAll
in 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:
lastIndexOf
in 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:
listIterator
in 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:
listIterator
in interfaceList<T>
- Parameters:
index
- the starting index- Returns:
- the iterator
-
hashCode
public int hashCode()
Returns the hashcode of the list.- Specified by:
hashCode
in interfaceCollection<T>
- Specified by:
hashCode
in interfaceList<T>
- Overrides:
hashCode
in classObject
- Returns:
- the hashcode
- See Also:
ArrayList.hashCode()
-
toString
public String toString()
Returns the list as string.- Overrides:
toString
in classObject
- Returns:
- the string
- See Also:
AbstractCollection.toString()
-
-