Get the size of an iterator (number of items in it). - Android java.util

Android examples for java.util:Iterable

Description

Get the size of an iterator (number of items in it).

Demo Code

/*/*  w ww.j  a v a  2  s. co m*/
 *******************************************************************************
 * Copyright (C) 1996-2015, International Business Machines Corporation and    *
 * others. All Rights Reserved.                                                *
 *******************************************************************************
 */
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;

public class Main{
    /**
     * Get the size of an iterator (number of items in it).
     * @param source
     * @return
     */
    public static int size(Iterator source) {
        int result = 0;
        while (source.hasNext()) {
            source.next();
            ++result;
        }
        return result;
    }
}

Related Tutorials