您的位置:首页 > 其它

Collection.isEmpty() should be used to test for emptiness

2017-07-26 17:29 351 查看
Using Collection.size() to test for emptiness works, but using Collection.isEmpty() makes the code more readable and can be more performant. The time complexity of any isEmpty() method implementation should be O(1) whereas some implementations of size() can be O(n).

Noncompliant Code Example

// Noncompliant
if (myCollection.size() == 0) {
/* ... */
}


Compliant Solution

if (myCollection.isEmpty()) {
/* ... */
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐