I don't believe that "is null or empty" is a question you ever really want to be asking about a collection.
Up vote 7 down vote favorite share g+ share fb share tw.
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 guava link|improve this question edited Feb 19 at 1:00mikera27k22989 asked Aug 2 '11 at 9:37Ramesh42618 69% accept rate.
I 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, why not get all the ambiguity out of the way right away, like this: Set foos = NaughtyClass.getFoos(); if (foos == null) { foos = ImmutableSet.of(); } or like this: Set foos = Objects. FirstNonNull( NaughtyClass.getFoos(), ImmutableSet.of()); After which you can use .isEmpty() like usual.
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.
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 at 19:02 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 – Kevin Bourrillion Mar 14 at 17:25.
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.
There's a CollectionUtils.isEmpty() in commons-collections.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.