Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clone object in Kotlin?

Tags:

clone

kotlin

The Kotlin documentation describes cloning only in accessing Java and in enum class. In latter case clone is just throwing an exception.

So, how would I / should I clone arbitrary Kotlin object?

Should I just use clone() as in Java?

like image 207
Dims Avatar asked Mar 01 '18 15:03

Dims


People also ask

How do you duplicate an object in Kotlin?

For a data class , you can use the compiler-generated copy() method. Note that it will perform a shallow copy. To create a copy of a collection, use the toList() or toSet() methods, depending on the collection type you need. These methods always create a new copy of a collection; they also perform a shallow copy.

How do you copy objects on Android?

You have to specify on your class that it implements Cloneable interface and you have to override clone method inside that class. By Default it will use clone method of Object class.


3 Answers

For a data class, you can use the compiler-generated copy() method. Note that it will perform a shallow copy.

To create a copy of a collection, use the toList() or toSet() methods, depending on the collection type you need. These methods always create a new copy of a collection; they also perform a shallow copy.

For other classes, there is no Kotlin-specific cloning solution. You can use .clone() if it suits your requirements, or build a different solution if it doesn't.

like image 138
yole Avatar answered Sep 21 '22 18:09

yole


You can use Gson library to convert the original object to a String and then convert back that String to an actual Object type, and you'll have a clone. Although this is not the intended usage of the Gson library which is actually used to convert between JSON and other object types, but I have devised this method to solve the cloning problem in many of my Kotlin based Android applications. See my example. Put this function in the class/model of which you want to create a clone. In my example I'm cloning an Animal type object so I'll put it in the Animal class

class Animal{
 fun clone(): Animal 
 {
   val stringAnimal = Gson().toJson(this, Animal::class.java)
   return Gson().fromJson<Animal>(stringAnimal, Animal::class.java)
 }
}
       

Then use it like this:

val originalAnimal = Animal()
val clonedAnimal = originalAnimal.clone()
like image 43
zulkarnain shah Avatar answered Sep 21 '22 18:09

zulkarnain shah


A Kotlin data class is easy to clone using .copy()

All values will be shallow copied, be sure to handle any list/array contents carefully.

A useful feature of .copy() is the ability to change any of the values at copy time. With this class:

data class MyData(
    val count: Int,
    val peanuts: Int?,
    val name: String
)
val data = MyData(1, null, "Monkey")

You could set values for any of the properties

val copy = data.copy(peanuts = 100, name = "Elephant")

The result in copy would have values (1, 100, "Elephant")

like image 27
Gibolt Avatar answered Sep 21 '22 18:09

Gibolt