Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

kotlin
communitycreator

What is the hashSetOf() method in Kotlin?

Syed Muhammad Ali Mustafa

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

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.

RELATED TAGS

kotlin
communitycreator

CONTRIBUTOR

Syed Muhammad Ali Mustafa

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring