Kotlin Data Class
In this tutorial we will cover the concept of Data class in Kotlin. In every project we require some classes whose only purpose is to store data. These classes are known as data class or data access objects or data transfer objects, etc.
Data classes mainly contains:
-
variables and their getters and setters,
-
constructor,
-
some functions like toString()
, equals()
, hashCode()
etc.
In Java we need to write a lot of boilerplate code for defining a data class. Let us create a simple Person
class in Java which has some properties. This is how the code will be in Java:
public class Person {
private String name;
private String gender;
private int age;
public Person(String name, String gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
'}';
}
}
This is really annoying writing this code as all it does is hold data and provide utility functions for managing the data. Kotlin solves this problem by introducing Data class.
Kotlin Data Class
A data class is created using the data
keyword. Kotlin data class by default provides:
-
Getters and setters for the class properties (also true in case of normal classes).
-
toString()
function to print the details of the object.
-
copy()
function to copy contents of one object to another.
-
equals()
and hashCode()
functions for hashing and comparison.
-
componentN()
functions corresponding to the properties in their order of declaration in constructor.
Let us create same data class Person
(like we did for Java above) in Kotlin:
data class Person(var name: String, var gender: String, var age: Int)
And that's all. It is one of the great example of Kotlin's conciseness.
Let us create an object of this class and print it:
fun main() {
val man = Person("Ninja", "Male", 18)
println(man)
}
Person(name=Ninja, gender=Male, age=18)
While printing the object, the toString()
function is called itself. So from the Kotlin data class example, we can see how easy it is to create a data class and then use it and all the basic functions are automatically defined by Kotlin.
Requirement for Data class
To create a data class, the following requirements should be met:
-
In the primary constructor atleast one parameter should be present.
-
All the parameters in primary constructor must be marked with with either val
or var
.
-
Data classes cannot be abstract, open, sealed or inner.
Copy Object Properties in Data class
If we wants to create a new object for any data class in Kotlin, using some properties from another object. Then we can achieve this using the copy()
function:
fun main() {
val man = Person("Ninja", "Male", 18)
val woman = man.copy(gender = "Female")
println(man)
println(woman)
}
Person(name=Ninja, gender=Male, age=18)
Person(name=Ninja, gender=Female, age=18)
When we use copy()
function to copy a single property value from any object, we should use named arguments otherwise the value may be assigned to other variable.
Kotlin Data Class: hashCode
and equals
functions
The equals()
and hashCode()
functions are used to check if two objects are equals or not. The hashCode()
generates the hash value of an object. The hash values of two objects is same if all the corresponding values of two objects are equal. The equals()
function is also used to check if two objects are equal.
Let's take a code example:
fun main() {
val man = Person("Ninja", "Male", 18)
val manNew = Person("Ninja", "Male", 18)
val woman = man.copy(gender = "Female")
if (man.hashCode() == manNew.hashCode())
println("man and manNew are equal")
if (man.hashCode() == woman.hashCode())
println("It will not be printed")
if (man.equals(manNew))
println("man and manNew are equal")
if (man.equals(woman))
println("It will not be printed")
}
man and manNew are equal
man and manNew are equal
Destructuring Data class object - componentN()
function
If we wants to map an objects properties into independent variable then it is a tedious task. In Kotlin we can destruct a object and get its properties in independent variables using the destructuring declaration. Let us destruct man
object into indenpendent variables:
fun main() {
val man = Person("Ninja", "Male", 18)
val (name,gender) = man
println("Independent variable name: $name")
println("Independent variable gender: $gender")
}
Independent variable name: Ninja
Independent variable gender: Male
It is possible because the compiler will generate componentN()
function for each property like:
man.component1() // Ninja
man.component2() // Male
man.component3() // 18
Hence, if you have a data class object with a lot of properties values stored in it, we can use this destructuring declaration to store the value for the data class object properties into independent variables.
Summary
And with this, this tutorial ends. In this tutorial we learned about Kotlin Data classes and the functionality they provide. Kotlin data classes reduce the boilerplate code and provides getter, setters, toString, equals, etc. common function automatically. In the next tutorial we will discuss about Enum in Kotlin.