Showing posts with label HashMap. Show all posts
Showing posts with label HashMap. Show all posts

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]

android kotlin hashmap initialization

MainActivity.kt
package com.example.espl.hashmapkotlinapp
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log

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

        //    Returns a new HashMap with the specified contents, given as a list of pairs where the  first component is the key and the second is the value.
        //    fun <K, V> hashMapOf(vararg pairs: Pair<K, V>): HashMap<K, V>
        // kotlin init hashmap with values
     val specifiedContentsMap: HashMap<Int, String> = hashMapOf(1 to "x", 2 to "y", -1 to "zz")

        //define empty hash map
        val initEmptyHashMap:HashMap<Int,String> = HashMap()

        //    initialize with its initial capacity of 3
        val initialCapacityHashMap:HashMap<Int,String> = HashMap(3)

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

        // kotlin hashmap put example - add item
        hashMap.put(1,"Value1")
        hashMap.put(2,"Value2")
        hashMap.put(3,"Value3")
        hashMap.put(4,"Value4")
        hashMap.put(5,"Value5")

        // kotlin hashmap add item
        initEmptyHashMap.put(1,"Value1")
        initEmptyHashMap.put(2,"Value2")
        initEmptyHashMap.put(3,"Value3")

        initialCapacityHashMap.put(1,"Value1")
        initialCapacityHashMap.put(2,"Value2")
        initialCapacityHashMap.put(3,"Value3")
        initialCapacityHashMap.put(4,"Value4")

        //print hash map in Log
        Log.e("hashmap","::"+hashMap)
        Log.e("specifiedContentsMap","::"+specifiedContentsMap)
        Log.e("initEmptyHashMap","::"+initEmptyHashMap)
        Log.e("initialCapacityHashMap","::"+initialCapacityHashMap)
    }
}

Output:
com.example.espl.hashmapkotlinapp E/hashmap: ::{4=Value4, 1=Value1, 5=Value5, 3=Value3, 2=Value2}
com.example.espl.hashmapkotlinapp E/specifiedContentsMap: ::{-1=zz, 1=x, 2=y}
com.example.espl.hashmapkotlinapp E/initEmptyHashMap: ::{1=Value1, 3=Value3, 2=Value2}
com.example.espl.hashmapkotlinapp E/initialCapacityHashMap: ::{4=Value4, 1=Value1, 3=Value3, 2=Value2}

Popular Posts