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:



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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–

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.