Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to update childList of parentList on Koltlin

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

        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 ?

>Solution :

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 }
    })
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading