Reputation: 3816
I want to verify whether a collection is empty and null
. Could anyone please let me know the best practice.
Currently, I am checking as below:
if (null == sampleMap || sampleMap.isEmpty()) {
// do something
}
else {
// do something else
}
Upvotes: 343
Views: 628135
Reputation: 11597
As an extension to @Jalayn's answer, if your'e using AWS SDK, you can also use CollectionUtils.isNullOrEmpty(..)
References
CollectionUtils.java
CollectionUtils
Upvotes: 0
Reputation: 360
Taking the case you collection is a List, you can simply use this
private boolean collectionIsNotNullAndNotEmpty(List<?> collection) {
return Objects.nonNull(collection) && !collection.isEmpty();
}
Upvotes: 0
Reputation: 9274
If you use the Apache Commons Collections library in your project, you may use the CollectionUtils.isEmpty(...)
and MapUtils.isEmpty(...)
methods which respectively check if a collection or a map is empty or null (i.e. they are "null-safe").
The code behind these methods is more or less what user @icza has written in his answer.
Regardless of what you do, remember that the less code you write, the less code you need to test as the complexity of your code decreases.
Upvotes: 523
Reputation: 455
We'll check a Collection object is empty, null or not. these all methods which are given below, are present in org.apache.commons.collections4.CollectionUtils package.
Check on List or set type of collection Objects.
CollectionUtils.isEmpty(listObject);
CollectionUtils.isNotEmpty(listObject);
Check on Map type of Objects.
MapUtils.isEmpty(mapObject);
MapUtils.isNotEmpty(mapObject);
The return type of all methods is boolean.
Upvotes: 12
Reputation: 1117
When you use spring then you can use
boolean isNullOrEmpty = org.springframework.util.ObjectUtils.isEmpty(obj);
where obj is any [map,collection,array,aything...]
otherwise: the code is:
public static boolean isEmpty(Object[] array) {
return (array == null || array.length == 0);
}
public static boolean isEmpty(Object obj) {
if (obj == null) {
return true;
}
if (obj.getClass().isArray()) {
return Array.getLength(obj) == 0;
}
if (obj instanceof CharSequence) {
return ((CharSequence) obj).length() == 0;
}
if (obj instanceof Collection) {
return ((Collection) obj).isEmpty();
}
if (obj instanceof Map) {
return ((Map) obj).isEmpty();
}
// else
return false;
}
for String best is:
boolean isNullOrEmpty = (str==null || str.trim().isEmpty());
Upvotes: 26
Reputation: 43
For all the collections including map use: isEmpty
method which is there on these collection objects. But you have to do a null check before:
Map<String, String> map;
........
if(map!=null && !map.isEmpty())
......
Upvotes: -4
Reputation: 17466
You can use org.apache.commons.lang.Validate
's "notEmpty" method:
Validate.notEmpty(myCollection)
-> Validate that the specified argument collection is neither null nor a size of zero (no elements); otherwise throwing an exception.
Upvotes: 8
Reputation: 2280
If you use Spring frameworks, then you can use CollectionUtils
to check against both Collections (List, Array) and Map etc.
if(CollectionUtils.isEmpty(...)) {...}
Upvotes: 73
Reputation: 417402
That is the best way to check it. You could write a helper method to do it:
public static boolean isNullOrEmpty( final Collection< ? > c ) {
return c == null || c.isEmpty();
}
public static boolean isNullOrEmpty( final Map< ?, ? > m ) {
return m == null || m.isEmpty();
}
Upvotes: 90
Reputation: 53462
If you need to check for null, that is the way. However, if you have control on this, just return empty collection, whenever you can, and check only for empty later on.
This thread is about the same thing with C#, but the principles applies equally well to java. Like mentioned there, null should be returned only if
- null might mean something more specific;
- your API (contract) might force you to return null.
Upvotes: 2
Reputation: 2859
Personally, I prefer to use empty collections instead of null
and have the algorithms work in a way that for the algorithm it does not matter if the collection is empty or not.
Upvotes: 23