list.isEmpty()和list.size()>0时间复杂度

List的源码如下所示:(关于Empty和size)

  /**
     * Returns the number of elements in this list.
     *
     * @return the number of elements in this list
     */
    public int size() {
        return size;
    }
 
    /**
     * Returns true if this list contains no elements.
     *
     * @return true if this list contains no elements
     */
    public boolean isEmpty() {
        return size == 0;
    }

如果我要查size时间复杂度是o(n) ,如果是empty o(1);所以判断集合是否为空一般用list.isEmpty()来判断

你可能感兴趣的:(【Java】)