How to check if a Collection is empty

Use Collection.size() to test how emptiness works, but use Collection.isEmpty() to make the code more readable and allow for better performance. The time complexity of any isEmpty() method execution should be O(1), while some implementations of scale() can be O\(N).

 

 

Compliant Solution

if (myCollection.isEmpty()) {    // Compliant
  /* ... */
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326082479&siteId=291194637
Recommended