Add Elements to HashSet in Kotlin | Kotlin HashSet add Function

The add function in Kotlin is used to add an element to a HashSet. This function is part of the Kotlin standard library and provides a convenient way to insert elements into a set.

Table of Contents

  1. Introduction
  2. add Function Syntax
  3. Understanding add
  4. Examples
    • Basic Usage
    • Adding Duplicate Elements
  5. Real-World Use Case
  6. Conclusion

Introduction

The add function allows you to insert an element into a HashSet. If the element is already present in the set, the set remains unchanged, and the function returns false. If the element is not present, it is added to the set, and the function returns true.

add Function Syntax

The syntax for the add function is as follows:

fun add(element: E): Boolean

Parameters:

  • element: The element to be added to the set.

Returns:

  • Boolean: Returns true if the element was added to the set, false if the element was already present.

Understanding add

The add function checks if the specified element is already present in the HashSet. If the element is not present, it is added to the set, and the function returns true. If the element is already present, the function returns false, and the set remains unchanged.

Examples

Basic Usage

To demonstrate the basic usage of add, we will create a HashSet and add elements to it.

Example

fun main() {
    val set = hashSetOf<String>()
    val added1 = set.add("Apple")
    val added2 = set.add("Banana")

    println("Set after adding elements: $set")
    println("Was 'Apple' added? $added1")
    println("Was 'Banana' added? $added2")
}

Output:

Set after adding elements: [Apple, Banana]
Was 'Apple' added? true
Was 'Banana' added? true

Adding Duplicate Elements

This example shows how add handles duplicate elements.

Example

fun main() {
    val set = hashSetOf("Apple", "Banana")
    val added1 = set.add("Cherry")
    val added2 = set.add("Banana")

    println("Set after adding elements: $set")
    println("Was 'Cherry' added? $added1")
    println("Was 'Banana' added again? $added2")
}

Output:

Set after adding elements: [Apple, Banana, Cherry]
Was 'Cherry' added? true
Was 'Banana' added again? false

Real-World Use Case

Managing a Set of Unique Usernames

In real-world applications, the add function can be used to manage a set of unique usernames, ensuring that no duplicate usernames are added.

Example

fun main() {
    val usernames = hashSetOf("Alice", "Bob")
    val added1 = usernames.add("Charlie")
    val added2 = usernames.add("Alice")

    println("Usernames: $usernames")
    println("Was 'Charlie' added? $added1")
    println("Was 'Alice' added again? $added2")
}

Output:

Usernames: [Alice, Bob, Charlie]
Was 'Charlie' added? true
Was 'Alice' added again? false

Conclusion

The add function in Kotlin is a simple and effective way to insert elements into a HashSet. It ensures that only unique elements are added to the set, making it useful for various applications, including managing unique items and data validation. 

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

Comments