Kotlin Tutoial
Basics
Control Flow
Array & String
Functions
Collections
OOPs Concept
Exception Handling
Null Safety
Regex & Ranges
Java Interoperability
Miscellaneous
Android
In Kotlin, HashSet
is a mutable collection that doesn't allow duplicate elements and doesn't guarantee the order of elements. The hashSetOf()
function is a convenient way to create a HashSet
. Here's a tutorial on how to use hashSetOf()
in Kotlin:
You can use the hashSetOf()
function to initialize a HashSet
:
val set1 = hashSetOf<Int>() // Empty HashSet val set2 = hashSetOf(1, 2, 3, 4) // HashSet with initial values
Although the initial values can be set using hashSetOf()
, you can also add elements later:
val fruits = hashSetOf("Apple", "Banana") fruits.add("Cherry")
You can check whether an element exists in the set:
val hasApple = "Apple" in fruits // true val hasGrape = "Grape" in fruits // false
fruits.remove("Apple") // Removes the element "Apple" from the set
for (fruit in fruits) { println(fruit) }
Kotlin provides various functions to perform set operations:
val setA = hashSetOf(1, 2, 3, 4) val setB = hashSetOf(3, 4, 5, 6) val union = setA.union(setB) // Results in [1, 2, 3, 4, 5, 6] val intersection = setA.intersect(setB) // Results in [3, 4] val difference = setA.subtract(setB) // Results in [1, 2]
val size = fruits.size // Returns the number of elements val isEmpty = fruits.isEmpty() // Checks if the set is empty
fruits.clear() // Removes all elements from the set
You can easily convert a HashSet
to other collections, like lists:
val fruitList = fruits.toList()
You can use the filter
function to get a new set that satisfies a particular condition:
val filteredSet = fruits.filter { it.startsWith('A') }.toHashSet()
HashSet
is a useful collection in Kotlin when you need a group of unique elements without any specific order. Using hashSetOf()
, you can easily create and initialize a HashSet
. Familiarity with HashSet
and its operations can greatly assist in many programming scenarios where duplicates are unwanted.
Creating a HashSet in Kotlin:
val hashSet = HashSet<String>()
Initializing and declaring a HashSet using hashSetOf():
val uniqueNumbers = hashSetOf(1, 2, 3, 4, 5)
Adding and removing elements in a Kotlin HashSet:
uniqueNumbers.add(6) // Adding uniqueNumbers.remove(3) // Removing
Accessing values in a HashSet in Kotlin:
val containsTwo = 2 in uniqueNumbers
Iterating over a HashSet in Kotlin:
for (number in uniqueNumbers) { println(number) }
Difference between hashSetOf() and mutableSetOf() in Kotlin:
val setA = hashSetOf(1, 2, 3) val setB = mutableSetOf(4, 5, 6)
Default values for HashSet in Kotlin:
val defaultIfAbsent = uniqueNumbers.getOrElse(10) { 0 }
Filtering and transforming values in a Kotlin HashSet:
filter
and map
to manipulate values.val evenNumbers = uniqueNumbers.filter { it % 2 == 0 } val squaredNumbers = uniqueNumbers.map { it * it }
Checking if an element exists in a Kotlin HashSet:
val containsFour = uniqueNumbers.contains(4)
Converting other collections to a HashSet in Kotlin:
val list = listOf(1, 2, 3, 4, 5) val setFromList = list.toHashSet()
Handling null values in a Kotlin HashSet:
val nullableValue: Int? = uniqueNumbers.find { it == 3 }
Using hashSetOf() with data classes in Kotlin:
data class Person(val name: String, val age: Int) val peopleSet = hashSetOf( Person("Alice", 25), Person("Bob", 30) )