9

I have read so many article as on difference between isEmpty() and size() > 0 for check that collection is empty or not and found that isEmpty() have perfomance over size() but I could not understand easily why perfomance of isEmpty() is good even though inside isEmpty() is only size == 0 ?

My questions are :

  1. Can any one explain easily in which scenario isEmpty() is faster as well as when to use isEmpty() and size() function for checking if collection is empty or not?

  2. Can any one explain this, using code or other way(Diagrams,graphs etc) so that any beginner can understand easily?

Kamlesh Kanazariya
  • 1,209
  • 2
  • 15
  • 32
  • 1
    Hope you have checked: http://stackoverflow.com/questions/11152536/check-if-a-collection-is-empty-in-java-which-is-the-best-method http://stackoverflow.com/questions/9341740/ifliststr-size-0-versus-ifliststr-isempty http://stackoverflow.com/questions/1508975/why-is-list-size0-slower-than-list-isempty-in-java – Pramod Karandikar Dec 17 '14 at 12:14
  • Thanks @Pam but I had already read this articles but not getting the exact difference as well as how **O(1) and O(n)** is calculated in terms of performance. – Kamlesh Kanazariya Dec 17 '14 at 12:18
  • 1
    @KamleshKanazariya you might want to read about complexity http://stackoverflow.com/questions/3255/big-o-how-do-you-calculate-approximate-it – MihaiC Dec 17 '14 at 12:21
  • I've edited my answer now to give you an example of a standard JDK collection that's constant-time for `isEmpty()` and linear for `size()`. – chiastic-security Dec 17 '14 at 12:24
  • 1
    @OldCurmudgeon This isn't a duplicate of either of the tagged questions. Both of those talk about *lists* specifically, rather than *collections* in general (despite the title of the second one). – chiastic-security Dec 17 '14 at 12:34
  • Thank you very much @MihaiC , I am reading that article but question is that `isEmpty()` function call internally `size()` function so how can possible that difference in performance? – Kamlesh Kanazariya Dec 17 '14 at 12:56
  • @KamleshKanazariya no, `isEmpty()` is not *always* internally just a `size()` call. See my answer for an example of where it isn't. – chiastic-security Dec 17 '14 at 13:31

3 Answers3

11

It might be that some collections just use size()==0 inside their isEmpty() method, but that doesn't mean that they all do. The default implementation of isEmpty() just checks whether size() == 0, but a particular collection is free to override this with something else if it's more efficient.

Here's a nice example. The ConcurrentSkipListSet documentation says:

Beware that, unlike in most collections, the size method is not a constant-time operation.

For this class, you'd certainly want to use isEmpty() rather than size() == 0.

(To understand why it's true for a skip list, you'd need to read up on how skip lists work, but do come back and ask another question about them if you want to know more.)

chiastic-security
  • 20,430
  • 4
  • 39
  • 67
  • 2
    LinkedList's implementation of size() doesn't traverse the list, but rather keeps track of size when the list is modified. – Martin Dec 17 '14 at 12:18
  • @Martin yes, I've just added something about this. This is why I'd written "linked list" rather than `LinkedList`. The standard implementation keeps track of the size, but that's not to say that every collection *must* do this. – chiastic-security Dec 17 '14 at 12:19
  • @Martin I've deleted the linked list example and replaced with a clear and concrete example. – chiastic-security Dec 17 '14 at 12:22
6

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

Bhargav Modi
  • 2,605
  • 3
  • 29
  • 49
  • 2
    AbstractCollection : `public boolean isEmpty() { return size() == 0; }`. Which means size() and isEmpty() have the same complexity in all the major collections – Eran Dec 17 '14 at 12:15
  • 1
    you just copied one answer from here http://stackoverflow.com/questions/9341740/ifliststr-size-0-versus-ifliststr-isempty , just point the OP to that question ... – MihaiC Dec 17 '14 at 12:17
  • @MihaiC I told that I had read it from stack overflow itself but this doesnot mean that it has the copy right. :) – Bhargav Modi Dec 17 '14 at 12:22
  • @Eran in major `collections` but not in all collections you take look on `linkedlist` implementation. – Bhargav Modi Dec 17 '14 at 12:25
  • 1
    @BhargavModi LinkedList extends AbstractSequentialList which extends AbstractList which extends AbstractCollection, so LinkedList has the same implementation. – Eran Dec 17 '14 at 12:30
1

The top reasons for using isEmpty rather than size would be:

it is more expressive (the code is easier to read and to maintain)

it is faster, in some cases by orders of magnitude.

Detailed explanation here

++ same question asked here

Community
  • 1
  • 1
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • Thanks @ankur-singhal , I am agree with your answer but currently i am focusing on difference on **performance** rather than code style or maintenance. – Kamlesh Kanazariya Dec 17 '14 at 12:37
  • Performance-wise, `isEmpty()` may *allow* an implementation to do some optimization compared to `size()`. However, if and to what extend `isEmpty()` is performance-optimized is completely up to the specific implementation class used. I think the only thing one can rely on is that `O(isEmpty) <= O(size)`. Hence, using `isEmpty` does make for more expressive code and is kind of a best effort on performance. – JimmyB Dec 17 '14 at 12:44