If I have a list of objects with interesting fields that I want to copy to a new list of new (and slightly different objects) objects, how to I go about it?
Say you have two different classes:
class Person(name: String, surname: String)
class Technician(firstName: String, lastName: String, title: String)
Now assume you have a list of Technicians, what is a good way to create a list of Persons from that list of Technicians? Persons don’t have a title field, so that can be ignored, and the firstName and lastName fields from technician needs to be mapped to name and surname for each new Person in the new list.
You can use the
mapoperation of theListclass:As the name says, it maps all the objects in one list to the result of the block and returns them as a new list.