Java tutorial
//package com.java2s; import java.lang.reflect.Array; import java.util.Collection; import java.util.Enumeration; import java.util.Iterator; import java.util.Map; public class Main { public static boolean isEmpty(Map<?, ?> map) { return (map == null) || (map.size() == 0); } public static boolean isEmpty(Collection<?> collection) { return (collection == null) || (collection.size() == 0); } /** * Gets the size of the collection/iterator specified. * <p> * This method can handles objects as follows * <ul> * <li>Collection - the collection size * <li>Map - the map size * <li>Array - the array size * <li>Iterator - the number of elements remaining in the iterator * <li>Enumeration - the number of elements remaining in the enumeration * </ul> * * @param object * the object to get the size of * @return the size of the specified collection * @throws IllegalArgumentException * thrown if object is not recognised or null * @since Commons Collections 3.1 */ public static int size(Object object) { int total = 0; if (object instanceof Map) { total = ((Map<?, ?>) object).size(); } else if (object instanceof Collection) { total = ((Collection<?>) object).size(); } else if (object instanceof Object[]) { total = ((Object[]) object).length; } else if (object instanceof Iterator) { Iterator<?> it = (Iterator<?>) object; while (it.hasNext()) { total++; it.next(); } } else if (object instanceof Enumeration) { Enumeration<?> it = (Enumeration<?>) object; while (it.hasMoreElements()) { total++; it.nextElement(); } } else if (object == null) { throw new IllegalArgumentException("Unsupported object type: null"); } else { try { total = Array.getLength(object); } catch (IllegalArgumentException ex) { throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName()); } } return total; } }