T
- the type of the collection elementspublic class ConcatCollection<T> extends AbstractCollection<T> implements Cloneable, Serializable
Collection
s
provide concatenation only through
Collection.addAll(collection)
in
O(n)
time. Also note that
Stream.concat(Stream, Stream)
discourages repeated concatenation. The Collection is useful e.g. for
accumulating partial results in the combine phase of
RecursiveTask
s, when subtasks
return a collection of elements which are to be combined into one flat
collection.The characteristics of this implementation include the following:
O(1)
time concatenation.null
values.iterator()
returns the elements
in the order they were added).Cloneable
.Iterator.remove()
operation in
O(1)
time.Spliterator
is optimized for lower temporary memory usage
compared to the default Spliterator. It is
Spliterator.ORDERED
| Spliterator.SIZED
| Spliterator.SUBSIZED
.
Constructor and Description |
---|
ConcatCollection()
Creates a new empty ConcatCollection instance.
|
ConcatCollection(Collection<? extends T> collection)
Creates a new ConcatCollection instance and adds all elements of the given
collection.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(T newElement) |
void |
clear() |
ConcatCollection<T> |
clone()
Returns a shallow copy of this collection.
|
void |
concat(ConcatCollection<T> other)
Returns the concatenation of this and the other collection.
|
boolean |
detectCycle()
Returns true, if an internal cycle is detected.
|
boolean |
equals(Object obj)
Compares this collection with the given object.
|
int |
hashCode() |
Iterator<T> |
iterator() |
int |
size() |
Spliterator<T> |
spliterator() |
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
finalize, getClass, notify, notifyAll, wait, wait, wait
parallelStream, removeIf, stream
public ConcatCollection()
public ConcatCollection(Collection<? extends T> collection)
collection
- if not null, all elements of the collection are added to
this ConcatCollectionpublic Iterator<T> iterator()
iterator
in interface Iterable<T>
iterator
in interface Collection<T>
iterator
in class AbstractCollection<T>
public Spliterator<T> spliterator()
NOTE:
While the iterator
supports a save
remove
operation, the Spliterator does not! This
collection must not be changed during spliteration.
Implementation note:
This Spliterator is more memory efficient than the default
Spliterators.spliterator(Collection, int)
by avoiding to copy all elements into an array and directly operating on
internal node objects.
spliterator
in interface Iterable<T>
spliterator
in interface Collection<T>
ConcurrentModificationException
- if the collections change during
comparisonpublic int size()
size
in interface Collection<T>
size
in class AbstractCollection<T>
public boolean add(T newElement)
add
in interface Collection<T>
add
in class AbstractCollection<T>
public void clear()
clear
in interface Collection<T>
clear
in class AbstractCollection<T>
public void concat(ConcatCollection<T> other)
O(1)
time by direct connection of
internal nodes. Note, that changes made to the other collection after the
concatenation will be reflected in the resulting collection.
NOTE: The caller is responsible for not concatenating the same ConcatCollection instance twice, otherwise [spl]iteration will result in endless loops!
other
- the other collectiondetectCycle()
public boolean detectCycle()
concatenated
twice. This method is primarily for testing and debugging purposes.concat(ConcatCollection)
public boolean equals(Object obj)
equals
in interface Collection<T>
equals
in class Object
obj
- the other object to compareConcurrentModificationException
- if the collections change during
comparisonpublic int hashCode()
hashCode
in interface Collection<T>
hashCode
in class Object
public ConcatCollection<T> clone() throws CloneNotSupportedException
clone
in class Object
CloneNotSupportedException
- this class will never throw a
CloneNotSupportedException, whereas a specialized class might do so