111

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

7 Answers 7

152

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.

13
  • 7
    What if some framework is setting it field to null and you can't control it (spring framework when on binding to list) although you explicitly set and use empty list in the object.
    – OMax
    Mar 11, 2012 at 19:02
  • 15
    My answer is the same: try to remove the ambiguity by normalizing null to an empty collection as soon as you can. If you just can't, then okay, you might be one of the <1% of users who really want to write their own trivial isNullOrEmpty(Collection) helper. Mar 14, 2012 at 17:25
  • 8
    (or skip the helper and just write if (collection == null || collection.isEmpty()) in those cases where you need it! Is that so bad?) Aug 28, 2012 at 17:43
  • 14
    This is an opinionated answer that doesn't actually address the question at all. It also goes against defensive programming principles.
    – Neovibrant
    Oct 16, 2013 at 16:23
  • 12
    @Neovibrant since the question is about the capabilities of the Guava library, and the answer is from one of the lead developer of Guava, the answer is kinda implied: "No, Guava doesn't have that". The answer may be improved by starting with that statement though :) Nov 10, 2013 at 21:50
37

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.

1
  • 11
    it's inconsistent. The same argument could be made for Strings, yet there is a Strings.isNullOrEmpty() Apr 16, 2015 at 18:20
32

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

4
  • 1
    Unfortunately, it seems to have been removed in more recent versions. See org.apache.commons.collections15.CollectionUtils.
    – spaaarky21
    May 15, 2012 at 20:38
  • 1
    Looking at the Javadoc from apache.commons.collection the methods isEmpty() and isNotEmpty() still exist in the latest version. collections15.CollectionUtils seems like an independent project trying to provide generic support.
    – proko
    Aug 13, 2012 at 13:18
  • 1
    CollectionUtils.isEmpty() is useful when working with legacy apps, when you cannot change the up front code. Oct 9, 2012 at 9:58
  • 1
    we know this, but one was asking about guava not about apache commons.
    – To Kra
    Dec 3, 2015 at 9:27
11

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.

1
  • 6
    Although it's probably safe to use it, this class is marked with "Mainly for internal use within the framework" comment. This means its interface might change on a Spring upgrade and you will have to do additional work to make it work again. Dec 3, 2013 at 1:00
3

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

1

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);
}
}
4
  • 1
    This is exactly what's suggested in the top answer, except you don't use an ImmutableList.
    – dimo414
    May 18, 2015 at 20:41
  • I think negative because one wanted simple & quick solution like apache commons methods inNotEmpty ...
    – To Kra
    Dec 3, 2015 at 9:28
  • Help Center > Privileges > Vote Down When should I vote down? Use your downvotes whenever you encounter an egregiously sloppy, no-effort-expended post, or an answer that is clearly and perhaps dangerously incorrect. You have a limited number of votes per day, and answer down-votes cost you a tiny bit of reputation on top of that; use them wisely.stackoverflow.com/help/privileges/vote-down
    – jeton
    Dec 11, 2015 at 18:01
  • Do you think my answer is "egregiously sloppy, no-effort-expended post, or an answer that is clearly and perhaps dangerously incorrect"? If not, why are you negative voting?
    – jeton
    Dec 11, 2015 at 18:02
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.