![]() Kotlin is concise, safe, pragmatic, and focused on interoperability with Java code. Many thanks to Jake Wharton for helping with this article.The Kotlin programming language is a modern language that gives you more power for your everyday tasks. It's succinct, efficient, easy-to-understand, and completely sidesteps any null checking whatsoever. In this case, the clear winner is groupingBy().eachCount(). Often times it depends on context for which strategy is best. (I don't mean to imply that this is an exhaustive list of strategies differing circumstances may yield more options.) Casting to non-null (with possible exception).I've demonstrated a number of strategies to convince the compiler that your code is okay: Then we use the helper function Grouping.eachCount() to transform that into Map.Ĭollection-level operations can be incredibly powerful and are often more useful than iterating over collections manually (especially because the standard library can optimize what's going on behind the scenes). ![]() What we're doing here is first converting our List into a Grouping. Here's how we might try to write it: fun countInstances(list: List): Map. Suppose we want to convert a List into a Map, where each Int represents the number of times each String appeared in the list. Code that you know is ironclad turns out to be full of potential nulls. This null safety can occasionally create some tricky situations, though. Try to use a nullable type in a non-null way and the compiler will yell at you. One of the best features of Kotlin is its built-in null safety in the type system.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |