Checking if a collection is empty in Java: which is the best method?
You should absolutely use isEmpty(). Computing the size() of an arbitrary list could be expensive. Even validating whether it has any elements can be expensive, of course, but there’s no optimization for size() which can’t also make isEmpty() faster, whereas the reverse is not the case. For example, suppose you had a linked list structure … Read more