What is the hashSetOf() method in Kotlin?

The hashSetOf() method in Kotlin is used to create a mutable hashset. A mutable hashset allows both read and write operations.

Syntax

The hashSetOf() method can be declared as shown below:

fun <T> hashSetOf()(vararg elements: T): HashSet<T>
  • elements: The elements to be added to the hashset.

Return value

The hashSetOf() method returns a mutable hashset containing the specified elements.

Example

Consider the code snippet below, which demonstrates the use of the hashSetOf() method.

fun main(args: Array<String>)
{
val set1 = hashSetOf("aa", "bb", "cc");
println(set1)
}

Explanation

The hashSetOf() method in line 3 creates a mutable hashset of the three elements declared as parameters.

Free Resources