Java CopyOnWriteArraySet contains() Method

The CopyOnWriteArraySet.contains() method in Java is used to check if a CopyOnWriteArraySet contains a specified element.

Table of Contents

  1. Introduction
  2. contains Method Syntax
  3. Examples
    • Checking if an Element Exists
    • Handling Non-Existent Elements
  4. Real-World Use Case
    • Example: Checking 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 contains method allows you to check if the set contains a specified element. The CopyOnWriteArraySet achieves thread safety by creating a new copy of the underlying array whenever it is modified.

contains() Method Syntax

The syntax for the contains method is as follows:

public boolean contains(Object o)
  • The method takes one parameter:
    • o of type Object, which represents the element whose presence in the set is to be tested.
  • The method returns true if the set contains the specified element, and false otherwise.

Examples

Checking if an Element Exists

The contains method can be used to check if a CopyOnWriteArraySet contains a specified element.

Example

import java.util.concurrent.CopyOnWriteArraySet;

public class ContainsExample {
    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");

        // Checking if the set contains an element
        boolean containsPriya = names.contains("Priya");

        // Printing the result
        System.out.println("Does the set contain 'Priya'? " + containsPriya);
    }
}

Output:

Does the set contain 'Priya'? true

Handling Non-Existent Elements

The contains method returns false if the specified element is not found in the CopyOnWriteArraySet.

Example

import java.util.concurrent.CopyOnWriteArraySet;

public class NonExistentElementExample {
    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");

        // Checking if the set contains a non-existent element
        boolean containsAnita = names.contains("Anita");

        // Printing the result
        System.out.println("Does the set contain 'Anita'? " + containsAnita);
    }
}

Output:

Does the set contain 'Anita'? false

Real-World Use Case

Example: Checking User Presence in a Thread-Safe Set

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

Example

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");

        // Simulating concurrent operations
        Thread checkUserThread1 = new Thread(() -> {
            boolean containsPriya = userSet.contains("Priya");
            System.out.println("Does the set contain 'Priya'? " + containsPriya);
        });

        Thread checkUserThread2 = new Thread(() -> {
            boolean containsAnita = userSet.contains("Anita");
            System.out.println("Does the set contain 'Anita'? " + containsAnita);
        });

        // Starting the threads
        checkUserThread1.start();
        checkUserThread2.start();

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

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

Output:

Does the set contain 'Priya'? true
Does the set contain 'Anita'? false
Final user set: [Ravi, Priya, Vijay]

In this example, CopyOnWriteArraySet is used to manage a thread-safe set of user names, allowing concurrent operations while checking for the presence of specific users.

Conclusion

The CopyOnWriteArraySet.contains() method in Java provides a way to check if a CopyOnWriteArraySet contains a specified element 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 elements, making it a versatile tool for data management in multi-threaded scenarios.

Comments