Kotlin Program to Convert Map (HashMap) to List

Converting a Map (or HashMap) to a List is a common task in many programming scenarios. In Kotlin, maps are collections of key-value pairs, while lists are ordered collections of elements. This article will explore three different Kotlin Program to Convert Map (HashMap) to List.

1. Introduction

In Kotlin, a map is an unordered collection of key-value pairs, where each key is unique. A list, on the other hand, is an ordered collection that can contain duplicate elements. Converting a map to a list can be useful for various operations such as filtering, sorting, and transforming the data.

2. Example 1: Converting a Map to a List of Pairs

2.1 Program Explanation

In this example, we will convert a map to a list of pairs. Each pair in the list represents a key-value pair from the map. This is a straightforward way to convert a map to a list while preserving the key-value relationship.

2.2 Code Implementation

Kotlin
fun main() {
    val map = hashMapOf("Apple" to 1, "Banana" to 2, "Cherry" to 3)

    // Convert map to list of pairs
    val listOfPairs: List<Pair> = map.toList()

    println("Original Map: $map")
    println("List of Pairs: $listOfPairs")
}

2.3 Output

Kotlin
Original Map: {Apple=1, Banana=2, Cherry=3}
List of Pairs: [(Apple, 1), (Banana, 2), (Cherry, 3)]

3. Example 2: Converting a Map to a List of Keys

3.1 Program Explanation

In this example, we will convert a map to a list containing only the keys from the map. This can be useful when you are only interested in the keys and not the values.

3.2 Code Implementation

Kotlin
fun main() {
    val map = hashMapOf("Apple" to 1, "Banana" to 2, "Cherry" to 3)

    // Convert map to list of keys
    val listOfKeys: List<String> = map.keys.toList()

    println("Original Map: $map")
    println("List of Keys: $listOfKeys")
}

3.3 Output

Kotlin
Original Map: {Apple=1, Banana=2, Cherry=3}
List of Keys: [Apple, Banana, Cherry]

4. Example 3: Converting a Map to a List of Values

4.1 Program Explanation

In this example, we will convert a map to a list containing only the values from the map. This is useful when you need to work with the values independently of their keys.

4.2 Code Implementation

Kotlin
fun main() {
    val map = hashMapOf("Apple" to 1, "Banana" to 2, "Cherry" to 3)

    // Convert map to list of values
    val listOfValues: List<Int> = map.values.toList()

    println("Original Map: $map")
    println("List of Values: $listOfValues")
}

4.3 Output

Kotlin
Original Map: {Apple=1, Banana=2, Cherry=3}
List of Values: [1, 2, 3]

5. Conclusion

In this article, we explored three different methods to convert a Map (or HashMap) to a List in Kotlin. The first example demonstrated how to convert a map to a list of pairs, preserving the key-value relationship. The second example showed how to extract just the keys from the map into a list. The third example illustrated how to extract the values from the map into a list. Each method leverages Kotlin’s powerful collection functions to perform these conversions efficiently and cleanly. By understanding these techniques, you can effectively manage and manipulate collections in your Kotlin programs.