μ΄ κΈμ λ΄μ©μ [μ΄νν°λΈ μ½νλ¦°]μ μ°Έκ³ ν©λλ€.
λλ‘λ λ°μ΄ν°λ₯Ό νκΊΌλ²μ μ λ¬ν΄μΌ ν λκ° μμ΅λλ€. μλ μ½λμ²λΌ λ§μ΄μ£ .
data class Player(
val id: Int,
val name: String,
val points: Int
)
val player = Player(0, "Gecko", 9999)
data νμ μλ μλμ ν¨μλ€μ μλμΌλ‘ μμ±ν΄μ€λλ€.
- toString
- equals μ hashCode
- copy
- componentN(component1, component2 λ±)
toString
ν΄λμ€μ μ΄λ¦κ³Ό κΈ°λ³Έ μμ±μ ννλ‘ λͺ¨λ νλ‘νΌν°μ κ°μ μΆλ ₯ ν΄μ€λλ€. μ£Όλ‘ λ‘κ·Έ μΆλ ₯μ΄λ λλ²κΉ ν λ μ¬μ©ν©λλ€.
println(player) // Player(id=0, name=Gecko, points=9999)
equals
κΈ°λ³Έ μμ±μμ νλ‘νΌν°κ° κ°μμ§ νμΈν΄μ€λλ€. hashCode μ equals λ κ°μ κ²°κ³Όλ₯Ό λνλ λλ€.
player == Player(0, "Gecko", 9999) // true
player == Player(0, "Ross", 9999) // false
Copy
immutable λ°μ΄ν° ν΄λμ€λ₯Ό λ§λ€ λ νΈλ¦¬ν©λλ€. copyλ κΈ°λ³Έ μμ±μ νλ‘νΌν°κ° κ°μ μλ‘μ΄ κ°μ²΄λ₯Ό 볡μ ν©λλ€.
val newObj = player.copy(name = "Thor")
print(newObj) // Player(id=0, name=Thor, points=99999)
copy λ©μλλ μμ 볡μ¬μ΄μ§λ§ immutable κ°μ²΄λΌλ©΄ μκ΄μ΄ μμ΅λλ€. μ΄μ°¨νΌ νλ‘νΌν° λ³κ²½μ΄ λΆκ°λ₯ν ν λκΉμ.
ComponentN ν¨μ(Component1, Component2 λ±)
μμΉλ₯Ό κΈ°λ°μΌλ‘ κ°μ²΄λ₯Ό ν΄μ ν μ μκ² ν΄μ€λλ€.
val (id, name, pts) = player
μ΄λ κ² κ°μ²΄λ₯Ό ν΄μ νλ μ½λλ₯Ό μμ±νλ©΄, μ½νλ¦°μ λ΄λΆμ μΌλ‘ componentN ν¨μλ₯Ό μ¬μ©νλ μλμ κ°μ μ½λλ‘ λ³νν©λλ€.
// μ»΄νμΌ ν
val id: Int = player.component1()
val name: String = player.component2()
val pts: Int = player.component3()
μ΄λ κ² κ°μ²΄λ₯Ό ν΄μ νλ©΄ μ₯μ κ³Ό λ¨μ μ΄ μμ΅λλ€.
μ₯μ
- λ³μμ μ΄λ¦μ μνλ λλ‘ μ§μ ν μ μμ΅λλ€.
val visited = listOf("Korea", "Russia", "USA")
val (first, second, third) = visited
println("$first $second $third") // Korea Russia USA
val trip = mapOf(
"Korea" to "Seoul",
"Russia" to "moscow",
"USA" to "Wasinton"
)
for ((country, city) in trip) {
println("We Loved $city in $country")
}
λ¨μ
- μμΉλ₯Ό μλͺ» μ§μ νλ©΄ λ€μν λ¬Έμ κ° λ°μ ν μ μμ΅λλ€.
νν λμ λ°μ΄ν° ν΄λμ€ μ¬μ©νκΈ°
λ°μ΄ν° ν΄λμ€λ ννλ³΄λ€ λ§μ κ²μ μ 곡ν©λλ€.
ννμ λ¨μ
- ννμ λ°μ΄ν° ν΄λμ€μ κ°μ μν μ νμ§λ§ ν¨μ¬ κ°λ μ±μ΄ λ¨μ΄μ§λλ€.
- ννλ§ λ³΄κ³ μ΄λ€ νμ μ λνλ΄λμ§ μμΈ‘μ΄ μ΄λ ΅μ΅λλ€.
κ²°κ΅ ννλ³΄λ€ λ°μ΄ν° ν΄λμ€λ₯Ό μ¬μ©νλ κ²μ΄ λͺ¨λ λ©΄μμ μ’κΈ° λλ¬Έμ ννμ μ μ°¨ μμ΄μ§κ³ μμ΅λλ€.
κ°λ¨ν μμ λλ€.
fun String.parseName(): Pair<String, String>? {
val indexOfLastSpace = this.trim().lastIndexOf(' ')
if(indexOfLastSpace < 0) return null
val firstName = this.take(indexOfLastSpace)
val lastName = this.drop(indexOfLastSpace)
return Pair(firstName, lastName)
}
// μ¬μ©
val fullName = "Marcin Moskata"
val (fisrtName, lastName) = fullName.parseName() ?: return
μ μ½λμ λ¬Έμ μ
- Pair<String, String> μ λ³΄κ³ μ 체 μ΄λ¦μ λνλΈλ€λ κ²μ μΈμ§νκΈ°κ° μ΄λ ΅μ΅λλ€.
- lastNameκ³Ό firstName μ€ μ΄λ€ κ²μ΄ μμ μμμ§ μμΈ‘νκΈ°κ° μ΄λ ΅μ΅λλ€.
λ°λΌμ ννλ³΄λ¨ λ°μ΄ν° ν΄λμ€λ₯Ό νμ©νμ!!
data class FullName(
val firstName: String,
val lastName: String
)
fun String.parseName(): FullName? {
val indexOfLastSpace = this.trim().lastIndexOf(' ')
if(indexOfLastSpace < 0) return null
val firstName = this.take(indexOfLastSpace)
val lastName = this.drop(indexOfLastSpace)
return FullName(firstName, lastName)
}
// μ¬μ©
val fullName = "Marcin Moskata"
val (fisrtName, lastName) = fullName.parseName() ?: return
λ°μ΄ν° ν΄λμ€λ₯Ό μ¬μ©νμ λμ μ₯μ
- ν¨μμ λ¦¬ν΄ νμ μ΄ λͺ νν΄μ§λλ€. → Pair<String, String> λμ FullName μ¬μ©
- μ¬μ©μκ° λ°μ΄ν° ν΄λμ€μ μ ν μλ κ²κ³Ό λ€λ₯Έ μ΄λ¦μ νμ©ν΄ λ³μλ₯Ό ν΄μ νλ©΄, κ²½κ³ κ° μΆλ ₯λ©λλ€.
μ΄μ²λΌ λ°μ΄ν° ν΄λμ€λ₯Ό νμ©νλ©΄ ννμ νμ©ν λλ³΄λ€ λ λ§μ μ₯μ μ΄ μμ΅λλ€.
λ°λΌμ ν΄λμ€λ₯Ό νμ©νλλ° λλ €μμ κ°κΈ° λ§κ³ , μ κ·Ήμ μΌλ‘ νμ©ν©μλ€.