- Type Parameters:
E- the element type
- All Implemented Interfaces:
Serializable,Iterable<E>,ImmutableCollection<E>,ImmutableList<E>,ImmutableSequencedCollection<E>,ReadOnlyCollection<E>,ReadOnlyList<E>,ReadOnlySequencedCollection<E>
ImmutableList interface using a bit-mapped trie
(Vector).
The code has been derived from Vavr Vector.java.
Features:
- supports up to 231 - 1 elements
- allows null elements
- is immutable
- is thread-safe
- iterates in the order of the list
Performance characteristics:
- addLast: O(log₃₂ N)
- set: O(log₃₂ N)
- removeAt: O(N)
- removeFirst,removeLast: O(log₃₂ N)
- contains: O(N)
- toMutable: O(1)
- clone: O(1)
- iterator creation: O(log₃₂ N)
- iterator.next: O(1)
- getFirst, getLast: O(log₃₂ N)
- reversed: O(N)
References:
For a similar design, see 'Vector.java' in vavr. The internal data structure of this class is licensed from vavr.
- Vector.java. Copyright 2023 (c) vavr. MIT License.
- github.com
- See Also:
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConstructs a new empty list.protectedVectorList(@Nullable Iterable<? extends E> iterable) Constructs a new list that contains all the elements of the specified iterable.protectedVectorList(PrivateData privateData) Creates a new instance with the provided privateData data object. -
Method Summary
Modifier and TypeMethodDescriptionReturns a copy of this list that contains all elements of this list and the specified element appended to the end of the list.Returns a copy of this list that contains all elements of this list and the specified element appended to the end of the list.Returns a copy of this list that contains all elements of this list and all elements of the specified collection appended.Returns a copy of this list that contains all elements of this list and all elements of the specified collection appended.Returns a copy of this collection that contains all elements of this collection and also the specified element as the first element in the iteration order.Returns a copy of this collection that contains all elements of this collection and also the specified element as the last element in the iteration order.booleanReturnstrueif this collection contains the specified object.static <T> VectorList<T> <T> VectorList<T> empty()Returns a copy of this list that is empty.booleanCompares the specified object with this list for equality.get(int index) Returns the element at the specified position in this list.inthashCode()Returns the hash code value for this list.intiterator()Returns an iterator over elements of typeE.intmaxSize()Returns the maximal number of elements that this collection type can holdprotected VectorList<E> newInstance(PrivateData privateData) Creates a new instance with the provided privateData object as its internal data structure.static <T> VectorList<T> of()static <T> VectorList<T> of(T... t) static <T> VectorList<T> ofIterator(Iterator<T> iterator) static <T> VectorList<T> Returns a reversed-order view of this collection.readOnlySubList(int fromIndex, int toIndex) Returns a copy of this list that contains only the elements in the given index range.Returns a copy of this list that contains all elements of this list except the specified element.Returns a copy of this list that contains all elements of this list except the elements of the specified collection.removeAt(int index) Returns a copy of this list that contains all elements of this list except the element at the specified indexReturns a copy of this set that contains all elements of this set except the first.Returns a copy of this set that contains all elements of this set except the last.removeRange(int fromIndex, int toIndex) Returns a copy of this list that contains all elements of this list except the elements in the specified range.Returns a copy of this list that contains only elements that are in this list and in the specified collection.reverse()Returns a reversed copy of this list.Returns a copy of this list that contains all elements of this list and the specified element replaced.intsize()Returns the size of the collection.Returns a spliterator over elements of typeE.Returns a mutable copy of this list.toString()Returns a string representation of this list.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.jhotdraw8.icollection.immutable.ImmutableCollection
filterMethods inherited from interface org.jhotdraw8.icollection.readonly.ReadOnlyCollection
characteristics, containsAll, isEmpty, stream, toArray, toArrayMethods inherited from interface org.jhotdraw8.icollection.readonly.ReadOnlyList
asList, getFirst, getLast, getLast, indexOf, lastIndexOf, listIterator, listIterator, peekFirst, peekLastMethods inherited from interface org.jhotdraw8.icollection.readonly.ReadOnlySequencedCollection
asCollection
-
Constructor Details
-
VectorList
protected VectorList()Constructs a new empty list. -
VectorList
Constructs a new list that contains all the elements of the specified iterable.- Parameters:
iterable- an iterable
-
VectorList
Creates a new instance with the provided privateData data object.This constructor is intended to be called from a constructor of the subclass, that is called from method
newInstance(PrivateData).- Parameters:
privateData- an privateData data object
-
-
Method Details
-
newInstance
Creates a new instance with the provided privateData object as its internal data structure.Subclasses must override this method, and return a new instance of their subclass!
- Parameters:
privateData- the internal data structure needed by this class for creating the instance.- Returns:
- a new instance of the subclass
-
of
-
of
-
ofIterator
-
ofStream
-
copyOf
-
empty
Description copied from interface:ImmutableListReturns a copy of this list that is empty.- Specified by:
emptyin interfaceImmutableCollection<E>- Specified by:
emptyin interfaceImmutableList<E>- Specified by:
emptyin interfaceImmutableSequencedCollection<E>- Type Parameters:
T- the element type of the returned collection- Returns:
- this list instance if it is already empty, or a different list instance that is empty.
-
add
Description copied from interface:ImmutableListReturns a copy of this list that contains all elements of this list and the specified element appended to the end of the list.- Specified by:
addin interfaceImmutableCollection<E>- Specified by:
addin interfaceImmutableList<E>- Specified by:
addin interfaceImmutableSequencedCollection<E>- Parameters:
element- an element- Returns:
- a different list instance with the element added
-
add
Description copied from interface:ImmutableListReturns a copy of this list that contains all elements of this list and the specified element appended to the end of the list.- Specified by:
addin interfaceImmutableList<E>- Parameters:
index- the insertion indexelement- an element- Returns:
- a different list instance with the element added
-
addAll
Description copied from interface:ImmutableListReturns a copy of this list that contains all elements of this list and all elements of the specified collection appended.- Specified by:
addAllin interfaceImmutableCollection<E>- Specified by:
addAllin interfaceImmutableList<E>- Specified by:
addAllin interfaceImmutableSequencedCollection<E>- Parameters:
c- a collection to be added to this list- Returns:
- a different list instance with the elements added
-
addFirst
Description copied from interface:ImmutableSequencedCollectionReturns a copy of this collection that contains all elements of this collection and also the specified element as the first element in the iteration order.A collection may prevent that the same element can be added more than once.
If the iteration order is based on an ordering relation of the elements, then the element is only the first in a sequence of elements with the same ordering relation; which is not necessarily the first in the total iteration order.
- Specified by:
addFirstin interfaceImmutableList<E>- Specified by:
addFirstin interfaceImmutableSequencedCollection<E>- Parameters:
element- an element- Returns:
- this collection instance if it already contains the element as the first in the iteration order, or a different collection instance with the element added as the first in the iteration order
-
addLast
Description copied from interface:ImmutableSequencedCollectionReturns a copy of this collection that contains all elements of this collection and also the specified element as the last element in the iteration order.A collection may prevent that the same element can be added more than once.
If the iteration order is based on an ordering relation of the elements, then the element is only the last in a sequence of elements with the same ordering relation; which is not necessarily the last in the total iteration order.
- Specified by:
addLastin interfaceImmutableList<E>- Specified by:
addLastin interfaceImmutableSequencedCollection<E>- Parameters:
element- an element- Returns:
- this collection instance if it already contains the element as the last in the iteration order, or a different collection instance with the element added as the last in the iteration order
-
addAll
Description copied from interface:ImmutableListReturns a copy of this list that contains all elements of this list and all elements of the specified collection appended.- Specified by:
addAllin interfaceImmutableList<E>- Parameters:
index- the insertion indexc- a collection to be added to this list- Returns:
- a different list instance with the elements added
-
readOnlyReversed
Description copied from interface:ReadOnlySequencedCollectionReturns a reversed-order view of this collection. Changes to the underlying collection are visible in the reversed view.- Specified by:
readOnlyReversedin interfaceReadOnlySequencedCollection<E>- Returns:
- a reversed-order view of this collection
-
reverse
Description copied from interface:ImmutableListReturns a reversed copy of this list.This operation may be implemented in O(N).
Use
ReadOnlySequencedCollection.readOnlyReversed()if you only need to iterate in the reversed sequence over this list.- Specified by:
reversein interfaceImmutableList<E>- Returns:
- a reversed copy of this list.
-
remove
Description copied from interface:ImmutableListReturns a copy of this list that contains all elements of this list except the specified element.- Specified by:
removein interfaceImmutableCollection<E>- Specified by:
removein interfaceImmutableList<E>- Specified by:
removein interfaceImmutableSequencedCollection<E>- Parameters:
element- an element- Returns:
- this list instance if it already does not contain the element, or a different list instance with the element removed
-
removeAt
Description copied from interface:ImmutableListReturns a copy of this list that contains all elements of this list except the element at the specified index- Specified by:
removeAtin interfaceImmutableList<E>- Parameters:
index- an index- Returns:
- a different list instance with the element removed
-
removeFirst
Description copied from interface:ImmutableSequencedCollectionReturns a copy of this set that contains all elements of this set except the first.- Specified by:
removeFirstin interfaceImmutableList<E>- Specified by:
removeFirstin interfaceImmutableSequencedCollection<E>- Returns:
- a new set instance with the first element removed
-
removeLast
Description copied from interface:ImmutableSequencedCollectionReturns a copy of this set that contains all elements of this set except the last.- Specified by:
removeLastin interfaceImmutableList<E>- Specified by:
removeLastin interfaceImmutableSequencedCollection<E>- Returns:
- a new set instance with the last element removed
-
retainAll
Description copied from interface:ImmutableListReturns a copy of this list that contains only elements that are in this list and in the specified collection.- Specified by:
retainAllin interfaceImmutableCollection<E>- Specified by:
retainAllin interfaceImmutableList<E>- Specified by:
retainAllin interfaceImmutableSequencedCollection<E>- Parameters:
c- a collection with elements to be retained in this set- Returns:
- this list instance if it has not changed, or a different list instance with elements removed
-
removeRange
Description copied from interface:ImmutableListReturns a copy of this list that contains all elements of this list except the elements in the specified range.- Specified by:
removeRangein interfaceImmutableList<E>- Parameters:
fromIndex- from index (inclusive) of the sub-listtoIndex- to index (exclusive) of the sub-list- Returns:
- a different list instance with the element removed
-
removeAll
Description copied from interface:ImmutableListReturns a copy of this list that contains all elements of this list except the elements of the specified collection.- Specified by:
removeAllin interfaceImmutableCollection<E>- Specified by:
removeAllin interfaceImmutableList<E>- Specified by:
removeAllin interfaceImmutableSequencedCollection<E>- Parameters:
c- a collection with elements to be removed from this set- Returns:
- this list instance if it already does not contain the elements, or a different list instance with the elements removed
-
set
Description copied from interface:ImmutableListReturns a copy of this list that contains all elements of this list and the specified element replaced.- Specified by:
setin interfaceImmutableList<E>- Parameters:
element- an element- Returns:
- this list instance if it has not changed, or a different list instance with the element changed
-
get
Description copied from interface:ReadOnlyListReturns the element at the specified position in this list.- Specified by:
getin interfaceReadOnlyList<E>- Parameters:
index- the index of the element- Returns:
- the element
-
readOnlySubList
Description copied from interface:ImmutableListReturns a copy of this list that contains only the elements in the given index range.- Specified by:
readOnlySubListin interfaceImmutableList<E>- Specified by:
readOnlySubListin interfaceReadOnlyList<E>- Parameters:
fromIndex- from index (inclusive) of the sub-listtoIndex- to index (exclusive) of the sub-list- Returns:
- this list instance if it has not changed, or a different list instance with the element changed
-
size
public int size()Description copied from interface:ReadOnlyCollectionReturns the size of the collection.- Specified by:
sizein interfaceReadOnlyCollection<E>- Returns:
- the size
-
indexOf
-
contains
Description copied from interface:ReadOnlyCollectionReturnstrueif this collection contains the specified object.- Specified by:
containsin interfaceReadOnlyCollection<E>- Parameters:
o- an object- Returns:
trueif this collection contains the specified object
-
hashCode
public int hashCode()Description copied from interface:ReadOnlyListReturns the hash code value for this list. The hash code is the result of the calculation described inList.hashCode().Implementations of this method should use
ReadOnlyList.iteratorToHashCode(java.util.Iterator<E>).- Specified by:
hashCodein interfaceReadOnlyList<E>- Overrides:
hashCodein classObject- Returns:
- the hash code value for this set
-
toMutable
Description copied from interface:ImmutableListReturns a mutable copy of this list.- Specified by:
toMutablein interfaceImmutableCollection<E>- Specified by:
toMutablein interfaceImmutableList<E>- Returns:
- a mutable copy.
-
iterator
Description copied from interface:ReadOnlyListReturns an iterator over elements of typeE.- Specified by:
iteratorin interfaceIterable<E>- Specified by:
iteratorin interfaceReadOnlyCollection<E>- Specified by:
iteratorin interfaceReadOnlyList<E>- Returns:
- an iterator.
-
maxSize
public int maxSize()Description copied from interface:ImmutableCollectionReturns the maximal number of elements that this collection type can hold- Specified by:
maxSizein interfaceImmutableCollection<E>- Returns:
- the maximal size
-
spliterator
Description copied from interface:ReadOnlyListReturns a spliterator over elements of typeE.- Specified by:
spliteratorin interfaceIterable<E>- Specified by:
spliteratorin interfaceReadOnlyList<E>- Returns:
- an iterator.
-
equals
Description copied from interface:ReadOnlyListCompares the specified object with this list for equality.Returns
trueif the given object is also a read-only list and the two lists contain the same elements in the same sequence.Implementations of this method should use
ReadOnlyList.listEquals(org.jhotdraw8.icollection.readonly.ReadOnlyList<E>, java.lang.Object).- Specified by:
equalsin interfaceReadOnlyList<E>- Overrides:
equalsin classObject- Parameters:
obj- an object- Returns:
trueif the object is equal to this list
-
toString
Returns a string representation of this list.The string representation is consistent with the one produced by
AbstractCollection.toString().
-