Home > OS >  how to update childList of parentList on Koltlin
how to update childList of parentList on Koltlin

Time:06-09

data class Skill(
    val id: Int,
    val name: String,
    val imageUrl: String? = null,
    val children: List<SkillChild>? = null,
) {
   
    data class SkillChild(
        val id: Int,
        val name: String,
        val imageUrl: String? = null,
        val note: Int? = null,
    )
}

I have 2 lists:

  • First is the full list of Skill object.
  • Second one, is a list of SkillChild object.

I want to replace all children of the First list by the second list.

Exemple:

        val skills = listOf<Skill>(
            Skill(
                id = 1,
                name = "skillName",
                imageUrl = null,
                children = listOf<Skill.SkillChild>(
                    Skill.SkillChild(
                        id = 45,
                        name = "skillChildName",
                        imageUrl = null,
                        note = null
                    ),
                    Skill.SkillChild(
                        id = 46,
                        name = "skillChildName",
                        imageUrl = null,
                        note = null
                    ),
                    Skill.SkillChild(
                        id = 47,
                        name = "skillChildName",
                        imageUrl = null,
                        note = null
                    ),
                )
            )
        )

        val skillChildren = listOf<Skill.SkillChild>(
            Skill.SkillChild(
                id = 45,
                name = "skillChildName",
                imageUrl = null,
                note = 2
            ),
            Skill.SkillChild(
                id = 46,
                name = "skillChildName",
                imageUrl = null,
                note = 3
            ),
        )

Then the result I'm expecting is:

    val result = listOf<Skill>(
        Skill(
            id = 1,
            name = "skillName",
            imageUrl = null,
            children = listOf<Skill.SkillChild>(
                Skill.SkillChild(
                    id = 45,
                    name = "skillChildName",
                    imageUrl = null,
                    note = 2
                ),
                Skill.SkillChild(
                    id = 46,
                    name = "skillChildName",
                    imageUrl = null,
                    note = 3
                ),
                Skill.SkillChild(
                    id = 47,
                    name = "skillChildName",
                    imageUrl = null,
                    note = null
                ),
            )
        )
    )

Of course the skill list will contain more than one Skill.

I'm a bit confuse on Collection, can someone help me ?

CodePudding user response:

data class Skill(
  val id: Int,
  val name: String,
  val imageUrl: String? = null,
  val children: List<SkillChild>? = null,
) {
  data class SkillChild(
    val id: Int,
    val name: String,
    val imageUrl: String? = null,
    val note: Int? = null,
  )
}

val skills = listOf(
  Skill(
    1, "skillName", null, listOf(
      Skill.SkillChild(45, "skillChildName", null, null),
      Skill.SkillChild(46, "skillChildName", null, null),
      Skill.SkillChild(47, "skillChildName", null, null),
    )
  )
)

val skillChildren = listOf(
  Skill.SkillChild(45, "skillChildName", null, 2),
  Skill.SkillChild(46, "skillChildName", null, 3),
)

val mapIdToSkillChildren = skillChildren.associateBy { it.id }

val result = skills.map { skill ->
  skill.copy(children = skill.children?.map { child ->
    mapIdToSkillChildren.getOrElse(child.id) { child }
  })   // removed  ?: skill.children  based on @IvoBeckers comment below
}

Edit: it make make sense to not create a copy of a Skill which is not found in skillChildren:

val result = skills.map { skill ->
  when (skill.children) {
    null -> skill
    else -> skill.copy(children = skill.children.map { child ->
      mapIdToSkillChildren.getOrElse(child.id) { child }
    })
  }
}

CodePudding user response:

This is a possible way:

val result = skills.map { skill ->
    skill.copy(children = skill.children?.map { child ->
        skillChildren.find { child.id == it.id }  ?: child
    })
}
  • Related