Google Guava isNullOrEmpty for collections

JavaGuava

Java Problem Overview


I see that Guava has isNullOrEmpty utility method for Strings

Strings.isNullOrEmpty(str)

Do we have anything similar for Lists? Something like

Lists.isNullOrEmpty(list)

which should be equivalent to

list == null || list.isEmpty()

Also, do we have anything similar for Arrays? Something like

Arrays.isNullOrEmpty(arr)

which should be equivalent to

arr == null || arr.length == 0

Java Solutions


Solution 1 - Java

No, this method does not exist in Guava and is in fact in our "idea graveyard."

We don't believe that "is null or empty" is a question you ever really want to be asking about a collection.

If a collection might be null, and null should be treated the same as empty, then get all that ambiguity out of the way up front, like this:

Set<Foo> foos = NaughtyClass.getFoos();
if (foos == null) {
  foos = ImmutableSet.of();
}

or like this (if you prefer):

Set<Foo> foos = MoreObjects.firstNonNull(
    NaughtyClass.getFoos(), ImmutableSet.<Foo>of());

After that, you can just use .isEmpty() like normal. Do this immediately upon calling the naughty API and you've put the weirdness behind you, instead of letting it continue on indefinitely.

And if the "null which really means empty collection" is not being returned to you, but passed to you, your job is easy: just let a NullPointerException be thrown, and make that caller shape up.

Solution 2 - Java

One thing you will tend to find throughout Guava is that they tend to be very antagonistic toward nulls. The authors want to discourage you from using null as much as you probably do, and providing utility methods to make it easier to use null would be counterproductive against this end.

If you want to use Guava's paradigm, consider if the source of this collection (or array) really ought to optionally return null. If not, consider marking it @NonNull and return empty collections instead of null. Or as a parameter to a function that is expecting no null-valued lists, consider using Preconditions.checkNotNull, which throws an exception if a null is (unexpectedly) passed in.

If null really is legitimate, list == null || list.isEmpty() is not that hard.

Solution 3 - Java

There's a CollectionUtils.isEmpty() in commons-collections.

Solution 4 - Java

Spring Framework has specialized util class called CollectionUtils. And the method you are looking for is: org.springframework.util.CollectionUtils.isEmpty. It returns true for null and empty collections.

And for arrays there is org.springframework.util.ObjectUtils.isEmpty method which behaves pretty the same.

Solution 5 - Java

Apache CollectionUtils 4 has a method CollectionUtils.emptyIfNull() that returns a empty list if the collection is null. This is very useful in a foreach loop, so you dont need to do a null check before iterating

Solution 6 - Java

My solution is : MoreObjects.firstNonNull(list, Collections. emptyList())

I am using Guava MoreObjects with JDK Collections.

 @Test
public void listnull() {
List<String> list = null;

for (String str : MoreObjects.firstNonNull(list, Collections.<String> emptyList())) {
    System.out.println(str);
}

list = new ArrayList<String>();
list.add("http://stackoverflow.com/");

for (String str : MoreObjects.firstNonNull(list, Collections.<String> emptyList())) {
    System.out.println(str);
}
}

Solution 7 - Java

Look at Appache Collections CollectionUtils.isEmpty() returns true if collection is null or empty

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRameshView Question on Stackoverflow
Solution 1 - JavaKevin BourrillionView Answer on Stackoverflow
Solution 2 - JavaRayView Answer on Stackoverflow
Solution 3 - JavaSteven SchlanskerView Answer on Stackoverflow
Solution 4 - JavaLukaszView Answer on Stackoverflow
Solution 5 - JavaJeffreyView Answer on Stackoverflow
Solution 6 - JavajetonView Answer on Stackoverflow
Solution 7 - JavaAnastasiia KuzinaView Answer on Stackoverflow