![]() ![]() Next, we call the UserView constructor with a custom parameter mapping. We also don’t want to break the encapsulation of our User class and use a helper class to reach into our User object and pull out its data, in order to create an UserView object.įun User.toUserViewReflection() = with(::UserView). Since UserView is on the outer layer of our application, we don’t want to add this function to our User class. What we need now is a mapping function, that will map User-> UserView. While it’s representing the same data in our domain, some fields are an aggregate of the fields of our User class and some fields simply have a different name: data class UserView( We’ve decided to call this class UserView and we can imagine it being used as a response send from a web controller. Now we want to provide a different view on this data. It’s also possible that it’s an entity we load from a relational database. Comment down below if you have any queries.Let’s use the following example: We have a class User, which might be a class from our core domain. ![]() I hope you got an idea of how to use map in kotlin. ![]() Yugansh Generic Example fun main(args: Array)") Val myMap = mapOf(1 to "Ishank", 4 to "Aditya", 3 to "Yugansh") If we want to use the Map interface in the Kotlin than we have to use the function called mapOf() or we can use mapOf(k,v)>(). Map interface is immutable in nature, have a fixed size and the methods available in the Kotlin Map Interface supports only the read only access. The key and value may be in the different pairs such as: The key in the (K,V) stands for the key is unique and can only hold one value for each key. It also stores the data in the form of (K,V) key and Value pair. Kotlin Map is an interface and is the generic collection of all the elements. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |