Clone a set in Kotlin
This article explores different ways to clone a set in Kotlin.
1. Using Copy Constructor
The standard solution is to use a copy constructor for cloning a set. A copy constructor is a special constructor for creating a new object as a shallow copy of an existing object.
|
1 2 3 4 5 6 7 8 9 10 |
fun <T> clone(original: Set<T>): Set<T> { return HashSet(original) } fun main() { val set = setOf('A', 'B', 'C') val items = clone(set) println(items) // [A, B, C] } |
2. Using toSet() function
Another preferred solution is to call the toSet or toMutableSet function, as demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 |
fun <T> clone(original: Set<T>): Set<T> { return original.toSet() } fun main() { val set = setOf('A', 'B', 'C') val items = clone(set) println(items) // [A, B, C] } |
3. Using addAll() function
You can also use the addAll() function, which appends all elements of the specified collection at the end of a set.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
fun <T> clone(original: Set<T>): Set<T> { val copy: MutableSet<T> = HashSet() copy.addAll(original) return copy } fun main() { val set = setOf('A', 'B', 'C') val items = clone(set) println(items) // [A, B, C] } |
4. Implement Cloneable Interface
To clone a set of custom objects, you can have the class implement the Cloneable interface and override its clone() function.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// Implement the `Cloneable` interface class Employee(var name: String, var age: Int) : Cloneable { public override fun clone(): Employee { return Employee(name, age) } override fun toString(): String { return "{$name:$age}" } } fun main() { val original = listOf( Employee("Kate", 25), Employee("Jane", 20) ) val copy = original.map(Employee::clone).toSet(); copy.first().age = 30 println(original) // [{Kate:25}, {Jane:20}] println(copy) // [{Kate:30}, {Jane:20}] } |
5. Using Data class
Kotlin’s data class automatically provides an implementation of the copy() function.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
data class Employee(var name: String, var age: Int) fun main() { val original = listOf( Employee("Kate", 25), Employee("Jane", 20) ) val copy = original.map { it.copy() } copy.first().age = 30 println(original) // [Employee(name=Kate, age=25), Employee(name=Jane, age=20)] println(copy) // [Employee(name=Kate, age=30), Employee(name=Jane, age=20)] } |
6. Custom copy() function
Finally, you can provide your own implementation of the copy() function. The implementation should copy an object and alter some of its properties but keep the rest unchanged.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class Employee(var name: String, var age: Int) { fun copy(name: String = this.name, age: Int = this.age) = Employee(name, age) override fun toString(): String { return "{$name:$age}" } } fun main() { val original = listOf( Employee("Kate", 25), Employee("Jane", 20) ) val copy = original.map { it.copy(age = 30) } println(original) // [{Kate:25}, {Jane:20}] println(copy) // [{Kate:30}, {Jane:30}] } |
That’s all about cloning a set in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)