Converting between arrays and sets is a common task in many programming scenarios. In Kotlin, arrays and sets are both part of the collections framework, but they serve different purposes. Arrays are ordered collections that can contain duplicate elements, while sets are unordered collections that do not allow duplicates. This article will explore how to convert arrays to sets (HashSet) and vice versa using Kotlin. In this article We will write three different Kotlin Program to Convert Array to Set (HashSet) and Vice-Versa, each with detailed explanations and output.
1. Introduction
In Kotlin, converting between arrays and sets can be achieved using built-in functions and methods. Arrays provide fast access to elements by index, while sets are useful for ensuring that no duplicates exist in the collection. By understanding how to convert between these two types of collections, you can leverage their unique properties effectively in your programs.
2. Example 1: Converting an Array to a HashSet
2.1 Program Explanation
In this example, we will convert an array of integers to a HashSet
. The toSet
function can be used to achieve this, which will automatically remove any duplicate elements from the array.
2.2 Code Implementation
fun main() {
val array = arrayOf(1, 2, 3, 4, 4, 5, 5, 6)
val hashSet = array.toHashSet()
println("Original Array: ${array.contentToString()}")
println("Converted HashSet: $hashSet")
}
2.3 Output
Original Array: [1, 2, 3, 4, 4, 5, 5, 6]
Converted HashSet: [1, 2, 3, 4, 5, 6]
3. Example 2: Converting a HashSet to an Array
3.1 Program Explanation
In this example, we will convert a HashSet
of strings to an array. The toTypedArray
function is used to convert the set to an array of the same type.
3.2 Code Implementation
fun main() {
val hashSet = hashSetOf("Kotlin", "Java", "Python", "Kotlin")
val array = hashSet.toTypedArray()
println("Original HashSet: $hashSet")
println("Converted Array: ${array.contentToString()}")
}
3.3 Output
Original HashSet: [Kotlin, Java, Python]
Converted Array: [Kotlin, Java, Python]
4. Example 3: Handling Complex Data Types
4.1 Program Explanation
This example demonstrates how to convert between arrays and sets when dealing with complex data types such as custom objects. We will create a simple data class Person
and convert between an array and a set of Person
objects.
4.2 Code Implementation
data class Person(val name: String, val age: Int)
fun main() {
val peopleArray = arrayOf(
Person("Alice", 30),
Person("Bob", 25),
Person("Charlie", 35),
Person("Alice", 30)
)
val peopleSet = peopleArray.toHashSet()
println("Original Array: ${peopleArray.contentToString()}")
println("Converted HashSet: $peopleSet")
val newArray = peopleSet.toTypedArray()
println("Converted Back to Array: ${newArray.contentToString()}")
}
4.3 Output
Original Array: [Person(name=Alice, age=30), Person(name=Bob, age=25), Person(name=Charlie, age=35), Person(name=Alice, age=30)]
Converted HashSet: [Person(name=Alice, age=30), Person(name=Bob, age=25), Person(name=Charlie, age=35)]
Converted Back to Array: [Person(name=Alice, age=30), Person(name=Bob, age=25), Person(name=Charlie, age=35)]
5. Conclusion
In this article, we explored three different methods to convert arrays to sets (HashSet) and vice versa in Kotlin. The first example demonstrated a simple conversion of an array of integers to a HashSet, highlighting how duplicates are removed. The second example showed how to convert a HashSet of strings to an array. The third example handled complex data types by converting between arrays and sets of custom objects. Each method leverages Kotlin’s built-in functions to achieve efficient and readable conversions. By understanding these techniques, you can effectively manage collections in your Kotlin programs.