![]() Also, we’re going to filter this list by a condition. ![]() In this example, we’re appending the List of elements of type Integer returned by the filterIsInstanceTo() function to the mutableList. Overview In this tutorial, we’re going to see how to find an element in a list using Kotlin language. Kotlin is easy to learn, especially if you already know Java (it is 100 compatible with Java). Subsequently, we can use the filterIsInstanceTo() function to append a List of elements of a specified type to the destination List: val countr圜ode = listOf("Germany", 49, null, "India", 91, "Japan", 81, "Brazil", null, "Australia", 61)ĪssertTrue(ntainsAll(listOf(1,24,49,91,81,61))) Kotlin is a modern, trending programming language. In this case, we’re using the filterIsInstance() function to return a List of elements of type Integer. Val filterList = countr圜ode.filterIsInstance()ĪssertTrue(ntainsAll(listOf(49,91,81,61))) ![]() The filterIsInstance() function can be used to return a List of elements of a specified type: val countr圜ode = listOf("Germany", 49, null, "India", 91, "Japan", 81, "Brazil", null, "Australia", 61) The function String.split() can help split the string by one or more whitespace characters, and then we can count the number of split parts returned by the function using unt(). ![]() ![]() In this example, the filterIndexedTo() function appends the resulting two elements matching the predicate to the mutableList. To count number of words in string in Kotlin, you can use String.split() function and unt() functions. It then returns a List of elements matching the given predicate: val filterList = countries.filterIndexed )ĪssertTrue(ntainsAll(listOf("United States","Canada","Germany","Australia"))) The predicate of this function uses both the index and the element as arguments. We can use the filterIndexed() function to utilize the element position in the filter. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |