0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

kotlin filter

Posted at

Kotlin filter

役割:条件に合致した値のみリストに返却

使い方

filter
A.filter{ it XXXX }
A(List)11つに対して順番に処理を実行し、XXXXの条件に合致するもののみリストに返却する

Stringのリストの各要素に対して処理実行

filter
  fun filter(){
    val okashiList = listOf("cookie","chocolate","pinapple","manjuu","chuppachapps")
    println(okashiList.filter { it.contains("c") })
  }
result
[cookie, chocolate, chuppachapps]

Integerのリストの各要素に対して処理実行

count
fun main(){
    val pieList = listOf("3","1","4","1","5","9","2","6","5","3","5","8","9","7","9","3","2","3","8")
    println(pieList.filter { it.toInt() % 2 == 0 })
    println(pieList.filter { it.toInt() % 2 != 0 })
}
result
[4, 2, 6, 8, 2, 8]
[3, 1, 1, 5, 9, 5, 3, 5, 9, 7, 9, 3, 3]
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?