Check for Multiple Elements in ArrayList in Kotlin | Kotlin ArrayList containsAll Function

The containsAll function in Kotlin is used to check if all elements of a specified collection are present in an ArrayList. This function is part of the Kotlin standard library and provides a convenient way to verify if a list contains all elements of another collection.

Table of Contents

  1. Introduction
  2. containsAll Function Syntax
  3. Understanding containsAll
  4. Examples
    • Basic Usage
    • Checking Multiple Types of Collections
  5. Real-World Use Case
  6. Conclusion

Introduction

The containsAll function allows you to check if all elements of a specified collection are present in an ArrayList. This is useful for scenarios where you need to verify the presence of multiple elements within a list.

containsAll Function Syntax

The syntax for the containsAll function is as follows:

fun <T> ArrayList<T>.containsAll(elements: Collection<T>): Boolean

Parameters:

  • elements: The collection of elements to be checked for presence in the list.

Returns:

  • Boolean: Returns true if all elements of the specified collection are present in the list, false otherwise.

Understanding containsAll

The containsAll function checks if all elements of the specified collection are present in the ArrayList by comparing each element in the collection with the elements in the list. If all elements are found, it returns true; otherwise, it returns false.

Examples

Basic Usage

To demonstrate the basic usage of containsAll, we will create an ArrayList and check if it contains all elements of another list.

Example

fun main() {
    val numbers = arrayListOf(1, 2, 3, 4, 5)
    val sublist1 = listOf(2, 3, 4)
    val sublist2 = listOf(2, 3, 6)

    println("Does the list contain all elements of sublist1? ${numbers.containsAll(sublist1)}")
    println("Does the list contain all elements of sublist2? ${numbers.containsAll(sublist2)}")
}

Output:

Does the list contain all elements of sublist1? true
Does the list contain all elements of sublist2? false

Checking Multiple Types of Collections

This example shows how to use containsAll to check for the presence of multiple types of collections in an ArrayList.

Example

fun main() {
    val fruits = arrayListOf("Apple", "Banana", "Cherry", "Date")
    val sublist1 = listOf("Banana", "Cherry")
    val sublist2 = setOf("Apple", "Date", "Elderberry")

    println("Does the list contain all elements of sublist1? ${fruits.containsAll(sublist1)}")
    println("Does the list contain all elements of sublist2? ${fruits.containsAll(sublist2)}")
}

Output:

Does the list contain all elements of sublist1? true
Does the list contain all elements of sublist2? false

Real-World Use Case

Checking User Permissions

In real-world applications, the containsAll function can be used to check if a user has all required permissions or roles.

Example

data class User(val id: Int, val name: String, val roles: ArrayList<String>)

fun main() {
    val user = User(1, "Alice", arrayListOf("admin", "editor", "viewer"))
    val requiredRoles1 = listOf("admin", "editor")
    val requiredRoles2 = listOf("admin", "contributor")

    println("Does the user have all required roles (requiredRoles1)? ${user.roles.containsAll(requiredRoles1)}")
    println("Does the user have all required roles (requiredRoles2)? ${user.roles.containsAll(requiredRoles2)}")
}

Output:

Does the user have all required roles (requiredRoles1)? true
Does the user have all required roles (requiredRoles2)? false

Conclusion

The containsAll function in Kotlin is a simple and effective way to check if an ArrayList contains all elements of a specified collection. It allows you to verify the presence of multiple elements within a list, making it useful for various applications, including data validation, user permissions, and more. 

By understanding and using the containsAll function, you can effectively manage and manipulate ArrayList collections in your Kotlin applications.

Comments