Return the size of the iterable or 0 if its null - Java Collection Framework

Java examples for Collection Framework:Iterable

Description

Return the size of the iterable or 0 if its null

Demo Code

/*******************************************************************************
 * Copyright (c) 2014 Open Door Logistics (www.opendoorlogistics.com)
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v3
 * which accompanies this distribution, and is available at http://www.gnu.org/licenses/lgpl.txt
 ******************************************************************************/
//package com.java2s;

public class Main {
    /**/*from  ww  w.j a v a  2  s . c  o m*/
     * Return the size of the iterable or 0 if its null
     * @param iterable
     * @return
     */
    public static int size(Iterable<?> iterable) {
        if (iterable == null) {
            return 0;
        }
        int count = 0;
        for (@SuppressWarnings("unused")
        Object o : iterable) {
            count++;
        }
        return count;
    }
}

Related Tutorials