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


Abaric

I want to check if anything in Map, HashMap, ArrayList, List or Collections is empty or empty?

I have this, but it doesn't work when I pass the map:

protected static <T extends Collection, Map> boolean isCollectionMapNullOrEmpty(final T c) {
  if (c == null) {
      return true;
  }

  return c.isEmpty();
 }

fail:

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

Yours isCollectionMapNullOrEmptycompiles and works, but not the way you want.

<T extends Collection, Map>

You have declared two type variables, Tand Map, which Tmust be Collection. Type variables Maphave nothing to do with interfaces Mapand are actually not even used. (Also, you're using a raw interface Collectionhere .) What you're passing aListis Collection, so it compiles. However, it's aMapnot Collection, so compilation doesn't pass Map.

It looks like you want to Tbe 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.

(By the way, you can say one thing and the other: T extends Collection & Map, or no primitive types T extends Collection<?> & Map<?, ?>, but I don't know of any classes that implement both interfaces.)

You can have two overloaded methods, one for a Collectionand one for a Map, which will perform the same function. Here, I take advantage of short-circuiting to combine the following 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 since there is no super interface declaration isEmpty, this looks as good as it gets.

Related


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

Abaric I want to check if anything in Map, HashMap, ArrayList, List or Collections is empty or empty? I have this, but it doesn't work when I pass the map: protected static <T extends Collection, Map> boolean isCollectionMapNullOrEmpty(final T c) { if (c ==

How to check if map or collection is empty?

Zac Delventhal Using traditional objects in JavaScript, it's easy to check if it's null using :Object.keys const emptyObj = {}; console.log(Object.keys(emptyObj).length === 0); // true, i.e. "empty" const populatedObj = { foo: 'bar' }; console.log(Object.key

How to check if map or collection is empty?

Zac Delventhal Using traditional objects in JavaScript, it's easy to check if it's null using :Object.keys const emptyObj = {}; console.log(Object.keys(emptyObj).length === 0); // true, i.e. "empty" const populatedObj = { foo: 'bar' }; console.log(Object.key

How to check if map or collection is empty?

Zac Delventhal Using traditional objects in JavaScript, it's easy to check if it's null using :Object.keys const emptyObj = {}; console.log(Object.keys(emptyObj).length === 0); // true, i.e. "empty" const populatedObj = { foo: 'bar' }; console.log(Object.key

How to check if map or collection is empty?

Zac Delventhal Using traditional objects in JavaScript, it's easy to check if it's null using :Object.keys const emptyObj = {}; console.log(Object.keys(emptyObj).length === 0); // true, i.e. "empty" const populatedObj = { foo: 'bar' }; console.log(Object.key

How to check if map or collection is empty?

Zac Delventhal Using traditional objects in JavaScript, it's easy to check if it's null using :Object.keys const emptyObj = {}; console.log(Object.keys(emptyObj).length === 0); // true, i.e. "empty" const populatedObj = { foo: 'bar' }; console.log(Object.key

Check if a collection is empty in Java: which is the best way?

Vikrant I have two ways to check if the list is empty if (CollectionUtils.isNotEmpty(listName)) and if (listName != null && listName.size() != 0) My arches tell me that the former is better than the latter. But I think the latter is better. Can anyone clari

Check if a collection is empty in Java: which is the best way?

Vikrant I have two ways to check if the list is empty if (CollectionUtils.isNotEmpty(listName)) and if (listName != null && listName.size() != 0) My arches tell me that the former is better than the latter. But I think the latter is better. Can anyone clari

Check if a collection is empty in Java: which is the best way?

Vikrant I have two ways to check if the list is empty if (CollectionUtils.isNotEmpty(listName)) and if (listName != null && listName.size() != 0) My arches tell me that the former is better than the latter. But I think the latter is better. Can anyone clari

Check if a collection is empty in Java: which is the best way?

Vikrant I have two ways to check if the list is empty if (CollectionUtils.isNotEmpty(listName)) and if (listName != null && listName.size() != 0) My arches tell me that the former is better than the latter. But I think the latter is better. Can anyone clari

Python way to check for empty dictionary and null values

Rappen I'm using these kinds of dictionaries, which can be empty, completely empty, collection_aor contain a single level of nested dictionaries that may be empty. It won't have more levels. collection_a = {} collection_b = {"test": {}} print is_empty(collect

Python way to check for empty dictionary and null values

Rappen I'm using these kinds of dictionaries, which can be empty, completely empty, collection_aor contain a single level of nested dictionaries that may be empty. It won't have more levels. collection_a = {} collection_b = {"test": {}} print is_empty(collect

Python way to check for empty dictionary and null values

Rappen I'm using these kinds of dictionaries, which can be empty, completely empty, collection_aor contain a single level of nested dictionaries that may be empty. It won't have more levels. collection_a = {} collection_b = {"test": {}} print is_empty(collect

Correct way to check if variable is null or empty

Zapnologica I had a colleague of mine told me the following code was incorrect and would crash if my variable was null: List<FSKUser> users = null; if (users == null || users.Count() == 0) { return false; } Obviously =nullthis is just for testing purposes

Python way to check for empty dictionary and null values

Rappen I'm using these kinds of dictionaries, which can be empty, completely empty, collection_aor contain a single level of nested dictionaries that may be empty. It won't have more levels. collection_a = {} collection_b = {"test": {}} print is_empty(collect

Python way to check for empty dictionary and null values

Rappen I'm using these kinds of dictionaries, which can be empty, completely empty, collection_aor contain a single level of nested dictionaries that may be empty. It won't have more levels. collection_a = {} collection_b = {"test": {}} print is_empty(collect

Laravel check if collection is empty

Jamie: I've found this in my Laravel web app: @foreach($mentors as $mentor) @foreach($mentor->intern as $intern) <tr class="table-row-link" data-href="/werknemer/{!! $intern->employee->EmployeeId !!}"> <td>{{ $intern->employee->FirstNam

Laravel check if collection is empty

Jamie I've found this in my Laravel web app: @foreach($mentors as $mentor) @foreach($mentor->intern as $intern) <tr class="table-row-link" data-href="/werknemer/{!! $intern->employee->EmployeeId !!}"> <td>{{ $intern->employee->FirstName

Laravel check if collection is empty

Jamie: I've found this in my Laravel web app: @foreach($mentors as $mentor) @foreach($mentor->intern as $intern) <tr class="table-row-link" data-href="/werknemer/{!! $intern->employee->EmployeeId !!}"> <td>{{ $intern->employee->FirstNam

react, map and check for null or empty string doesn't work for me

rookie encoder I have some data to run a map to display. Before that, I check if the data exists, and if so I loop through it and display it. The problem I'm having is that the second map breaks when it encounters an empty string. The array it loops over can b

What's a bulletproof way to check if an NSString is empty/null?

sunny j I'm building an app and I want the contact information of the cleaner to be displayed in a UIButton if the cleaner is assigned to a job, otherwise the UIButton is not displayed. I have "Date" objects that have "cleaner" and "cleanerPhone" strings as pr

What's a bulletproof way to check if an NSString is empty/null?

sunny j I'm building an app and I want to show the contact information of the cleaner in a UIButton if the cleaner is assigned to a job, otherwise not show that UIButton. I have "Date" objects that have "cleaner" and "cleanerPhone" strings as properties. I am

What's a bulletproof way to check if an NSString is empty/null?

sunny j I'm building an app and I want the contact information of the cleaner to be displayed in a UIButton if the cleaner is assigned to a job, otherwise the UIButton is not displayed. I have "Date" objects that have "cleaner" and "cleanerPhone" strings as pr

What's a bulletproof way to check if an NSString is empty/null?

sunny j I'm building an app and I want the contact information of the cleaner to be displayed in a UIButton if the cleaner is assigned to a job, otherwise the UIButton is not displayed. I have "Date" objects that have "cleaner" and "cleanerPhone" strings as pr

Scala: Check for null/empty value and return 0 in a concise way

Cabrese After reading the data file, I need to convert some string values to Ints. The field may be empty, causing an error when trying to convert. Is there an easy way to check for null (or an appropriate value that can be converted to an integer) and just re

What's a bulletproof way to check if an NSString is empty/null?

sunny j I am building an app and I want to show the contact information of the cleaner in a UIButton if the cleaner is assigned to a job, otherwise not show that UIButton. I have "Date" objects that have "cleaner" and "cleanerPhone" strings as properties. I am

What's a bulletproof way to check if an NSString is empty/null?

sunny j I'm building an app and I want the contact information of the cleaner to be displayed in a UIButton if the cleaner is assigned to a job, otherwise the UIButton is not displayed. I have "Date" objects that have "cleaner" and "cleanerPhone" strings as pr