![]() Output: Variable value 4eec7777 Class Type: When you call Collection functions on a List in Kotlin, each operation returns a new collection with the desired values.Įach operation in the pipeline works on all the elements in the List before going to the next function.Įxample: val numbers = listOf( 1, 2, 3, 4, 5) See! I told you something was wrong ) Collection Functions #Kotlin val scores = listOf( 1, 2, 3, 4, 5) #Java List scores = Arrays.asList(1, 2, 3, 4, 5) Let's prove that the outputs would be different by adding a little text to tweak it: If your intention was to write the Kotlin code that would execute just as the Java streams did then the above code is wrong. Let's try to do that in Kotlin: val scores = listOf( 1, 2, 3, 4, 5) List scores = Arrays.asList(1, 2, 3, 4, 5) ![]() Sample Java program to filter through a list, get even numbers, multiply them by 4 and then print them out. We need to call the Stream API in order to perform these operations in Java, not so in Kotlin. Java has some common Stream API functions like Map(), Filter(), FlatMap() and Sorted(). When we need to run a couple of operations on a Kotlin collection, it's good to know the difference between using the Kotlin collection functions or the Kotlin Sequences. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |