I have an array:
var month: List<String> = arrayListOf("January", "February", "March")
I have to filter the list so I am left with only "January"
.
If you want to use element positions in the filter, use filterIndexed() . It takes a predicate with two arguments: the index and the value of an element. To filter collections by negative conditions, use filterNot() . It returns a list of elements for which the predicate yields false .
Kotlin arrayListOf() Example 4 - get()The get() function of arrayListOf() is used to retrieve the element present at specified index. For example: fun main(args: Array<String>){ val list: ArrayList<String> = arrayListOf<String>()
You can use this code to filter out January from array, by using this code
var month: List<String> = arrayListOf("January", "February", "March") // to get the result as list var monthList: List<String> = month.filter { s -> s == "January" } // to get a string var selectedMonth: String = month.filter { s -> s == "January" }.single()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With