Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Kotlin Collections

DZone's Guide to

Kotlin Collections

This breakdown of Kotlin's Collection API explains how it differs from Java's and how the hierarchy and mutability work in your code.

· Java Zone
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

At the root of Kotlin’s Collection API lies the Iterator interface. It's similar to Java’s, but the similitude stops after that. java.util.ListIterator features are broken down into different contracts:

  1. ListIterator to move the iterator index forward and backward.
  2. MutableIterator to remove content from the iterator.
  3. MutableListIterator inherits from the two interfaces above to mimic the entire contract of java.util.ListIterator.
Iterator API

Collection, List, and Set

The hierarchy of collections in Kotlin are very similar as in Java: Collection, List, and Set. (I won’t detail maps, but they follow the same design). The only, but huge, difference is that it’s divided between mutable and immutable types. Mutable types have methods to change their contents (e.g. add() and `set()), while immutable types don’t.

Of course, the hierarchy is a bit more detailed compared to Java, but that’s expected from a language that benefits from its parent’s experience.

Collection, List and Set

Implementations

IMHO, the important bit about Kotlin collections is not their hierarchy — though it’s important to understand the difference between mutable and immutable.

As Java developers know, there’s no such things as out-of-the-box immutable collection types in Java. When an immutable collection is required, the mutable collection must be wrapped into an unmodifiable type via a call to the relevant Collections.unmodifiableXXX(). But unmodifiable types are not public, they are private in Collections: types returned are generic ones (List or Set interfaces). It means they implement all methods of the standard collections. Immutability comes from the mutable-related methods throwing exceptions: At compile time, there’s no way to differentiate between a mutable and an immutable collection.

On the other hand, Kotlin offers a clean separation between mutable and immutable types. It also provides dedicated functions to create objects of the relevant type:

Collections-creating functions


As opposed to Scala, Kotlin doesn’t implement its own collection types, it reuses those from Java. That means that even when the compile-time type is immutable, the runtime type is always mutable. The downside is that it’s possible to change the collection elements by casting it to the correct runtime type. In my opinion, this is no more severe than what allows standard reflection. There are several advantages, though:

  1. Faster time-to-market.
  2. Java collection types benefited from years of improvement.
  3. The underlying implementation can be changed in the future with full backward compatibility.

To go further:

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:
java ,kotlin ,collections api ,mutability ,tutorial

Published at DZone with permission of Nicolas Frankel, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}