Kotlin

[Kotlin] none,any,all,contains

onemask 2021. 3. 30. 01:07

Collection에서 많이 사용되는 함수에 대하여 알아보자. 


any

- Collection에 적어도 1개의 element 가 있으면 return true

val list = listOf("red", "yellow", "green", "banana")
val result: Boolean = list.any { it == "banana" }
println("list any result is ${result}") //true

none

- any랑 반대

- match 되는 element가 없을 때 return true

val list = listOf("red", "yellow", "green", "banana")
val resultNone: Boolean = list.none { it == "banana" }
println("list none result is ${resultNone}") //false

all

- 모든 collection의 element와 match 될 때 return true

val list = listOf("red", "yellow", "green", "banana")
val resultAll = list.all { it.contains("e") }
println("list all result is ${resultAll}") //false

contains

- list에 해당 elemen가 있는지 없는지 체크

  
  val list = listOf("red", "yellow", "green", "banana")
  val resultContains = list.contains("banana")
  println("list contains result is ${resultContains}") //true


Ref)
grokonez.com/kotlin/kotlin-methods-none-contains

 

Kotlin methods - any(), none(), all(), contains() example » grokonez

In the tutorial, Grokonez will show you how to work with some Kotlin methods: any(), all(), none(), contains(). ContentsI. Kotlin methods – any(), none(), all(), contains()1. any() vs none() methods2. all() method3. contains() methodII. Full Sourcecode I

grokonez.com

 

LIST