Collections

Kotlin collections were inspired by Scala and its mutable and immutable collection types. Kotlin defines its collection types in the kotlin.collections package, and there you'll see that it basically redefines types that already exist in Java. But all of them get compiled to the Java version of the same type. So you don't have to worry that there is an additional overhead when using them.

The Iterable interface is the basis for all collection types. It has only one method, iterator, which returns an object that can iterate over a collection:

public interface Iterable<out T> {
public operator fun iterator(): Iterator<T>
}

The Iterator object knows how to iterate over a collection. It has two methods: next which returns the next element, and hasNext which tells us if there any more elements in the collection:

public interface Iterator<out T> 
{
public operator fun next(): T
public operator fun hasNext(): Boolean
}

The next interface in the hierarchy is the Collection. It adds a couple more functions to the Iterable interfaces, like the size of a collection, and a way to check if an element is contained inside a collection:

public interface Collection<out E> : Iterable<E> {
public val size: Int
public fun isEmpty(): Boolean
public operator fun contains(element: @UnsafeVariance E): Boolean
override fun iterator(): Iterator<E>
public fun containsAll(elements: Collection<@UnsafeVariance E>): Boolean
}

The next in the interface hierarchy are List and Set. They both extend the Collection interface. The List is an indexed collection, and with its get method, you can obtain an element from a collection by its position:

public interface List<out E> : Collection<E> {
override val size: Int

override fun isEmpty(): Boolean
override fun contains(element: @UnsafeVariance E): Boolean
override fun iterator(): Iterator<E>

override fun containsAll(elements: Collection<@UnsafeVariance E>): Boolean

public operator fun get(index: Int): E

public fun indexOf(element: @UnsafeVariance E): Int

public fun lastIndexOf(element: @UnsafeVariance E): Int

public fun listIterator(): ListIterator<E>

public fun listIterator(index: Int): ListIterator<E>

public fun subList(fromIndex: Int, toIndex: Int): List<E>
}

The Set is an unindexed collection of unique elements, as can be seen in the following command:

public interface Set<out E> : Collection<E> {
override val size: Int

override fun isEmpty(): Boolean
override fun contains(element: @UnsafeVariance E): Boolean
override fun iterator(): Iterator<E>

override fun containsAll(elements: Collection<@UnsafeVariance E>): Boolean
}

There is also the Map interface which represents a collection of key-value pairs. The Map doesn't extend the Collection and Iterable interfaces:

public interface Map<K, out V> {
public val size: Int

public fun isEmpty(): Boolean

public fun containsKey(key: K): Boolean

public fun containsValue(value: @UnsafeVariance V): Boolean

public operator fun get(key: K): V?

@SinceKotlin("1.1")
@PlatformDependent
public fun getOrDefault(key: K, defaultValue: @UnsafeVariance V): V {
return null as V
}

public val keys: Set<K>

public val values: Collection<V>

public val entries: Set<Map.Entry<K, V>>

public interface Entry<out K, out V> {
public val key: K
public val value: V
}
}

Now, all of these interfaces are present in Java, in the java.util package. But there is one big difference though: in Kotlin they are all read-only, that is, they are immutable. Kotlin differentiates between mutable and immutable collections. Each of these interfaces in Kotlin also has a mutable version, including the MutableIterator and MutableList.

The following table represents the Mutable and Immutable types with its Java representation:

Java Type
Kotlin Immutable Type
Kotlin Mutable Type
Iterable<E>
Iterable<E>
MutableIterable<E>
Collection<E>
Collection<E>
MutableCollection<E>
List<E>
List<E>
MutableList<E>
Set<E>
Set<E>
MutableSet<E>
Map<K, V>
Map<K, V>
MutableMap<K, V>

 

The mutable interfaces extend the immutable ones and add the methods for modifying the collections. Mutable versions are considered equal to their Java counterpart.

Types that you'll be working with mostly implement the List, Set, or Map interfaces and now we'll explore them in greater detail.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.219.80