![Kotlin for each with index](https://cdn1.cdnme.se/5447227/9-3/screenshot_8_64e62948e087c31b4a9aa5ed.jpg)
![kotlin for each with index kotlin for each with index](https://user.oc-static.com/upload/2018/05/31/15277805300705_kotlin_range_with_index.png)
Use the indexing operator to get an element: map.Ĭheck whether a map contains an element or elements Use the indexing operator map = value instead of put() and replace(). Also, you may use the plusAssign( +=) operator: map += Pair(key, value) or map += anotherMap. In Kotlin, the assignment map = value behaves the same as put(key, value). Kotlin has its own way to process streams: sequences and methods like map() and filter(). Use isNotEmpty() to check whether a collection is not empty. Use the in keyword to call contains() in the operator form: element in collection. Use the plusAssign( +=) operator: collection += element, collection += anotherCollection.Ĭheck whether a collection contains an element or elements In Kotlin, there are many operations on collections that look exactly the same as their counterparts in Java. This video by Sebastian Aigner, Kotlin Developer Advocate.Īll of the examples below use Java and Kotlin standard library APIs only. The second part of the guide, starting from Mutability, explains some of the differences by looking at specific cases.įor an introduction to collections, see the Collections overview or watch It is divided into operations that are the sameĪnd operations that exist only in Kotlin.
![kotlin for each with index kotlin for each with index](https://d1jnx9ba8s6j9r.cloudfront.net/blog/wp-content/uploads/2019/06/Kotlin.png)
The first part of this guide contains a quick glossary of operations on the same collections in Java and Kotlin. It will help you migrate from Java to Kotlin and write your code in the authentically Kotlin way. This guide explains and compares collection concepts and operations in Java and Kotlin. Collections are groups of a variable number of items (possibly zero) that are significant to the problem being solved and are commonly operated on.
![Kotlin for each with index](https://cdn1.cdnme.se/5447227/9-3/screenshot_8_64e62948e087c31b4a9aa5ed.jpg)