Java tutorial
//package com.java2s; import java.util.*; public class Main { /** * Returns the size of the specified collection. If the specified collection is null, this method returns the specified default size. * * @param c Collection of Objects referencing the collection to retrieve the size from. * @param defaultSize int referencing the size to be returned when the collection is null. * @return int referencing the size of the collection, or the default size when the collection is null. */ public static int defaultSizeIfNull(Collection c, int defaultSize) { return (c == null ? defaultSize : c.size()); } /** * Nullsafe method returning the number of elements the incoming collection contains. * * @param c Collection of Objects to count the number of elements from * @return int containing the number of the elements in the collection, or -1 if the collection is null */ public static int size(Collection c) { int size = -1; if (c != null) { size = c.size(); } return size; } /** * Nullsafe method returning the number of entries the incoming map contains. * * @param m Map of Map.Entry objects to count the number of entries from * @return int containing the number of the entries in the map, or -1 if the map is null */ public static int size(Map m) { int size = -1; if (m != null) { size = m.size(); } return size; } }