Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

CopyOnWriteArraySet size() method in Java

  • Last Updated : 26 Nov, 2018

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

Syntax:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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–




My Personal Notes arrow_drop_up
Recommended Articles
Page :