Here you can find the source of retrieveFirstAndExhaustIterator(Iterator
Parameter | Description |
---|---|
E | the type of elements in the iterator |
iterator | the iterator to exhaust |
public static <E> E retrieveFirstAndExhaustIterator(Iterator<E> iterator)
//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(); } } }