Java Iterable Size getIterableSize(Iterable iterable)

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

Description

get Iterable Size

License

Open Source License

Declaration

public static int getIterableSize(Iterable<?> iterable) 

Method Source Code

//package com.java2s;
/**//from  w  w w  .  ja  va  2  s  .  c  o  m
 * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com
 *
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.md file.
 */

import java.util.Collection;
import java.util.Iterator;

public class Main {
    public static int getIterableSize(Iterable<?> iterable) {
        if (iterable instanceof Collection<?>) {
            return ((Collection<?>) iterable).size();
        } else {
            Iterator<?> it = iterable.iterator();
            int i = 0;
            while (it.hasNext()) {
                i++;
            }
            return i;
        }
    }
}

Related

  1. countElements(Iterable pairs)
  2. getSingle(Iterable it)
  3. getSingleElementOrNull(Iterable coll)
  4. getSingleIfExist(Iterable iterable)
  5. getSingleOrNull(Iterable iterable)