How to Convert Map (HashMap) to List in Kotlin Android

This example Program to Convert Map (HashMap) to List in Kotlin Android

MainActivity.kt
package com.example.espl.hashmaptolistkotlin
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import java.security.KeyPair

class MainActivity : AppCompatActivity() {
    //    fun <K, V> hashMapOf(): HashMap<K, V>
    //    Returns an empty new HashMap.
    //Method hashMapOf() in Kotlin
    val hashMap: HashMap<Int,String> = hashMapOf()

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

    hashMap.put(1,"Value1")
    hashMap.put(2,"Value2")
    hashMap.put(3,"Value3")
    hashMap.put(4,"Value4")
    hashMap.put(5,"Value5")
        //print hash map in Log
        Log.e("hashmap","::"+hashMap)

    //         Returns a [List] containing all key-value pairs.
    //        public fun <K, V> Map<out K, V>.toList(): List<Pair<K, V>>
        // Convert Kotlin Map to List using toList() method
        val pairKeyValueList : List<Pair<Int, String>> = hashMap.toList();
        Log.e("HashmapPairKeyValueList","::"+pairKeyValueList)

        // Convert Kotlin Map to List of keys after extracting the keys
        //public Set<K> keySet() method return a set view of the keys contained in this map
        val listKeys : Set<Int> = hashMap.keys
        //Convert the returned set to ArrayList
        val arraListOfKeys: List<Int> = ArrayList<Int>(listKeys.toList())
        Log.e("hashmap:arraListOfKeys","::"+arraListOfKeys)

        // Convert Kotlin Map to List after extracting the values
        //public Collection<V> values() method return a view of the values contained in this map
        val listValues = hashMap.values
        //Convert the returned Collection to ArrayList
        val arraListOfvalues: List<String> = ArrayList<String>(listValues.toList())
        Log.e("hashmap:arraListOfvalues","::"+arraListOfvalues)

        val students:HashMap<Int,Student> = hashMapOf()
        students.put(1, Student("Ram", "VI", Address("New street", "Ahmedabad")))
        students.put(2, Student("Raju", "V", Address("Green cross", "Rajkot")))

        val pairKeyValueListStudents:ArrayList<Pair<Int,Student>>  = ArrayList(students.toList())
        Log.e("HashmapPairKeyValueList","::"+pairKeyValueListStudents)

        val listKeysStudents: Set<Int> = students.keys
        Log.e("map:listKeysStudents","::"+listKeysStudents)

        //public Collection<V> values() method return a view of the values(student objects) contained in this map
        val listValuesStudents:ArrayList<Student> = ArrayList(students.values)
        Log.e("listValuesStudents","::"+listValuesStudents)

        val listValuesStudentsAddress:ArrayList<Address> = ArrayList(students.values.map { x->x.address})
        Log.e("listValStudentsAddress","::"+listValuesStudentsAddress)

// While working with Map Object, we can associate with map(transform: (T) -> R) function to customise a returned-list
        val listValuesStudentsCity: List<String?> = students.values.map { x->x.address.city}
        Log.e("listValuesStudentsCity","::"+listValuesStudentsCity)
    }

    data class Address(
        var street : String? = null,
        var city : String? = null
    ){}

    data class Student(
        var name: String? = null,
        var std: String? = null,
        var address: Address = Address()) {
    }
}
Output:
com.example.espl.hashmaptolistkotlin E/hashmap: ::{4=Value4, 1=Value1, 5=Value5, 3=Value3, 2=Value2}
com.example.espl.hashmaptolistkotlin E/HashmapPairKeyValueList: ::[(4, Value4), (1, Value1), (5, Value5), (3, Value3), (2, Value2)]
com.example.espl.hashmaptolistkotlin E/hashmap:arraListOfKeys: ::[4, 1, 5, 3, 2]
com.example.espl.hashmaptolistkotlin E/hashmap:arraListOfvalues: ::[Value4, Value1, Value5, Value3, Value2]
com.example.espl.hashmaptolistkotlin E/HashmapPairKeyValueList: ::[(1, Student(name=Ram, std=VI, address=Address(street=New street, city=Ahmedabad))), (2, Student(name=Raju, std=V, address=Address(street=Green cross, city=Rajkot)))]

com.example.espl.hashmaptolistkotlin E/map:listKeysStudents: ::[1, 2]
com.example.espl.hashmaptolistkotlin E/listValuesStudents: ::[Student(name=Ram, std=VI, address=Address(street=New street, city=Ahmedabad)), Student(name=Raju, std=V, address=Address(street=Green cross, city=Rajkot))]

com.example.espl.hashmaptolistkotlin E/listValStudentsAddress: ::[Address(street=New street, city=Ahmedabad), Address(street=Green cross, city=Rajkot)]
com.example.espl.hashmaptolistkotlin E/listValuesStudentsCity: ::[Ahmedabad, Rajkot]

No comments:

Post a Comment

Popular Posts