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 Chain
s 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 theleft
Chain and theright
Chain, in O(1) time.Returns a new Chain concatenating elements fromthis
Chain followed bylastElement
, in O(1) time.get
(int i) getFirst()
Returns the first element.boolean
isEmpty()
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 withfirst
followed byremaining
.int
size()
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, set
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, remove, 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.Collection
parallelStream, removeIf, toArray
Methods 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 withfirst
followed byremaining
. -
concat
Returns a new Chain concatenating elements from theleft
Chain and theright
Chain, in O(1) time.Encounter order of elements is preserved. That is,
concat([1, 2], [3, 4])
returns[1, 2, 3, 4]
. -
concat
Returns a new Chain concatenating elements fromthis
Chain followed bylastElement
, in O(1) time. -
size
public int size()Returns the size of the chain. O(1) operation.- Specified by:
size
in interfaceCollection<T>
- Specified by:
size
in interfaceList<T>
- Specified by:
size
in classAbstractCollection<T>
-
isEmpty
public boolean isEmpty()Always false. O(1) operation.- Specified by:
isEmpty
in interfaceCollection<T>
- Specified by:
isEmpty
in interfaceList<T>
- Overrides:
isEmpty
in classAbstractCollection<T>
-
stream
Returns a lazy stream of the elements in this list. The returned stream is lazy in that concatenated chains aren't consumed until the stream reaches their elements. -
getFirst
Returns the first element. Will override the SequencedCollection method. Takes O(1) time. -
get
-
iterator
-
spliterator
-
listIterator
- Specified by:
listIterator
in interfaceList<T>
- Overrides:
listIterator
in classAbstractList<T>
-
listIterator
- Specified by:
listIterator
in interfaceList<T>
- Overrides:
listIterator
in classAbstractList<T>
-
subList
-