I want to check if a Map, HashMap, ArrayList, List or anything from Collections is Empty or null?
I have this, but its not working when I pass a Map:
protected static <T extends Collection, Map> boolean isCollectionMapNullOrEmpty(final T c) { if (c == null) { return true; } return c.isEmpty(); } Failure:
List<String> aList = Arrays.asList("a1", "a2", "a4"); Map<String, Object> aMap = new HashMap<String, Object>(); aMap.put("a2", "foo"); aMap.put("a1", "foo"); aMap.put("a3", "foo"); aMap.put("a4", "foo"); System.out.println(isCollectionMapNullOrEmpty(aList)); // works // fails with The method isCollectionMapNullOrEmpty(T) in the type LearnHashMap is not applicable for the arguments (Map<String,Object>) System.out.println(isCollectionMapNullOrEmpty(aMap));
<T extends Collection, Map>declares two type variables:Twhich is bounded by the rawCollection, andMapwhich is unbounded.