A method to check if a Collection or Map is empty or null?

how to check hashmap value is null or empty in java
check if map is empty or null java
null check for map in java 8
check if map is empty javascript
groovy check if map is empty
check if list is null java
check if map is empty apex
check if map is empty c++

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();


  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>)

Your isCollectionMapNullOrEmpty compiles and works, but not the way you intended.

<T extends Collection, Map>

You've declared two type variables, T and Map, where T must be a Collection. The type variable Map has nothing to do with the interface Map, and is in fact not even used. (Also, you used the raw Collection interface here.) You are passing aList which is a Collection, so it compiles. However, aMap is not a Collection, so the compilation fails passing in a Map.

It looks like you wanted T to be either a Collection or a Map. But Java's generics don't work that way; you can't declare a type parameter to be one thing or another thing.

(As an aside, you can say one thing and another thing: T extends Collection & Map, or without raw types, T extends Collection<?> & Map<?, ?>, but I'm not aware of any classes that implement both interfaces.)

You can have two overloaded methods, one for a Collection and one for a Map, that will perform the same functionality. Here, I've taken advantage of short-circuiting to combine the statements:

protected static boolean isCollectionMapNullOrEmpty(final Collection<?> c) {
    return c == null || c.isEmpty();
protected static boolean isCollectionMapNullOrEmpty(final Map<?, ?> m) {
    return m == null || m.isEmpty();

Both methods have the same exact code, but because there is no super-interface declaring isEmpty, this appears to be as good as it gets.

Check if Map is Null or Empty in Java, The java.util.HashMap.isEmpty() method of HashMap class is used to check for the Return Value: The method returns boolean true if the map is empty or does not Program 1: Mapping String Values to Integer Keys. Java-Collections. I am not getting how can i check it against NULL or empty. i tried following strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\WMI") Set colItems = objWMIService.ExecQuery( _ "SELECT * FROM MSFC_FCAdapterHBAAttributes") If colItems Is Nothing Then Wscript.echo "Connected to SAN" Else Wscript.echo "Not

if(storeMAPDetails.size==0) You can do this directly to check that your map is empty or not !

HashMap isEmpty() Method in Java, In additional, Spring has universal utility method for null-safe check array, CharSequence, Collection and/or Map: // class org.springframework.util  Java null-safe Collection empty check. Spring has universal utility method for null-safe check array, CharSequence, Collection and/or Map:

Apache Collections provides a static method which does this. Is there a reason you are writing your own implementation?

Java null-safe Collection empty check – Denis Migol, Checking non-empty list. isNotEmpty() method of CollectionUtils can be used to check if a list is not empty without worrying about null list. So null check is not  map(Collection::stream) extracts the value contained in the Optional object as an argument to the map method (Collection.stream()) orElseGet(Stream::empty) returns the fallback value in the event that the Optional object is empty, i.e the passed-in collection is null .

You have the method (link) :

boolean org.apache.commons.collections.CollectionUtils.isEmpty(Collection coll)

wich has null-safe check if the specified collection is empty. This way you don't have to be checking for null values or defining custom methods

Commons Collections - Safe Empty Checks, isEmpty() Method - The isEmpty() method is used to check if this map contains The method call returns 'true' if this map contains no key-value mappings. public class HashMapDemo { public static void main(String args[]) { // create isEmpty(); // check the boolean value System.out.println("Is hash map empty: " + val); } }. The java.util.HashMap.isEmpty() method of HashMap class is used to check for the emptiness of the map. The method returns True if no key-value pair or mapping is present in the map else False.

java.util.HashMap.isEmpty() Method, Program for How to Check if a HashMap is Empty or Not. import java.util.*; public class HashMapEmptyExample { public static void main(String args[])  Checking if a generic collection is empty with Linq and C# 23 January 2013 Posted in Generics, Linq, c#. This is a common issue for many developers. How do you quickly and efficiently determine whether a given non-null collection contains any valid non-empty elements .

How to check if a HashMap is empty or not?, Explore how to create a null-safe stream from a given collection. First, the null check gets in the way of the business logic decreasing the as an argument to the map method (Collection.stream()); orElseGet(Stream::empty)  Although we eliminated the need for a null check on the caller of this API, we used it to return an empty response. To avoid this, Optional provides an ofNullable method that returns an Optional with the specified value, or empty, if the value is null :

Java Null-Safe Streams from Collections, We are using isEmpty() method of HashMap class to perform this check. String​>(); // Checking whether HashMap is empty or not /* isEmpty() method signature  Well, first we have to check if the movie object is null, and if it is, return a not found. And the problem is, by the signature of the method alone you can't tell. But go ahead and read the last phrase of the documentation in the above image. See, there's some documentation to tell you that it may return a null value.

  • <T extends Collection, Map> declares two type variables: T which is bounded by the raw Collection, and Map which is unbounded.
  • You can't do it without casting. Sounds like an XY problem.
  • Write two methods, one for maps and one for collections