Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to check collections not null & null conditions

we use collections like ArrayList,hashmap & many more.

Number of times we have check condition like whether list is null or not.

We have many ways to chek whether our collection is null or not.

Different ways.

1. if(list==null)
2. if(list.size()==0)
3. if(list.isEmpty())

Also sometimes we also need to check whether list is not null , so we normally check it by these ways

1. if(list!=null)
2. if(list.size()>0)
3. if(!list.isEmpty()) 

Which is best condition or do we need to make some combination of these considering performance of program execution?

like image 940
Java Avatar asked Oct 10 '13 11:10

Java


People also ask

Does collections isEmpty check for null?

isEmpty() doesn't check if a list is null . If you are using the Spring framework you can use the CollectionUtils class to check if a list is empty or not.

Can collection have null values?

Yes, null s are rarely useful in any collection; however, I've seen several times they were used deliberately for implementing different algorithms.

How do you check if the collection is empty or not in Java?

The size() and isEmpty() of java. util. Collection interface is used to check the size of collections and if the Collection is empty or not. isEmpty() method does not take any parameter and does not return any value.


4 Answers

Best combination would be

if(list!=null && !list.isEmpty()){

        //Yeah ,do something
}

One for null check, and then any thing there or not check

like image 90
Suresh Atta Avatar answered Oct 23 '22 23:10

Suresh Atta


1. if(list!=null)

You should make sure that is never the case!!

Read Effective Java 2nd Edition by Joshua Bloch
Item 43: Return empty arrays or collections, not nulls

[...] In summary, there is no reason ever to return null from an array- or collection-valued method instead of returning an empty array or collection. The null-return idiom is likely a holdover from the C programming language, in which array lengths are returned separately from actual arrays. In C, there is no advantage to allocating an array if zero is returned as the length.

In short, let your methods return Collections.emptyList() instead of null and you've got one thing less to worry about.

2. if(list.size()>0)
3. if(!list.isEmpty()) 

That depends. For a simple collection such as ArrayList, they are equivalent. But what if your Collection is actually a live view of a Database query? Calling size() might me a very expensive operation, whereas isEmpty() will always be O(1). I'd say use isEmpty().

Also see Jon Skeet's answer here: https://stackoverflow.com/a/11152624/342852

like image 43
Sean Patrick Floyd Avatar answered Oct 23 '22 22:10

Sean Patrick Floyd


In Java8 you can use Optional to handle null cases properly. For example, both lists animalsNull and animalWithNullElements would be properly handled by the function filterList:

List<String> animalsNull = null;

List<String> animalWithNullElements = new ArrayList<String>();
        animalWithNullElements.add(0, null);
        animalWithNullElements.add(1, "Guybrush Threepwood");
        animalWithNullElements.add(2, null);

private static List<String> filterList(List<String> animals) {
        return Optional.ofNullable(animals)
               .orElseGet(Collections::emptyList)
               .stream()
               .filter(Objects::nonNull)
               .collect(Collectors.toList());
    }
like image 42
Johnny Avatar answered Oct 24 '22 00:10

Johnny


It really depends on what you want to do. If you want to be sure that a list exist AND has some elements then you would use

if (list != null && !list.isEmpty())

If I may give some advice, while returning collections, return by default an empty collection. That way you will avoid nulls.

like image 34
Eel Lee Avatar answered Oct 23 '22 22:10

Eel Lee