-
- Type Parameters:
E- the type of elements maintained by this list
- All Superinterfaces:
Collection<E>,Iterable<E>,List<E>
- All Known Implementing Classes:
SynchronizedTreeList,UnsynchronizedTreeList
public interface TreeList<E> extends List<E>
A sorted list that contains no duplicate elements, backed by a red-black tree.Elements are ordered using either a
Comparatorsupplied at construction time, or their natural ordering. Two elementsaandbare considered duplicates if and only ifcompare(a, b) == 0.Unlike a general-purpose
List, the position of each element is determined by its sort order, not by the caller. The following optionalListoperations are therefore not supported and will always throwUnsupportedOperationException:List.add(int, Object)List.addAll(int, java.util.Collection)List.set(int, Object)ListIterator.add(E)ListIterator.set(E)
subList(int, int)returns a live view of this list bounded by element values. The view reflects mutations to the parent list and vice versa. Adding an element outside the view's value range throwsIllegalArgumentException, following the same convention asTreeMap.subMap(Object, Object).All elements inserted into a
TreeListmust be mutually comparable using the list's comparator (or natural ordering). Attempting to insert an incomparable element will throw aClassCastException.nullelements are not permitted.Duplicate elements are silently rejected:
List.add(Object)returnsfalseif the element is already present, leaving the list unchanged.The
List.equals(Object)contract is position-based: aTreeListis equal to anyListcontaining the same elements in the same sorted order.Two implementations are provided:
UnsynchronizedTreeList— not thread-safe; O(log n) for insertion, removal, and index-based access.SynchronizedTreeList— thread-safe, backed by aReentrantReadWriteLock.
- Version:
- 1.0
- Author:
- Dufrenoy
- See Also:
UnsynchronizedTreeList,SynchronizedTreeList
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description Optional<Comparator<? super E>>comparator()Returns the comparator used to order the elements in this list, or an emptyOptionalif the elements are ordered by their natural ordering.TreeList<E>subList(int fromIndex, int toIndex)Returns a live view of the portion of this list whose elements range from the element atfromIndex(inclusive) to the element attoIndex(exclusive).-
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
-
Methods inherited from interface java.util.List
add, add, addAll, addAll, clear, contains, containsAll, equals, get, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, replaceAll, retainAll, set, size, sort, spliterator, toArray, toArray
-
-
-
-
Method Detail
-
comparator
Optional<Comparator<? super E>> comparator()
Returns the comparator used to order the elements in this list, or an emptyOptionalif the elements are ordered by their natural ordering.- Returns:
- the comparator used to order this list, or an empty
Optionalif natural ordering is used
-
subList
TreeList<E> subList(int fromIndex, int toIndex)
Returns a live view of the portion of this list whose elements range from the element atfromIndex(inclusive) to the element attoIndex(exclusive). The returned view is itself aTreeList: it is sorted, contains no duplicates, and supports the same operations as this list.The view is bounded by element values, not by indices. After creation, the view contains all elements
ein this list such thatcompare(fromElement, e) <= 0andcompare(e, toElement) < 0, wherefromElementandtoElementare the elements at positionsfromIndexandtoIndexat the time the view was created.Structural modifications through the view (add, remove, clear) are reflected in the parent list, and vice versa. However, if the parent list is structurally modified outside the view, the view becomes invalid and subsequent operations will throw
ConcurrentModificationException(fail-fast).Adding an element that falls outside the view's value range throws
IllegalArgumentException, following the same convention asTreeMap.subMap(Object, Object).- Specified by:
subListin interfaceList<E>- Parameters:
fromIndex- low endpoint (inclusive) of the subListtoIndex- high endpoint (exclusive) of the subList- Returns:
- a view of the specified range within this list
- Throws:
IndexOutOfBoundsException- iffromIndex < 0ortoIndex > size()IllegalArgumentException- iffromIndex > toIndex
-
-