Class ElementAwareList<T>
- Type Parameters:
T- The element type. Often a tuple.
- All Implemented Interfaces:
Iterable<T>
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddAfter(T tuple, ElementAwareListEntry<T> previous) voidclear()first()voidConvenience method for where it is easy to use a non-capturing lambda.iterator()SeeforEach(Consumer)for a discussion on the correct use of this method.last()randomizedIterator(Random random) Returns an iterator that will randomly iterate over the elements.voidremove(ElementAwareListEntry<T> entry) intsize()toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
spliterator
-
Constructor Details
-
ElementAwareList
public ElementAwareList()
-
-
Method Details
-
add
-
addFirst
-
addAfter
-
remove
-
first
-
last
-
size
public int size() -
forEach
Convenience method for where it is easy to use a non-capturing lambda. If a capturing lambda consumer were to be created for this method, useiterator()instead, which will consume less memory.For example, the following code is perfectly fine:
for (int i = 0; i < 3; i++) { elementAwareList.forEach(entry -> doSomething(entry)); }It will create only one lambda instance, regardless of the number of iterations; it doesn't need to capture any state. On the contrary, the following code will create three instances of a capturing lambda, one for each iteration of the for loop:for (int a: List.of(1, 2, 3)) { elementAwareList.forEach(entry -> doSomething(entry, a)); }In this case, the lambda would need to capture "a" which is different in every iteration. Therefore, it will generally be better to use the iterator variant, as that will only ever create one instance of the iterator, regardless of the number of iterations:for (int a: List.of(1, 2, 3)) { for (var entry: elementAwareList) { doSomething(entry, a); } }This is only an issue on the hot path, where this method can create quite a large garbage collector pressure on account of creating throw-away instances of capturing lambdas. -
iterator
SeeforEach(Consumer)for a discussion on the correct use of this method. -
clear
public void clear() -
randomizedIterator
Returns an iterator that will randomly iterate over the elements. This iterator is exhaustive; once every element has been once iterated over, the iterator returns false for every subsequentIterator.hasNext(). The iterator does not support theIterator.remove()operation.- Parameters:
random- The random instance to use for shuffling.- Returns:
- never null
-
toString
-