Java CopyOnWriteArraySet containsAll() Method

The CopyOnWriteArraySet.containsAll() method in Java is used to check if the CopyOnWriteArraySet contains all the elements of a specified collection.

Table of Contents

  1. Introduction
  2. containsAll Method Syntax
  3. Examples
    • Checking if All Elements Are Contained
    • Handling Non-Contained Elements
  4. Real-World Use Case
    • Example: Verifying User Presence in a Thread-Safe Set
  5. Conclusion

Introduction

The CopyOnWriteArraySet is a thread-safe variant of Set in Java. It is part of the java.util.concurrent package and is designed for scenarios where read operations are more frequent than write operations. The containsAll method allows you to check if the set contains all the elements of another collection. The CopyOnWriteArraySet achieves thread safety by creating a new copy of the underlying array whenever it is modified.

containsAll() Method Syntax

The syntax for the containsAll method is as follows:

public boolean containsAll(Collection<?> c)
  • The method takes one parameter:
    • c of type Collection<?>, which represents the collection whose elements are to be checked for containment in the set.
  • The method returns true if the set contains all the elements of the specified collection, and false otherwise.

Examples

Checking if All Elements Are Contained

The containsAll method can be used to check if a CopyOnWriteArraySet contains all the elements of another collection.

Example

import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

public class ContainsAllExample {
    public static void main(String[] args) {
        // Creating a CopyOnWriteArraySet with String elements
        CopyOnWriteArraySet<String> names = new CopyOnWriteArraySet<>();

        // Adding elements to the CopyOnWriteArraySet
        names.add("Ravi");
        names.add("Priya");
        names.add("Vijay");

        // Creating another collection with String elements
        ArrayList<String> checkNames = new ArrayList<>();
        checkNames.add("Ravi");
        checkNames.add("Priya");

        // Checking if the set contains all elements of the collection
        boolean containsAll = names.containsAll(checkNames);

        // Printing the result
        System.out.println("Does the set contain all elements? " + containsAll);
    }
}

Output:

Does the set contain all elements? true

Handling Non-Contained Elements

The containsAll method returns false if the CopyOnWriteArraySet does not contain all the elements of the specified collection.

Example

import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

public class NonContainedElementsExample {
    public static void main(String[] args) {
        // Creating a CopyOnWriteArraySet with String elements
        CopyOnWriteArraySet<String> names = new CopyOnWriteArraySet<>();

        // Adding elements to the CopyOnWriteArraySet
        names.add("Ravi");
        names.add("Priya");
        names.add("Vijay");

        // Creating another collection with String elements
        ArrayList<String> checkNames = new ArrayList<>();
        checkNames.add("Ravi");
        checkNames.add("Anita");

        // Checking if the set contains all elements of the collection
        boolean containsAll = names.containsAll(checkNames);

        // Printing the result
        System.out.println("Does the set contain all elements? " + containsAll);
    }
}

Output:

Does the set contain all elements? false

Real-World Use Case

Example: Verifying User Presence in a Thread-Safe Set

A common real-world use case for CopyOnWriteArraySet is managing a thread-safe set of users and verifying if a set of users are present in the main user set.

Example

import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

public class UserSetManager {
    public static void main(String[] args) {
        // Creating a CopyOnWriteArraySet to manage user names
        CopyOnWriteArraySet<String> userSet = new CopyOnWriteArraySet<>();

        // Adding user names to the CopyOnWriteArraySet
        userSet.add("Ravi");
        userSet.add("Priya");
        userSet.add("Vijay");

        // Creating another collection with user names to check
        ArrayList<String> checkUserSet = new ArrayList<>();
        checkUserSet.add("Ravi");
        checkUserSet.add("Priya");

        // Simulating concurrent operations
        Thread checkThread = new Thread(() -> {
            boolean containsAll = userSet.containsAll(checkUserSet);
            System.out.println("Does the user set contain all checked users? " + containsAll);
        });

        Thread addThread = new Thread(() -> {
            userSet.add("Anita");
            System.out.println("Added user: Anita");
        });

        // Starting the threads
        checkThread.start();
        addThread.start();

        // Waiting for the threads to finish
        try {
            checkThread.join();
            addThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // Printing the final user set
        System.out.println("Final user set: " + userSet);
    }
}

Output:

Does the user set contain all checked users? true
Added user: Anita
Final user set: [Ravi, Priya, Vijay, Anita]

In this example, CopyOnWriteArraySet is used to manage a thread-safe set of user names, allowing concurrent operations while verifying if a set of users are present.

Conclusion

The CopyOnWriteArraySet.containsAll() method in Java provides a way to check if a CopyOnWriteArraySet contains all the elements of a specified collection in a thread-safe manner. By understanding how to use this method, you can efficiently manage collections of elements in your Java applications, especially in concurrent environments. The method allows you to verify the presence of multiple elements, making it a versatile tool for data management in multi-threaded scenarios.

Comments