Skip to content
Advertisement

Copy some values from one ArrayList to another kotlin

I have list:

var skillsList: MutableList<ComplexModel> = mutableListOf()

ComplexModel contains (Name,Title,StartDate,Summarry)

I want to find better way to create new MutableList from skillsList but the new list have to get only Name and Title from the first.For the moment i`m with this solution but i wonder if there are better way to achieve this.

   val newArray: MutableList<SkillModel> = mutableListOf()

    for (skill in skillsList) {
        newArray.add(SkillModel(name = skill.name, title = skill.title))
    }

Advertisement

Answer

var skills: List<SkillModel> = skillsList.map { SkillModel(it.name,it.title) }
Advertisement