Skip to content

Commit 14eec50

Browse files
update according to the review comments
1 parent 2170a23 commit 14eec50

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

src/main/kotlin/codecollection/kotlinfeatures/DataClass.kt

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,34 @@ fun main() {
3535
val person2 = Person(firstName = "Bob", age = 32, email = "bob@example.com")
3636
val person3 = Person(firstName = "Alice", age = 30, email = "alice@example.com")
3737

38+
demonstrateToString(person1, person2)
39+
demonstrateToEquals(person1, person2, person3)
40+
demonstrateToHashCode(person1, person2, person3)
41+
demonstrateToCopy(person1)
42+
demonstrateToDestructuring(person1)
43+
}
44+
45+
fun demonstrateToString(person1: Person, person2: Person) {
3846
// toString() - automatically generated
3947
println("\n=== toString() ===")
4048
println("Person 1: $person1")
4149
println("Person 2: $person2")
4250
// Output:
4351
// Person 1: Person(firstName=Alice, age=30, email=alice@example.com)
4452
// Person 2: Person(firstName=Bob, age=32, email=bob@example.com)
53+
}
4554

46-
// equals()
55+
fun demonstrateToEquals(person1: Person, person2: Person, person3: Person) {
56+
// equals() - automatically generated
4757
println("\n=== equals() ===")
4858
println("person1 == person3: ${person1 == person3}") // true
4959
println("person1 == person2: ${person1 == person2}") // false
60+
}
5061

51-
// hashCode()
62+
fun demonstrateToHashCode(person1: Person, person2: Person, person3: Person) {
63+
// hashCode() - automatically generated
5264
println("\n=== hashCode() ===")
53-
println("hashCode person1: ${person1.hashCode()}")
54-
println("hashCode person2: ${person2.hashCode()}")
55-
println("hashCode person3: ${person3.hashCode()}") // same as person1.hashCode()
56-
// Output:
57-
// hashCode person1: -1399032001
58-
// hashCode person2: 531733799
59-
// hashCode person3: -1399032001
65+
println("person1.hashCode() equals person3.hashCode(): ${person1.hashCode() == person3.hashCode()}") // true
6066

6167
// Checking in HashSet
6268
println("\n=== Checking in HashSet ===")
@@ -77,17 +83,21 @@ fun main() {
7783
)
7884

7985
println("person1 salary: ${salary[person1]}") // 30000
80-
println("person3 salary: ${salary[person3]}") // 30000, because is same key
86+
println("person3 salary: ${salary[person3]}") // 30000, because person3 equals person1 (same hashCode and equals)
8187

8288
// Important! The use "var" is not recommended in the data classes.
8389
println("\n=== Mutation properties: ===")
8490
val mutablePerson = MutablePerson("Charlie", 35, "charlie@example.com")
85-
val testMap = hashMapOf(mutablePerson to "Developer")
86-
println("before mutation: ${testMap[mutablePerson]}") // Developer
91+
val roleMap = hashMapOf(mutablePerson to "Developer")
92+
println("before mutation: ${mutablePerson.hashCode()}")
8793
mutablePerson.age = 36
88-
println("after mutation: ${testMap[mutablePerson]}") // null, the key is "lost"
94+
println("after mutation: ${mutablePerson.hashCode()}") // Different!
95+
println("Map size: ${roleMap.size}") // Map still contains the entry but can't find it.
96+
}
8997

90-
// copy() Attention! It is shallow copy. See example
98+
fun demonstrateToCopy(person1: Person) {
99+
// copy() - automatically generated.
100+
// Attention! It is shallow copy. See example
91101
println("\n=== copy() ===")
92102

93103
val person1Updated = person1.copy(age = 31, email = "new.alice@example.com")
@@ -111,7 +121,9 @@ fun main() {
111121
println("employee2: $employee2")
112122
// employee1: Employee(position=Developer, person=Person(firstName=Alice, age=30, email=alice@example.com), address=Address(city=New York, street=Park Avenue))
113123
// employee2: Employee(position=Developer, person=Person(firstName=Alice, age=30, email=alice@example.com), address=Address(city=New York, street=Park Avenue))
124+
}
114125

126+
fun demonstrateToDestructuring(person1: Person) {
115127
// Destructuring - automatically generated componentN() functions corresponding to the properties in their order of declaration.
116128
println("\n=== Destructuring: ===")
117129
val (name, age, email) = person1
@@ -127,3 +139,4 @@ fun main() {
127139
// age: 30
128140
// email: alice@example.com
129141
}
142+

0 commit comments

Comments
 (0)