Java HashSet containsAll() Method

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

Table of Contents

  1. Introduction
  2. containsAll Method Syntax
  3. Examples
    • Basic Example
    • Real-World Use Case: Validating Subset of User Permissions
  4. Conclusion

Introduction

The HashSet class in Java is part of the Java Collections Framework and implements the Set interface. A HashSet is used to store unique elements and provides constant-time performance for basic operations like add, remove, contains, and size. The containsAll method is used to check if all elements of a specified collection are present in the HashSet.

containsAll() Method Syntax

The syntax for the containsAll method is as follows:

public boolean containsAll(Collection<?> c)
  • c: The collection to be checked for containment in the HashSet.
  • Returns: true if the HashSet contains all elements of the specified collection; false otherwise.

Examples

Basic Example

In this example, we'll use the containsAll method to check if a HashSet contains all elements of a specified List.

Example

import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;

public class HashSetContainsAllExample {
    public static void main(String[] args) {
        // Creating a HashSet of Strings
        HashSet<String> set = new HashSet<>();
        set.add("Java");
        set.add("Python");
        set.add("C");
        set.add("JavaScript");

        // Creating a List of Strings
        List<String> list = new ArrayList<>();
        list.add("Java");
        list.add("C");

        // Checking if the HashSet contains all elements of the List
        boolean containsAll = set.containsAll(list);

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

Output:

Does the HashSet contain all elements of the List? true

Real-World Use Case: Validating Subset of User Permissions

In a web application, you might want to check if a user's permissions include all required permissions for a specific action.

Example

import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;

public class UserPermissionsExample {
    public static void main(String[] args) {
        // Creating a HashSet to store user permissions
        HashSet<String> userPermissions = new HashSet<>();
        userPermissions.add("READ");
        userPermissions.add("WRITE");
        userPermissions.add("DELETE");

        // Creating a List of required permissions for a specific action
        List<String> requiredPermissions = new ArrayList<>();
        requiredPermissions.add("READ");
        requiredPermissions.add("WRITE");

        // Checking if the user has all required permissions
        boolean hasRequiredPermissions = userPermissions.containsAll(requiredPermissions);

        // Printing the result
        System.out.println("Does the user have all required permissions? " + hasRequiredPermissions);
    }
}

Output:

Does the user have all required permissions? true

Example: Checking Inventory for Required Items

In an inventory management system, you might want to check if the inventory contains all required items for an order.

Example

import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;

public class InventoryCheckExample {
    public static void main(String[] args) {
        // Creating a HashSet to store inventory items
        HashSet<String> inventoryItems = new HashSet<>();
        inventoryItems.add("Laptop");
        inventoryItems.add("Monitor");
        inventoryItems.add("Keyboard");
        inventoryItems.add("Mouse");

        // Creating a List of required items for an order
        List<String> requiredItems = new ArrayList<>();
        requiredItems.add("Laptop");
        requiredItems.add("Mouse");

        // Checking if the inventory contains all required items
        boolean hasAllItems = inventoryItems.containsAll(requiredItems);

        // Printing the result
        System.out.println("Does the inventory contain all required items? " + hasAllItems);
    }
}

Output:

Does the inventory contain all required items? true

Example: Checking for Required Skills

In a job application system, you might want to check if an applicant's skills include all required skills for a job posting.

Example

import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;

public class SkillsCheckExample {
    public static void main(String[] args) {
        // Creating a HashSet to store applicant's skills
        HashSet<String> applicantSkills = new HashSet<>();
        applicantSkills.add("Java");
        applicantSkills.add("Python");
        applicantSkills.add("SQL");

        // Creating a List of required skills for the job
        List<String> requiredSkills = new ArrayList<>();
        requiredSkills.add("Java");
        requiredSkills.add("SQL");

        // Checking if the applicant has all required skills
        boolean hasAllSkills = applicantSkills.containsAll(requiredSkills);

        // Printing the result
        System.out.println("Does the applicant have all required skills? " + hasAllSkills);
    }
}

Output:

Does the applicant have all required skills? true

Conclusion

The HashSet.containsAll() method in Java provides a way to check if all elements of a specified collection are present in the HashSet. This method is useful in various scenarios, such as validating user permissions, checking inventory for required items, or verifying if an applicant has the necessary skills for a job. By understanding how to use this method, you can efficiently manage and validate collections in your Java applications.

Comments