size()AbstractCollection 类的方法返回集合中元素的数量。Integer.MAX_VALUE如果集合中的元素总数超过 Interger.MAX_VALUE,则该方法返回。
语法如下:
public abstract int size()
要在 Java 中使用 AbstractCollection 类,请导入以下包:
import java.util.AbstractCollection;
以下是size()在 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("Count of Elements in the AbstractCollection = " + absCollection.size()); absCollection.remove("Tablet"); absCollection.remove("SSD"); System.out.println("Collection after removing some 2 elements = " + absCollection); System.out.println("Count of Elements in the updated AbstractCollection = " + absCollection.size()); } }输出结果
AbstractCollection = [Laptop, Tablet, Mobile, E-Book Reader, SSD, HDD] Count of Elements in the AbstractCollection = 6 Collection after removing some 2 elements = [Laptop, Mobile, E-Book Reader, HDD] Count of Elements in the updated AbstractCollection = 4