- Type Parameters:
E- type of elements returned by this iterator
- All Implemented Interfaces:
Iterator<E>
Provides random iteration over a list with the ability to stop iteration early when a condition is met. This can provide significant performance improvements over Collections.shuffle() when the iteration process may terminate before processing all elements.
Uses BitSet for tracking served items and I2P RandomSource for better randomness distribution. Includes Android compatibility workarounds and proper bounds checking to prevent infinite loops.
Performance characteristics:
- O(N) time complexity for iteration setup
- O(1) average time for
next()calls - Memory efficient with minimal object allocation
Note: Not recommended for small lists or when iterating through a large portion of a collection. Use Collections.shuffle() for those cases instead.
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final booleanTrue on the Android versions whose bit operations are broken.The list being traversed in random order.private final intSize of that list.private intUsed to narrow the range to take random indexes from.private final RandomThe random number generator has a great influence on the running time of this iterator.private final BitSetMapping indicating which items were served (by index).private intThe amount of items served so far.private intHow many of the remaining elements may still be picked.Constructor Summary
ConstructorsConstructorDescriptionRandomIterator(List<E> list) Fixes the index range from the list size and marks nothing as served yet.Method Summary
Modifier and TypeMethodDescriptionbooleanhasNext()Whether the next random element is present.next()Return the next random element.private intnextClearBit(int n) Workaround for bug in Android (ICS only?) http://code.google.com/p/android/issues/detail?id=31036private intpreviousClearBit(int n) Finds the last unset bit at or below a position.voidremove()Removal is not supported.private static voidTest case from android ticket above.Methods inherited from class Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Iterator
forEachRemaining
Field Details
served
Mapping indicating which items were served (by index). if served[i] then the item with index i in the list has already been served.Note it is possible to save memory here by using BitSet rather than a boolean array, however it will increase the running time slightly.
servedCount
private int servedCountThe amount of items served so far.list
LIST_SIZE
private final int LIST_SIZESize of that list.rand
The random number generator has a great influence on the running time of this iterator.See, for instance, Michael Brundage's random number generation essay for some implementations, which are faster than java.util.Random. This is the shared Random; the JVM may substitute a faster implementation on this platform.
lower
private int lowerUsed to narrow the range to take random indexes from.upper
private int upperHow many of the remaining elements may still be picked.HAS_ANDROID_BUG
private static final boolean HAS_ANDROID_BUGTrue on the Android versions whose bit operations are broken.
Constructor Details
RandomIterator
Method Details
hasNext
next
previousClearBit
private int previousClearBit(int n) Finds the last unset bit at or below a position.- Parameters:
n- the position to search down from- Returns:
- the highest unset bit at or below n
nextClearBit
private int nextClearBit(int n) Workaround for bug in Android (ICS only?) http://code.google.com/p/android/issues/detail?id=31036- Parameters:
n- the index to start searching from- Returns:
- the first unserved index at or after n, -1 if there is none
remove
public void remove()Removal is not supported.- Specified by:
removein interfaceIterator<E>- Throws:
UnsupportedOperationException- always, as this iterator is not mutable
testAndroid
private static void testAndroid()Test case from android ticket above.