Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementation of isEmpty()

I'm trying to figure some basic things out. I was exploring standard library ArrayList.java when found that ArrayList has implementation of method isEmpty().

ArrayList.java:

public boolean isEmpty() {
    return size == 0;
}

ArrayList extends AbstractList extends AbstractCollection. And AbstractCollection has implementation of isEmpty as well:

public boolean isEmpty() {
    return size() == 0;
}

I'm just trying to get the logic? Why ArrayList implements already implemented method? What for?

P.S. ArrayList also has size

public int size() {
    return size;
}
like image 842
AndreiKud Avatar asked Sep 14 '26 21:09

AndreiKud


1 Answers

The ArrayList version is a minor, but effective, optimisation.

like image 184
Bathsheba Avatar answered Sep 16 '26 10:09

Bathsheba