1. list에서 특정 기준으로 중복된 요소 제거, distinct(), distinctBy()
fun main() {
data class xxx(val title:String, val context:String)
val tiger = xxx("tiger", "strong")
val tiger2 = xxx("tiger", "cute")
val lion = xxx("lion", "forceful")
val lion2 = xxx("lion", "powerful")
val cat = xxx("cat", "cute")
val animals = listOf(tiger,tiger2,lion,lion2,cat)
fun distinct() {
animals.distinctBy { animal -> animal.title }.forEach { println(it) }
}
distinct()
}
2. Foreach를 통한 List element 변경 예제
fun main() {
data class xxx(val title:String, var context:String)
val tiger = xxx("tiger", "strong")
val tiger2 = xxx("tiger", "cute")
val lion = xxx("lion", "forceful")
val lion2 = xxx("lion", "powerful")
val cat = xxx("cat", "cute")
val animals = listOf(tiger,tiger2,lion,lion2,cat)
animals.forEach{ if(it.title == "lion") {it.context = "unionLion" } }
}