Class Chain<T>
- All Implemented Interfaces:
Iterable<T>,Collection<T>,List<T>,SequencedCollection<T>
List implementation that supports O(1) concatenation.
At high level, this class provides similar behavior as Stream.concat()
or Guava Iterables.concat(), except it's not
recursive. That is, if your Chain is the result of 1 million concatenations, you won't run
into stack overflow error because under the hood, it's a heap-allocated immutable tree structure.
The expected use case is to concatenate lots of smaller Chains using the
concat() methods to create the final Chain. O(n) materialization cost will be (lazily) incurred
upon the first time accessing the elements of the final Chain through the List
interface such as List.get(int), List.equals(java.lang.Object), AbstractCollection.toString() etc. You may also
want to copy the final Chain into a more conventional List such as Guava ImmutableList.
On the other hand, it's inefficient to materialize, concatenate then materialize the concatenated Chain...
Unless explicitly documented as lazy or an O(1) operation, all List methods will
materialize the elements eagerly if not already.
Null elements are not allowed.
While bearing a bit of similarity, this class isn't a persistent data structure.
Besides the O(1) concatenation, it's a traditional immutable List supporting
no other functional updates. Concatenation is O(1) as opposed to O(logn) in persistent lists;
and random access is also O(1) (after one-time lazy materialization).
- Since:
- 8.1
-
Field Summary
Fields inherited from class java.util.AbstractList
modCount -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Chain<T> Returns a new Chain concatenating elements from theleftChain and therightChain, in O(1) time.Returns a new Chain concatenating elements fromthisChain followed bylastElement, in O(1) time.get(int i) getFirst()Returns the first element.booleanisEmpty()Always false.iterator()listIterator(int index) static <T> Chain<T> of(T element) Returns a Chain with a single element.static <T> Chain<T> of(T first, T... remaining) Returns a Chain withfirstfollowed byremaining.intsize()Returns the size of the chain.stream()Returns a lazy stream of the elements in this list.subList(int fromIndex, int toIndex) Methods inherited from class java.util.AbstractList
add, add, addAll, clear, equals, hashCode, indexOf, lastIndexOf, remove, removeRange, setMethods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, remove, removeAll, retainAll, toArray, toArray, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, toArrayMethods inherited from interface java.util.List
addAll, addFirst, addLast, contains, containsAll, getLast, remove, removeAll, removeFirst, removeLast, replaceAll, retainAll, reversed, sort, toArray, toArray
-
Method Details
-
of
Returns a Chain with a single element. -
of
Returns a Chain withfirstfollowed byremaining. -
concat
-
concat
-
size
public int size()Returns the size of the chain. O(1) operation.- Specified by:
sizein interfaceCollection<T>- Specified by:
sizein interfaceList<T>- Specified by:
sizein classAbstractCollection<T>
-
isEmpty
public boolean isEmpty()Always false. O(1) operation.- Specified by:
isEmptyin interfaceCollection<T>- Specified by:
isEmptyin interfaceList<T>- Overrides:
isEmptyin classAbstractCollection<T>
-
stream
-
getFirst
Returns the first element. Will override the SequencedCollection method. Takes O(1) time. -
get
-
iterator
-
spliterator
-
listIterator
- Specified by:
listIteratorin interfaceList<T>- Overrides:
listIteratorin classAbstractList<T>
-
listIterator
- Specified by:
listIteratorin interfaceList<T>- Overrides:
listIteratorin classAbstractList<T>
-
subList
-