Check for Element in HashSet in Kotlin | Kotlin HashSet contains Function

The contains function in Kotlin is used to check if a specified element is present in a HashSet. This function is part of the Kotlin standard library and provides a convenient way to verify the existence of an element in a set.

Table of Contents

  1. Introduction
  2. contains Function Syntax
  3. Understanding contains
  4. Examples
    • Basic Usage
    • Checking for Different Element Types
  5. Real-World Use Case
  6. Conclusion

Introduction

The contains function allows you to check if a specified element is present in a HashSet. This is useful for scenarios where you need to verify the presence of an element before performing operations based on that element.

contains Function Syntax

The syntax for the contains function is as follows:

operator fun contains(element: E): Boolean

Parameters:

  • element: The element to be checked for presence in the set.

Returns:

  • Boolean: Returns true if the specified element is present in the set, false otherwise.

Understanding contains

The contains function checks if the specified element is present in the HashSet. If the element is found, it returns true; otherwise, it returns false.

Examples

Basic Usage

To demonstrate the basic usage of contains, we will create a HashSet and check if specific elements are present in the set.

Example

fun main() {
    val set = hashSetOf("Apple", "Banana", "Cherry")
    val hasApple = set.contains("Apple")
    val hasDate = set.contains("Date")

    println("Does the set contain 'Apple'? $hasApple")
    println("Does the set contain 'Date'? $hasDate")
}

Output:

Does the set contain 'Apple'? true
Does the set contain 'Date'? false

Checking for Different Element Types

This example shows how to use contains to check for the presence of different types of elements in a HashSet.

Example

fun main() {
    val set = hashSetOf(1, 2, 3, 4, 5)
    val hasThree = set.contains(3)
    val hasSix = set.contains(6)

    println("Does the set contain 3? $hasThree")
    println("Does the set contain 6? $hasSix")
}

Output:

Does the set contain 3? true
Does the set contain 6? false

Real-World Use Case

Checking for Active Users

In real-world applications, the contains function can be used to check if a user is in a set of active users.

Example

fun main() {
    val activeUsers = hashSetOf("user1", "user2", "user3")
    val userToCheck = "user2"

    if (activeUsers.contains(userToCheck)) {
        println("$userToCheck is an active user.")
    } else {
        println("$userToCheck is not an active user.")
    }
}

Output:

user2 is an active user.

Conclusion

The contains function in Kotlin is a simple and effective way to check if a specified element is present in a HashSet. It allows you to verify the presence of elements, making it useful for various applications, including data validation and session management. 

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

Comments