Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is !list.isEmpty() and list.size()>0 equal?

Tags:

java

I've seen code as below:

     if (!substanceList.isEmpty() && (substanceList.size() > 0))
    {
      substanceText = createAmountText(substanceList);
    }

Would the following be a valid refactor?

     if (!substanceList.isEmpty())
    {
      substanceText = createAmountText(substanceList);
    }

I would be grateful for an explanation of the above code and whether the second version may cause errors?

like image 516
uma Avatar asked Feb 17 '18 13:02

uma


People also ask

Is isEmpty same as size 0?

Basically, in implementations of some lists the method isEmpty() checks if the size is zero (and therefore from the point of view of performance they are practically equivalent). In other types of lists (for example the linked lists), however, counting items require more time than to check if it is empty or not.

What is difference between isEmpty and size in Java?

size() can be O(1) or O(N), depending on the data structure ; . isEmpty() is never O(N).

Can an ArrayList have a size of 0?

Another way to check if arraylist contains any element or not, we can check the size of arraylist. If the list size is greater than zero, then list is not empty. If list size is 0, list is empty.

What is the size of empty list in Java?

The size of an empty ArrayList is zero. ArrayList.


2 Answers

If in doubt, read the Javadoc:

Collection.isEmpty():

Returns true if this collection contains no elements.

Collection.size():

Returns the number of elements in this collection

So, assuming the collection is implemented correctly:

collection.isEmpty() <=> collection.size() == 0

Or, conversely:

!collection.isEmpty() <=> collection.size() != 0

Since the number of elements should only be positive, this means that:

!collection.isEmpty() <=> collection.size() > 0

So yes, the two forms are equivalent.

Caveat: actually, they're only equivalent if your collection isn't being modified from another thread at the same time.

This:

!substanceList.isEmpty() && (substanceList.size() > 0)

is equivalent to, by the logic I present above:

!substanceList.isEmpty() && !substanceList.isEmpty()

You can only simplify this to

!substanceList.isEmpty()

if you can guarantee that its value doesn't change in between evaluations of substanceList.isEmpty().

Practically, it is unlikely that you need to care about the difference between these cases, at least at this point in the code. You might need to care about the list being changed in another thread, however, if it can become empty before (or while) executing createAmountText. But that's not something that was introduced by this refactoring.

TL;DR: using if (!substanceList.isEmpty()) { does practically the same thing, and is clearer to read.

like image 171
Andy Turner Avatar answered Oct 16 '22 16:10

Andy Turner


Actually, you can read the source code downloaded in the JDK:

/**
 * Returns <tt>true</tt> if this list contains no elements.
 *
 * @return <tt>true</tt> if this list contains no elements
 */
public boolean isEmpty() {
    return size == 0;
}

I think that this settles all the queries.

like image 21
Raul Luna Avatar answered Oct 16 '22 15:10

Raul Luna