Java Utililty Methods Iterator

List of utility methods to do Iterator

Description

The list of methods to do Iterator are organized into topic(s).

Method

booleanremoveObject(Object item, Iterator iter, boolean justFirst)
Removes a specified object using an iterator.
boolean modified = false;
while (iter.hasNext()) {
    Object o = iter.next();
    if (item == null && o == null) {
        iter.remove();
        if (justFirst)
            return true;
        if (!modified)
...
IteratorremoveOnNext(final Iterator iterator)
remove On Next
return new Iterator<T>() {
    @Override
    public boolean hasNext() {
        return iterator.hasNext();
    @Override
    public void remove() {
        iterator.remove();
...
booleanremoveValue(List args, int idx, Iterator it)
remove Value
if (args.size() > idx) {
    final String nextArg = args.get(idx);
    if (!nextArg.startsWith("-")) {
        it.next();
        it.remove();
        return true;
return false;
EretrieveFirstAndExhaustIterator(Iterator iterator)
Returns the first element and exhausts an iterator
E returnVal = null;
if (iterator.hasNext()) {
    returnVal = iterator.next();
exhaustIterator(iterator);
return returnVal;
TsafeAdvance(Iterator iterator)
safe Advance
return iterator.hasNext() ? iterator.next() : null;
intsearchIndexInIterator(Iterator iterator, T value)
search Index In Iterator
return searchIndexInIterator(iterator, value, 0);
IterableshallowUnionColIter(Iterator> values)
shallow Union Col Iter
return () -> multiIterator(values);
Tsingle(Iterator i)
single
if (i.hasNext()) {
    return i.next();
return null;
IteratorsingleElementIterator(T element)
single Element Iterator
return Arrays.asList(element).iterator();
TsingleOrDefault(Iterator iterator)
single Or Default
if (!iterator.hasNext()) {
    return null;
T first = iterator.next();
if (iterator.hasNext()) {
    throw new IllegalStateException("Expected single result, but got more than one");
return first;
...