Java Iterator retrieveFirstAndExhaustIterator(Iterator iterator)

Here you can find the source of retrieveFirstAndExhaustIterator(Iterator iterator)

Description

Returns the first element and exhausts an iterator

License

Educational Community License

Parameter

Parameter Description
E the type of elements in the iterator
iterator the iterator to exhaust

Return

the first element of the iterator; null if the iterator's empty

Declaration

public static <E> E retrieveFirstAndExhaustIterator(Iterator<E> iterator) 

Method Source Code


//package com.java2s;
/*/*  w  w w.j ava2  s.  c o  m*/
 * Copyright 2007 The Kuali Foundation
 * 
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.opensource.org/licenses/ecl2.php
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Iterator;

public class Main {
    /**
     * Returns the first element and exhausts an iterator
     * 
     * @param <E> the type of elements in the iterator
     * @param iterator the iterator to exhaust
     * @return the first element of the iterator; null if the iterator's empty
     */
    public static <E> E retrieveFirstAndExhaustIterator(Iterator<E> iterator) {
        E returnVal = null;
        if (iterator.hasNext()) {
            returnVal = iterator.next();
        }
        exhaustIterator(iterator);
        return returnVal;
    }

    /**
     * Exhausts (i.e. complete iterates through) an iterator
     * 
     * @param iterator
     */
    public static void exhaustIterator(Iterator<?> iterator) {
        while (iterator.hasNext()) {
            iterator.next();
        }
    }
}

Related

  1. reduceSet(Iterator> values)
  2. removeAll(Collection collection, Iterator itemsToRemove)
  3. removeObject(Object item, Iterator iter, boolean justFirst)
  4. removeOnNext(final Iterator iterator)
  5. removeValue(List args, int idx, Iterator it)
  6. safeAdvance(Iterator iterator)
  7. searchIndexInIterator(Iterator iterator, T value)
  8. shallowUnionColIter(Iterator> values)
  9. single(Iterator i)