The size() method of Java AbstractCollection class

Java 8Object Oriented ProgrammingProgramming

The size() method of the AbstractCollection class returns the numbers of elements in the collection. The method returns Integer.MAX_VALUE if the total number of elemnts in the collection exceeds the Interger.MAX_VALUE.

The syntax is as follows:

public abstract int size()

To work with AbstractCollection class in Java, import the following package:

import java.util.AbstractCollection;

The following is an example to implement AbstractCollection size() method in Java:

Example

 Live Demo

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());
   }
}

Output

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
raja
Published on 01-Apr-2019 13:10:14
Advertisements