Java Iterable Size size(final Iterable iterable)

Here you can find the source of size(final Iterable iterable)

Description

Calculates the number of generated elements of the given iterable.

License

Open Source License

Parameter

Parameter Description
T The type of elements enumerated by the given iterable.
iterable The given iterable.

Return

The number of elements generated by the iterable.

Declaration

public static <T> int size(final Iterable<T> iterable) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Iterator;

public class Main {
    /**/*from w  w w . j  a v  a2s .c om*/
     * 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;
    }
}

Related

  1. iterableSize(Iterable itrbl)
  2. iterableSizeEq(Iterable itrbl, int k)
  3. singleOrNull(Iterable iterable)
  4. singleOrNull(Iterable iterable)
  5. size(final Iterable iterable)
  6. size(Iterable it)
  7. size(Iterable iterable)
  8. size(Iterable values)
  9. sizeEquals(Iterable iterable, int expectedSize)