org.omnaest.utils.structure.collection.list.sorted
Class TreeList<E>

java.lang.Object
  extended by org.omnaest.utils.structure.collection.CollectionAbstract<E>
      extended by org.omnaest.utils.structure.collection.list.ListAbstract<E>
          extended by org.omnaest.utils.structure.collection.list.sorted.SortedListAbstract<E>
              extended by org.omnaest.utils.structure.collection.list.sorted.TreeList<E>
Type Parameters:
E -
All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, Deque<E>, List<E>, Queue<E>, SortedList<E>, SortedSplitableList<E>

public class TreeList<E>
extends SortedListAbstract<E>

This sorted List implementation does use an internal TreeMap to provide all features a SortedSet has, except the fact that duplicate elements are allowed.

But be aware, that the order of elements, which are considered equal due to the Comparator.compare(Object, Object) method, is not determined.

The TreeList has a quite fast add(Object) method which uses the advantages of the underlying TreeMap. All index based operations like get(int) or remove(int) are slow for large Lists with many different elements, since all elements of the TreeMap have to be traversed to identify the right index position.

The TreeList does break the contract of the List.add(Object), as well as the List.add(int, Object) in the way, that it does insert new elements at the appropriate sort position.


The listIterator() does currently not support ListIterator.nextIndex() and ListIterator.previousIndex() and throws UnsupportedOperationException in the case calling those methods.

Performance is about 2x-3x slower compared to a TreeSet and quite similar to the performance of the TreeMultiset.

The following example is based on

for each sample.

( Of course a TreeList and a TreeMultiset will contain about 100 times more elements than the TreeSet since they allow duplicates )
 --- TreeList ---
 samples: 50
 max:     640
 average: 187.08
 median:  167
 
 --- TreeSet ---
 samples: 50
 max:     280
 average: 80.08
 median:  75
 
 --- TreeMultiSet ---
 samples: 50
 max:     862
 average: 182.2
 median:  163
 
 (durations in milliseconds)
 

Author:
Omnaest
See Also:
SortedList, Serialized Form

Nested Class Summary
protected  class TreeList.ElementList
          An TreeList.ElementList is based on an regular ArrayList but additionally removes itself from the underlying SortedMap if its last element is removed and the List is going to get empty.
protected static interface TreeList.ElementVisitor<E>
           
 
Nested classes/interfaces inherited from class org.omnaest.utils.structure.collection.list.sorted.SortedListAbstract
SortedListAbstract.SortedListAbstractSublist<E>
 
Nested classes/interfaces inherited from class org.omnaest.utils.structure.collection.list.ListAbstract
ListAbstract.ListAbstractSublist<E>
 
Field Summary
 
Fields inherited from class org.omnaest.utils.structure.collection.list.sorted.SortedListAbstract
comparator
 
Constructor Summary
  TreeList()
           
  TreeList(Collection<E> collection)
           
  TreeList(Comparator<E> comparator)
           
  TreeList(Comparator<E> comparator, Collection<E> collection)
           
protected TreeList(Factory<SortedMap<AccessorReadable<E>,TreeList.ElementList>> elementToEqualElementListMapFactory)
           
 
Method Summary
 boolean add(E element)
          This does add the given element at the right order position.
 void clear()
           
 boolean contains(Object object)
          Returns true if any element within the TreeList is Object.equals(Object) to the given Object
 E get(int index)
           
 int indexOf(Object object)
          Returns the index position of the first occurrence of an element which is Object.equals(Object) to the given Object
 boolean isEmpty()
           
 int lastIndexOf(Object object)
           
 ListIterator<E> listIterator()
           
protected  SortedList<E> newInstance(Collection<E> collection)
          Creates a new SortedList instance
 E remove(int index)
           
 boolean remove(Object object)
          Removes the first occurring element within the TreeList, which is Object.equals(Object) to the given Object
 int size()
           
protected  void visitElements(TreeList.ElementVisitor<E> elementVisitor)
          Similar to visitElements(ElementVisitor, Iterable) which uses all available TreeList.ElementList instances of the internal SortedMap in their sort order.
protected  void visitElements(TreeList.ElementVisitor<E> elementVisitor, Iterable<TreeList.ElementList> elementListCollection)
          Visitor method which allows to traverse over all given TreeList.ElementList in their current order and execute the TreeList.ElementVisitor.beforeTraversalOfElementList(int, int, List) on them.
 
Methods inherited from class org.omnaest.utils.structure.collection.list.sorted.SortedListAbstract
add, comparator, first, first, getFirst, getLast, headList, headList, last, last, set, splitAt, subList, subList, subList, tailList, tailList
 
Methods inherited from class org.omnaest.utils.structure.collection.list.ListAbstract
addAll, addFirst, addLast, descendingIterator, element, isValidIndex, iterator, listIterator, offer, offerFirst, offerLast, peek, peekFirst, peekLast, poll, pollFirst, pollLast, pop, push, remove, removeFirst, removeFirstOccurrence, removeLast, removeLastOccurrence
 
Methods inherited from class org.omnaest.utils.structure.collection.CollectionAbstract
addAll, containsAll, equals, hashCode, removeAll, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
addAll, addAll, containsAll, equals, hashCode, iterator, listIterator, removeAll, retainAll, toArray, toArray
 

Constructor Detail

TreeList

public TreeList()
See Also:
TreeList

TreeList

public TreeList(Comparator<E> comparator)
Parameters:
comparator -
See Also:
TreeList

TreeList

public TreeList(Collection<E> collection)
Parameters:
collection -
See Also:
TreeList

TreeList

public TreeList(Comparator<E> comparator,
                Collection<E> collection)
Parameters:
comparator -
collection -
See Also:
TreeList

TreeList

protected TreeList(Factory<SortedMap<AccessorReadable<E>,TreeList.ElementList>> elementToEqualElementListMapFactory)
Parameters:
elementToEqualElementListMapFactory -
See Also:
TreeList
Method Detail

size

public int size()

add

public boolean add(E element)
Description copied from interface: SortedList
This does add the given element at the right order position. This does break the contract of the Collection interface, since the new element is not appended to the Collection.


get

public E get(int index)

visitElements

protected void visitElements(TreeList.ElementVisitor<E> elementVisitor)
Similar to visitElements(ElementVisitor, Iterable) which uses all available TreeList.ElementList instances of the internal SortedMap in their sort order.

Parameters:
elementVisitor -
See Also:
#visitElements(ElementVisitor, int)

visitElements

protected void visitElements(TreeList.ElementVisitor<E> elementVisitor,
                             Iterable<TreeList.ElementList> elementListCollection)
Visitor method which allows to traverse over all given TreeList.ElementList in their current order and execute the TreeList.ElementVisitor.beforeTraversalOfElementList(int, int, List) on them.

Parameters:
elementVisitor -
elementListCollection -
See Also:
#visitElements(ElementVisitor, int)

remove

public E remove(int index)

indexOf

public int indexOf(Object object)
Returns the index position of the first occurrence of an element which is Object.equals(Object) to the given Object


lastIndexOf

public int lastIndexOf(Object object)

remove

public boolean remove(Object object)
Removes the first occurring element within the TreeList, which is Object.equals(Object) to the given Object

Specified by:
remove in interface Collection<E>
Specified by:
remove in interface Deque<E>
Specified by:
remove in interface List<E>
Overrides:
remove in class ListAbstract<E>
Returns:
true: if any equal Object is found

contains

public boolean contains(Object object)
Returns true if any element within the TreeList is Object.equals(Object) to the given Object

Specified by:
contains in interface Collection<E>
Specified by:
contains in interface Deque<E>
Specified by:
contains in interface List<E>
Overrides:
contains in class ListAbstract<E>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Collection<E>
Specified by:
isEmpty in interface List<E>
Overrides:
isEmpty in class CollectionAbstract<E>

clear

public void clear()
Specified by:
clear in interface Collection<E>
Specified by:
clear in interface List<E>
Overrides:
clear in class ListAbstract<E>

listIterator

public ListIterator<E> listIterator()
Specified by:
listIterator in interface List<E>
Overrides:
listIterator in class ListAbstract<E>

newInstance

protected SortedList<E> newInstance(Collection<E> collection)
Description copied from class: SortedListAbstract
Creates a new SortedList instance

Overrides:
newInstance in class SortedListAbstract<E>
Returns:


Copyright © 2013. All Rights Reserved.