Java Iterable Count count(Iterable iterable)

Here you can find the source of count(Iterable iterable)

Description

Runs through an Iterable and counts the number of elements.

License

Apache License

Parameter

Parameter Description
iterable is the Iterable to count the elements in.

Return

A long value with the number of elements is returned.

Declaration

public static long count(Iterable<?> iterable) 

Method Source Code

    //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];
 }
    }

Related

  1. count(final Iterable iterable, final int step)
  2. count(Iterable iterable)
  3. count(Iterable i)
  4. count(Iterable iterable)