Here you can find the source of count(Iterable> iterable)
Parameter | Description |
---|---|
iterable | is the Iterable to count the elements in. |
public static long count(Iterable<?> iterable)
//package com.java2s; //License from project: Apache License import java.util.Iterator; public class Main { /**/*from w ww.j a v a2s.c o m*/ * Runs through an {@link Iterable} and counts the number of elements. * * @param iterable * is the {@link Iterable} to count the elements in. * @return A long value with the number of elements is returned. */ public static long count(Iterable<?> iterable) { long[] count = { 0 }; iterable.forEach(c -> count[0]++); return count[0]; } /** * Runs through an {@link Iterator} and counts the number of elements. * * @param iterator * is the {@link Iterator} to count the elements in. * @return The current number of elements is returned. */ public static long count(Iterator<?> iterator) { long[] count = { 0 }; iterator.forEachRemaining(c -> count[0]++); return count[0]; } }