CopyOnWriteArraySet Class in Java


Class declaration

public class CopyOnWriteArraySet<E>
   extends AbstractSet<E>
implements Serializable

CopyOnWriteArraySet class uses CopyOnWriteArrayList internally for all of its operations and thus possesses the basic properties of CopyOnWriteArrayList.

  • CopyOnWriteArraySet is a thread-safe.

  • CopyOnWriteArraySet is to be used in Thread based environment where read operations are very frequent and update operations are rare.

  • Iterator of CopyOnWriteArraySet will never throw ConcurrentModificationException.

  • Any type of modification to CopyOnWriteArraySet will not reflect during iteration since the iterator was created.

  • Set modification methods like remove, set and add are not supported in the iteration. This method will throw UnsupportedOperationException.

CopyOnWriteArraySet Methods

Following is the list of important methods available in the CopyOnWriteArraySet class.

Sr.No.
Method & Description
1
add( )

Adds an object to the collection.

2
clear( )

Removes all objects from the collection.

3
contains( )

Returns true if a specified object is an element within the collection.

4
isEmpty( )

Returns true if the collection has no elements.

5
iterator( )

Returns an Iterator object for the collection, which may be used to retrieve an object.

6
remove( )

Removes a specified object from the collection.

7
size( )

Returns the number of elements in the collection.



Example

Following is an example to explain CopyOnWriteArraySet functionality −

Live Demo

import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArraySet;
public class Tester {

   public static void main(String args[]) {
      // create an array list
      CopyOnWriteArraySet<Integer> set = new CopyOnWriteArraySet();
      System.out.println("Initial size of set: " + set.size());
      int count[] = {34, 22,10,60,30,22};
      // add elements to the array list
      for(int i = 0; i < 5; i++) {
         set.add(count[i]);
      }

      System.out.println("Size of set after additions: " + set.size());

      // display the set
      System.out.println("Contents of set: " + set);

      // Remove elements from the array list
      set.remove(10);
      System.out.println("Size of set after deletion: " + set.size());
      System.out.println("Contents of set: " + set);

      try {
         Iterator<Integer> iterator = set.iterator();
         while(iterator.hasNext()) {
            iterator.remove();
         }
      }catch(UnsupportedOperationException e) {
         System.out.println("Method not supported:");
      }
      System.out.println("Size of set: " + set.size());
   }
}

This will produce the following result −

Output

Initial size of set: 0
Size of set after additions: 5
Contents of set: [34, 22, 10, 60, 30]
Size of set after deletion: 4
Contents of set: [34, 22, 60, 30]
Method not supported:
Size of set: 4

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 19-Jun-2020

407 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements