
- Android Basics
- Android - Home
- Android - Overview
- Android - Environment Setup
- Android - Architecture
- Android - Application Components
- Android - Hello World Example
- Android - Resources
- Android - Activities
- Android - Services
- Android - Broadcast Receivers
- Android - Content Providers
- Android - Fragments
- Android - Intents/Filters
- Android - User Interface
- Android - UI Layouts
- Android - UI Controls
- Android - Event Handling
- Android - Styles and Themes
- Android - Custom Components
- Android Advanced Concepts
- Android - Drag and Drop
- Android - Notifications
- Location Based Services
- Android - Sending Email
- Android - Sending SMS
- Android - Phone Calls
- Publishing Android Application
- Android Useful Examples
- Android - Alert Dialoges
- Android - Animations
- Android - Audio Capture
- Android - AudioManager
- Android - Auto Complete
- Android - Best Practices
- Android - Bluetooth
- Android - Camera
- Android - Clipboard
- Android - Custom Fonts
- Android - Data Backup
- Android - Developer Tools
- Android - Emulator
- Android - Facebook Integration
- Android - Gestures
- Android - Google Maps
- Android - Image Effects
- Android - ImageSwitcher
- Android - Internal Storage
- Android - JetPlayer
- Android - JSON Parser
- Android - Linkedin Integration
- Android - Loading Spinner
- Android - Localization
- Android - Login Screen
- Android - MediaPlayer
- Android - Multitouch
- Android - Navigation
- Android - Network Connection
- Android - NFC Guide
- Android - PHP/MySQL
- Android - Progress Circle
- Android - ProgressBar
- Android - Push Notification
- Android - RenderScript
- Android - RSS Reader
- Android - Screen Cast
- Android - SDK Manager
- Android - Sensors
- Android - Session Management
- Android - Shared Preferences
- Android - SIP Protocol
- Android - Spelling Checker
- Android - SQLite Database
- Android - Support Library
- Android - Testing
- Android - Text to Speech
- Android - TextureView
- Android - Twitter Integration
- Android - UI Design
- Android - UI Patterns
- Android - UI Testing
- Android - WebView Layout
- Android - Wi-Fi
- Android - Widgets
- Android - XML Parsers
- Android Useful Resources
- Android - Questions and Answers
- Android - Useful Resources
- Android - Discussion
How to convert a List to a Map in Kotlin?
In this article, we will see how we can convert a List to a Map using various options provided by the Kotlin Library.
Example: Using associate()
The most standard way of converting a list into a map is by using the associate() function. This function takes a list of items as an argument and it returns a map containing key-value pairs. In the following example, we will see how it works.
Example
data class mySubjectList(var name: String, var priority: String) fun main() { val mySubjectList: List<mySubjectList> = listOf( mySubjectList("Java", "1"), mySubjectList("Kotlin", "2"), mySubjectList("C", "3") ) // Creating a map and adding my own list of values in it. val myMap: Map<String, String> = mySubjectList.associate { Pair(it.priority, it.name) } println(myMap) }
Output
Once we run the above piece of code, it will generate the following output which is a map and we get the output in a key-value format.
{1=Java, 2=Kotlin, 3=C}
Example: Using associateBy()
AssociateBy() is another function that can be used in order to transform a list into a Map. In the following example, we will see how we can implement the same.
Example
data class mySubjectList(var name: String, var priority: String) fun main() { val mySubjectList: List<mySubjectList> = listOf( mySubjectList("Java", "1"), mySubjectList("Kotlin", "2"), mySubjectList("C", "3") ) // Creating a map and adding my own list of the values in it val myMap: Map<String, String> = mySubjectList.associateBy( {it.priority}, {it.name} ) println(myMap) }
Output
It will generate the following output which is a map and we get the output in a key-value format.
{1=Java, 2=Kotlin, 3=C}
Example: Using toMap()
Kotlin library provides another function to convert a list of items into a Map. Kotlin Map class contains a function called toMap() which returns a new map containing all the key-value pairs from a given collection. Let's see how it works.
Example
data class mySubjectList(var name: String, var priority: String) fun main() { val mySubjectList: List<mySubjectList> = listOf( mySubjectList("Java", "1"), mySubjectList("Kotlin", "2"), mySubjectList("C", "3") ) // Creating a map and adding my own list of the values in it . val myMap: Map<String, String> = mySubjectList.map{ it.priority to it.name }.toMap() println(myMap) }
Output
Once we run the above piece of code, it will generate the following output which is a map and we get the output in a key-value format.
{1=Java, 2=Kotlin, 3=C}
- Related Articles
- How to reverse a Map in Kotlin?
- Haskell Program to Convert List to a Map
- Java Program to convert Properties list into a Map
- Golang Program to Convert List to Map
- Java program to convert the contents of a Map to list
- How to create a list in Kotlin?
- How to convert a Bitmap to Drawable in Kotlin?
- Java Program to convert a Map to a read only map
- Convert object to a Map - JavaScript
- Program to convert a Map to a Stream in Java
- How to convert a Drawable to a Bitmap in Android using Kotlin?
- How to add an item to a list in Kotlin?
- How to clone or copy a list in Kotlin?
- How to Convert a PivotTable to a List in Excel?
- How to convert a Kotlin source file to a Java source file?
