Last active
August 17, 2020 15:55
-
-
Save Audhil/89593569e307d6cee29df9492813923c to your computer and use it in GitHub Desktop.
data class in kotlin demo - auto implemented equals(), hashcode(), and toString() methods. special function copy() make new reference or new object when data modified
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val emp1 = Employee(name = "audhil", id = 33) | |
val emp2 = Employee(name = "audhil", id = 33) | |
if (emp1 == emp2) { | |
println("equal") | |
} else | |
println("not equal") | |
println(emp1.hashCode()) | |
println(emp2.hashCode()) | |
val emp3 = emp1.copy() | |
println("emp3:") | |
println(emp3) | |
println(emp3.hashCode()) | |
val emp4 = emp1.copy(id = 33) | |
println("emp4:") | |
println(emp4) | |
println(emp4.hashCode()) | |
val emp5 = emp1.copy(id = 3) | |
println("emp5:") | |
println(emp5) | |
println(emp5.hashCode()) | |
/* | |
* o/p | |
* equal | |
-661290842 | |
-661290842 | |
emp3: | |
Employee(name=audhil, id=33) | |
-661290842 | |
emp4: | |
Employee(name=audhil, id=33) | |
-661290842 | |
emp5: | |
Employee(name=audhil, id=3) | |
-661290872 | |
* */ | |
} | |
data class Employee( | |
val name: String, | |
val id: Int | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val emp1 = Employee(name = "audhil", id = 33, company = Company("zoho")) | |
val emp2 = Employee(name = "audhil", id = 33, company = Company("logica")) | |
println(emp1) | |
println(emp1.hashCode()) | |
println(emp2) | |
println(emp2.hashCode()) | |
if (emp1 == emp2) { | |
println("yup equals") | |
} else | |
println("yup not equals") | |
val emp3 = emp1.copy() | |
println(emp3) | |
println(emp3.hashCode()) | |
val emp4 = emp1.copy(id = 11) | |
println(emp4) | |
println(emp4.hashCode()) | |
val emp5 = emp1.copy(company = Company("zam zam")) | |
println(emp5) | |
println(emp5.hashCode()) | |
/* | |
* o/p | |
* | |
* emp1: | |
Employee(name=audhil, id=33, company=Company(companyName=zoho)) | |
978564886 | |
emp2: | |
Employee(name=audhil, id=33, company=Company(companyName=logica)) | |
-122515235 | |
yup not equals | |
emp3: | |
Employee(name=audhil, id=33, company=Company(companyName=zoho)) | |
978564886 | |
emp4: | |
Employee(name=audhil, id=11, company=Company(companyName=zoho)) | |
978564204 | |
emp5: | |
Employee(name=audhil, id=33, company=Company(companyName=zam zam)) | |
459884870 | |
* | |
* */ | |
} | |
data class Employee( | |
val name: String, | |
val id: Int, | |
val company: Company | |
) | |
data class Company( | |
val companyName: String | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package _00WarmUp | |
fun main() { | |
val emp1 = Employee(name = "audhil", id = 33, company = Company("zoho"), tokenList = listOf(Token(1), Token(30))) | |
println("emp1:") | |
println(emp1) | |
println(emp1.hashCode()) | |
val emp2 = emp1.copy() | |
println("emp2:") | |
println(emp2) | |
println(emp2.hashCode()) | |
val emp3 = emp1.copy(id = 11) | |
println("emp3:") | |
println(emp3) | |
println(emp3.hashCode()) | |
/* | |
* o/p | |
* | |
* emp1: | |
Employee(name=audhil, id=33, company=Company(companyName=zoho), tokenList=[Token(tokenNo=1), Token(tokenNo=30)]) | |
270741416 | |
emp2: | |
Employee(name=audhil, id=33, company=Company(companyName=zoho), tokenList=[Token(tokenNo=1), Token(tokenNo=30)]) | |
270741416 | |
emp3: | |
Employee(name=audhil, id=11, company=Company(companyName=zoho), tokenList=[Token(tokenNo=1), Token(tokenNo=30)]) | |
270720274 | |
* | |
* */ | |
} | |
data class Employee( | |
val name: String, | |
val id: Int, | |
val company: Company, | |
val tokenList: List<Token> | |
) | |
data class Company( | |
val companyName: String | |
) | |
data class Token( | |
val tokenNo: Int | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment