Java AbstractCollection类的isEmpty()方法
isEmpty()
AbstractCollection类的方法检查集合是否为空,即它是否具有零个元素。如果Collectionn没有元素,则返回。
语法如下-
public boolean isEmpty()
要使用Java中的AbstractCollection类,请导入以下包-
import java.util.AbstractCollection;
以下是isEmpty()
在Java中实现AbstractCollection方法的示例-
示例
import java.util.ArrayList; import java.util.AbstractCollection; public class Demo { public static void main(String[] args) { AbstractCollection<Object> absCollection = new ArrayList<Object>(); absCollection.add("Laptop"); absCollection.add("Tablet"); absCollection.add("Mobile"); absCollection.add("E-Book Reader"); absCollection.add("SSD"); absCollection.add("HDD"); System.out.println("AbstractCollection = " + absCollection); System.out.println("Collection is empty? = " + absCollection.isEmpty()); } }
输出结果
AbstractCollection = [Laptop, Tablet, Mobile, E-Book Reader, SSD, HDD] Collection is empty? = false
让我们看另一个例子-
示例
import java.util.ArrayList; import java.util.AbstractCollection; public class Demo { public static void main(String[] args) { AbstractCollection<Object> absCollection = new ArrayList<Object>(); System.out.println("AbstractCollection = " + absCollection); System.out.println("Collection is empty? = " + absCollection.isEmpty()); } }
输出结果
AbstractCollection = [] Collection is empty? = true