Java tutorial
//package com.java2s; //License from project: Open Source License import java.util.Iterator; public class Main { /** * Calculates the number of generated elements of the given iterable. * * @param <T> The type of elements enumerated by the given iterable. * @param iterable The given iterable. * @return The number of elements generated by the iterable. */ public static <T> int size(final Iterable<T> iterable) { if (iterable != null) { return size(iterable.iterator()); } else { return 0x00; } } /** * Calculates the number of generated elements of the given iterator. * * @param <T> The type of elements enumerated by the given iterator. * @param iterator The given iterator. * @return The number of elements generated by the iterator. */ public static <T> int size(final Iterator<T> iterator) { int n = 0x00; if (iterator != null) { while (iterator.hasNext()) { iterator.next(); n++; } } return n; } }