Hanbit the Developer

Kotlin Documentation | Write operations 본문

Mobile/Kotlin

Kotlin Documentation | Write operations

hanbikan 2023. 5. 24. 15:40

Kotlin Documentation 시리즈에 대해

Category: Standard library - Collections

문서 링크: https://kotlinlang.org/docs/collection-write.html


Adding elements

  • add(), addAll()
  • +=
val numbers = mutableListOf("one", "two")
numbers += "three"
println(numbers)
numbers += listOf("four", "five")    
println(numbers)

Removing elements

  • remove(), removeAll()
  • retainAll(): removeAll()의 반대. 주어진 컬렉션만을 남긴다.
  • clear()
  • -=
val numbers = mutableListOf("one", "two", "three", "three", "four")
numbers -= "three"
println(numbers)
numbers -= listOf("four", "five")    
//numbers -= listOf("four")    // does the same as above
println(numbers)
[one, two, three, four]
[one, two, three]