Sling Academy
Home/Kotlin/Converting Between Arrays, Lists, Sets, and Maps in Kotlin

Converting Between Arrays, Lists, Sets, and Maps in Kotlin

Last updated: December 04, 2024

In Kotlin, working with collections is a daily task you perform when you manipulate or manage data. These collections can come in various types, such as arrays, lists, sets, and maps. Understanding how to convert between these collection types is crucial as it allows you to choose the best structure for your data and operations.

Arrays

An array is a container that holds data of a specific type. Once defined, its size is fixed. To declare an array in Kotlin, you can use the arrayOf() function:

val numbersArray = arrayOf(1, 2, 3, 4, 5)

Now, let's see how you can convert an array to other collections.

Converting Array to List

Kotlin provides the toList() method for easy conversion from an array to a list:

val numbersList: List<Int> = numbersArray.toList()

Converting Array to Set

To convert an array to a set, which is a collection of unique elements, you can use the toSet() method:

val numbersSet: Set<Int> = numbersArray.toSet()

Lists

Lists are ordered collections that have a more flexible size, making them more versatile than arrays.

Converting List to Array

Converting a list to an array involves using the toTypedArray() method:

val list = listOf(1, 2, 3, 4, 5)
val array: Array<Int> = list.toTypedArray()

Converting List to Set

Lists can contain duplicates, but when converting to a set, duplicates are removed. The method toSet() accomplishes this:

val setFromList: Set<Int> = list.toSet()

Sets

Sets are collections of unique elements, providing operations that assure all elements are distinct.

Converting Set to List

You can convert a set back to a list using the toList() function, reacquiring the possibility of duplicates:

val set = setOf(1, 2, 3, 4, 5)
val listFromSet: List<Int> = set.toList()

Converting Set to Array

Similar to lists, you can convert a set to an array:

val arrayFromSet: Array<Int> = set.toTypedArray()

Maps

Maps are pairs of keys and values where each key is unique. Unlike lists and sets, conversion from arrays, lists, or sets to maps requires defining keys explicitly.

Converting List to Map

A common approach to convert a list to a map is to consider pairs or to() operations:

val list2 = listOf("a" to 1, "b" to 2, "c" to 3)
val mapFromList: Map<String, Int> = list2.toMap()

Converting Set to Map

Similar to lists, to convert a set to a map, you need to form pairs:

val set2 = setOf("x" to 10, "y" to 20, "z" to 30)
val mapFromSet: Map<String, Int> = set2.toMap()

Dealing with Maps

When working with maps, conversions might involve splitting keys and values into separate lists or sets:

For instance, extracting keys:

val keys: Set<String> = mapFromList.keys

Extracting values:

val values: Collection<Int> = mapFromList.values

Conclusion

Mastering conversions between arrays, lists, sets, and maps in Kotlin empowers developers to seamlessly transition between different uses of collections as per application requirements. With these tools at your disposal, you can approach data manipulation efficiently and succinctly.

Next Article: Sorting Collections in Kotlin: Built-in Methods

Previous Article: Choosing the Right Collection Type in Kotlin

Series: Kotlin Collections

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin