Here you can find the source of counter(final Iterator iterator)
Parameter | Description |
---|---|
iterator | the iterator to count |
public static long counter(final Iterator iterator)
//package com.java2s; //License from project: Apache License import java.util.Iterator; import java.util.NoSuchElementException; public class Main { /**//from w w w . j a v a 2 s .co m * Count the number of objects in an iterator. * This will exhaust the iterator. * Note that the try/catch model is not "acceptable Java," but is more efficient given the architecture of AbstractPipe. * * @param iterator the iterator to count * @return the number of objects in the iterator */ public static long counter(final Iterator iterator) { long counter = 0; try { while (true) { iterator.next(); counter++; } } catch (final NoSuchElementException e) { } return counter; } }