I am trying to convert ArrayList of some class to a map by it's fields , if the filed name is "firstName" and it's value in the object that I am trying to convert is Sam than the key would be <"firstName", "Sam"> but the value is not always String, and the name of the key is the same name as the filed.
Asked
Active
Viewed 406 times
0
-
can you show us an example, how the JSON representation of your model class looks like? also how your map will look when you convert the specific model class? – Roaim Sep 15 '19 at 02:33
-
This may help- https://stackoverflow.com/a/50354581/97714 – Gulshan Sep 15 '19 at 07:43
1 Answers
0
Should be able to use associateByTo
to pull it off.
Given your list:
val fieldMap = mutableMapOf<Any, Any>()
list.asscoiateByTo(fieldMap, { SomeClass::someField.returnType.javaType }, { it.someField })
You will need the kotlin.reflect.jvm package as well

Brandon McAnsh
- 992
- 8
- 18
-
Thanks it seems like what i was looking for. I have never used reflection before, is it adviced to use it preformance wise? – The Y.c Sep 15 '19 at 02:08
-
Depending on the scope of what you need it for depends on it's impact. You shouldn't notice it for this. – Brandon McAnsh Sep 15 '19 at 02:17