Java AbstractCollection 类中的 size() 方法


AbstractCollection 类的 size() 方法返回集合中的元素数。如果集合中的元素总数超过 Interger.MAX_VALUE,则该方法将返回 Integer.MAX_VALUE。

语法如下

public abstract int size()

要在 Java 中使用 AbstractCollection 类,请导入以下包

import java.util.AbstractCollection;

下面是一个在 Java 中实现 AbstractCollection size() 方法的示例

示例

 动态演示

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

更新日期:30-7-2019

158 浏览

开启您的 职业生涯

完成课程以获得认证

入门
广告
© . All rights reserved.