Java Program to get the Size of Collection and verify that the Collection is Empty


Collections in Java is a framework which provides classes and interfaces to manipulate group of objects. Collections help in storing and manipulating different types of objects in java. Size of the collection tells us how many elements are present in that particular collection. Java provides various collection classes namely ArrayList, LinkedList, HashSet, and TreeSet, among others. In this section, we are going to write a java program to get the size of collection and verify the collection is empty or not.

The different types of collections in Java are −

  • List − List is an ordered collection of objects which allows duplicate objects to be stored. ArrayList, LinkedList, and Vector are some of the lists.

  • Set − Set is an unordered collection of objects that does not allow duplicate elements. HashSet, LinkedHashSet, and TreeSet are some of the sets used.

  • Map − A Map is used to store objects that are in the form of key-value pairs. Each key must be unique, and the keys can be used to retrieve values. HashMap, LinkedHashMap, and TreeMap are some of the maps used.

  • Queue − Queue is a collection of ordered list of objects that follows FIFO (First In First Out). Elements are added to the end of the queue and removed from the front of the queue. PriorityQueue and LinkedList are some of the queues used.

  • Stack − A Stack is a collection that follows LIFO (Last In First Out). Elements are added and removed from the top of the stack.

Syntax

Creating a Collection Object

Collection<datatype> objectname = new Collectiontype;

size() − It returns the size of the collection.

collectionObject.size()

isEmpty() − It returns boolean value if the collection is empty.

collectionObject.isEmpty()

Finding Size of Collection and verifying Collection is Empty or not

In this example, we will find the size of a collection using size() method and we will check whether the collection is empty or not using isEmpty() method.

Algorithm

  • Create a collection object and add elements to the collection.

  • Use size() method to get the size of collection and store in a variable.

  • Print the value.

  • Use isEmpty() method to check if the collection is empty or not.

Example

In this example, we first create a Collection Object ‘c’ using ArrayList () constructor.This, suggests that we create a ArrayList collection. We then add a few elements using “add()” method . Then, we find the size of collection using size() method and store the value in ‘size’ variable and print it. We used the “isEmpty()” method for verifying whether the arraylist is empty or not. If it return false the collection is not empty else it is empty.

import java.util.ArrayList;
import java.util.Collection;
public class Main {
   public static void main(String[] args) {
      Collection<String> c = new ArrayList<>();
      c.add("a");
      c.add("b");
      c.add("c");
      int size = c.size();
      System.out.println("The size of the collection is: " + size);
      boolean flag = c.isEmpty();
      if (flag) {
         System.out.println("The collection is empty.");
      } else {
         System.out.println("The collection is not empty.");
      }    
   }
}

Output

The size of the collection is: 3
The collection is not empty.

Thus, in this article we have discussed how to get the size of a collection and verify whether that collection is Empty or not using Java Programming Language.

Updated on: 16-Aug-2023

101 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements