Java Iterator searchIndexInIterator(Iterator iterator, T value)

Here you can find the source of searchIndexInIterator(Iterator iterator, T value)

Description

search Index In Iterator

License

Open Source License

Declaration

public static <T> int searchIndexInIterator(Iterator<T> iterator, T value) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Iterator;

public class Main {
    public static <T> int searchIndexInIterator(Iterator<T> iterator, T value) {
        return searchIndexInIterator(iterator, value, 0);
    }/*from  w  w  w  .  j  a  v a2s .  c  o  m*/

    public static <T> int searchIndexInIterator(Iterator<T> iterator, T value, int startIndex) {
        int expectedHashCode = value == null ? 0 : value.hashCode();
        for (int i = 0; i < startIndex && iterator.hasNext(); i++) {
            iterator.next();
        }
        if (!iterator.hasNext()) {
            return -1;
        }
        for (int i = startIndex; iterator.hasNext(); i++) {
            T next = iterator.next();
            if (next == null) {
                if (value == null) {
                    return i;
                } else {
                    continue;
                }
            } else if (value == null) {
                continue;
            }

            if (next.hashCode() == expectedHashCode && next.equals(value)) {
                return i;
            }
        }
        return -1;
    }
}

Related

  1. removeObject(Object item, Iterator iter, boolean justFirst)
  2. removeOnNext(final Iterator iterator)
  3. removeValue(List args, int idx, Iterator it)
  4. retrieveFirstAndExhaustIterator(Iterator iterator)
  5. safeAdvance(Iterator iterator)
  6. shallowUnionColIter(Iterator> values)
  7. single(Iterator i)
  8. singleElementIterator(T element)
  9. singleOrDefault(Iterator iterator)