集合判断不用size判断用isEmtpy判断

来源:互联网 发布:网络直播吃饭 编辑:程序博客网 时间:2024/05/17 22:01

Sonar静态代码检查发现下面缺陷:

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

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() method can be O(n).

用isEmpty判断的话可以更加有性能上的优势,而且isEmpty返回的是boolean,而size返回的是一个值,更适合于做判断条件

0 0
原创粉丝点击