Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaCopyOnWriteArraySet size() method in Java

CopyOnWriteArraySet size() method in Java

The size() method of CopyOnWriteArraySet returns the size of the Set. It returns the number of elements in the current Set.

Syntax:

public int size()

Return Value: The function returns the number of elements in the current Set.

Below programs illustrate the above function:

Program 1:




// Java Program to illustrate the CopyOnWriteArraySet
// size() method in Java
  
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create object of CopyOnWriteArraySet
        CopyOnWriteArraySet<Integer> ArrSet
            = new CopyOnWriteArraySet<Integer>();
  
        // print CopyOnWriteArraySet
        System.out.println("CopyOnWriteArraySet: "
                           + ArrSet);
  
        // size value print
        System.out.println("size of ArrSet: "
                           + ArrSet.size());
  
        // Add elements
        ArrSet.add(32);
        ArrSet.add(67);
        ArrSet.add(98);
        ArrSet.add(100);
  
        // print CopyOnWriteArraySet
        System.out.println("CopyOnWriteArraySet: "
                           + ArrSet);
  
        // size value print
        System.out.println("size of ArrSet: "
                           + ArrSet.size());
    }
}


Output:

CopyOnWriteArraySet: []
size of ArrSet: 0
CopyOnWriteArraySet: [32, 67, 98, 100]
size of ArrSet: 4

Program 2:




// Java Program to illustrate the CopyOnWriteArraySet
// size() method in Java
  
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create object of CopyOnWriteArraySet
        CopyOnWriteArraySet<String> ArrSet
            = new CopyOnWriteArraySet<String>();
  
        // Add elements
        ArrSet.add("gopal");
        ArrSet.add("gfg");
        ArrSet.add("jgec");
        ArrSet.add("sudo");
  
        // print CopyOnWriteArraySet
        System.out.println("CopyOnWriteArraySet: "
                           + ArrSet);
  
        // size value print
        System.out.println("size of ArrSet: "
                           + ArrSet.size());
    }
}


Output:

CopyOnWriteArraySet: [gopal, gfg, jgec, sudo]
size of ArrSet: 4

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CopyOnWriteArraySet.html#size–

RELATED ARTICLES

Most Popular

Recent Comments