How to convert/transform a collection to another collection by element’s property?

it’s easy to do in Kotlin:

//           v--- the variable type can be removed
var nameMap: MutableList<String> = persons.map { it.name }.toMutableList();

IF you want an immutable List, it can simplify as below:

//           v--- the variable type can be removed
var nameMap: List<String> = persons.map { it.name };

OR using function reference expression instead:

var nameMap = persons.map(Person::name);

Leave a Comment