Equals method for data class in Kotlin
Kenji Tomita
Posted on March 21, 2023
Kotlin data class have many advantages, one of which is automatically generating the equals()
method. equals()
is useful for comparing the equivalence of objects. For example, it is useful for checking the difference between before and after input in an input form. Let's take a look at the example code.
Example code
If the following User data class is used, the content of the user class can be compared for equality using ==
. By the way, ==
is a syntax sugar for equals()
.
data class User(
val name: String,
val country: String,
)
val user1 = User(
name = "tommykw",
country = "JP",
)
val user2 = User(
name = "tommykw2",
country = "JP",
)
val user3 = User(
name = "tommykw",
country = "JP",
)
user1 == user2 // false
user1 == user3 // true
Nested code
Furthermore, let's add val location: Location
to the User data class. As you can see, you can use ==
to confirm whether they have the same content. This is very convenient because equals()
is automatically implemented.
data class User(
val name: String,
val country: String,
val location: Location,
)
data class Location(
val latitude: Double,
val longitude: Double,
)
val user1 = User(
name = "tommykw",
country = "JP",
location = Location(
latitude = 1.0,
longitude = 1.0,
),
)
val user2 = User(
name = "tommykw",
country = "JP",
location = Location(
latitude = 1.0,
longitude = 1.0,
),
)
val user3 = User(
name = "tommykw",
country = "JP",
location = Location(
latitude = 1.1,
longitude = 1.1,
),
)
user1 == user2 // true
user1 == user3 // false
Generated code
The data class's equals()
is very convenient. Let's take a look at the auto-generated equals()
by decompiling the above code. In User.equals()
, you can see that name
, country
, and location
are compared by Intrinsics.areEqual()
.
public static final class User {
public boolean equals(@Nullable Object var1) {
if (this != var1) {
if (var1 instanceof User) {
User var2 = (User)var1;
if (Intrinsics.areEqual(this.name, var2.name) && Intrinsics.areEqual(this.country, var2.country) && Intrinsics.areEqual(this.location, var2.location)) {
return true;
}
}
return false;
} else {
return true;
}
}
}
If you look further at the implementation of Intrinsics.areEqual()
, comparing whether both are null or have the same value.
public class Intrinsics {
public static boolean areEqual(Object first, Object second) {
return first == null ? second == null : first.equals(second);
}
}
In the case of location.equals()
, you can see that Location.equlas()
is called to compare the latitude
and longitude
. Double.compare
compares the two specified double values.
public static final class Location {
public boolean equals(@Nullable Object var1) {
if (this != var1) {
if (var1 instanceof Location) {
Location var2 = (Location)var1;
if (Double.compare(this.latitude, var2.latitude) == 0 && Double.compare(this.longitude, var2.longitude) == 0) {
return true;
}
}
return false;
} else {
return true;
}
}
}
Conclusion
This article explained that data class automatically generate equals()
method, making comparison of data class extremely easy.
Posted on March 21, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 23, 2024