Java tutorial
//package com.java2s; //License from project: Apache License import java.util.Collection; import java.util.Map; public class Main { /** * Find the common element type of the given Collection, if any. * @param collection the Collection to check * @return the common element type, or <code>null</code> if no clear * common type has been found (or the collection was empty) */ public static Class<?> findCommonElementType(Collection collection) { if (isEmpty(collection)) { return null; } Class<?> candidate = null; for (Object val : collection) { if (val != null) { if (candidate == null) { candidate = val.getClass(); } else if (candidate != val.getClass()) { return null; } } } return candidate; } public static boolean isEmpty(Collection<?> collection) { return (collection == null || collection.isEmpty()); } /** * Return <code>true</code> if the supplied Map is <code>null</code> * or empty. Otherwise, return <code>false</code>. * @param map the Map to check * @return whether the given Map is empty */ public static boolean isEmpty(Map map) { return (map == null || map.isEmpty()); } }