Home > OS >  Re-index a list to nested map in kotlin
Re-index a list to nested map in kotlin

Time:04-06

I have a variables of type var students = List<StudentData>. I need to create a map with the following structure: Map<String, Map<Int, StudentData>>.

StudentData has information like: studentID, classId, courseA, courseB, courseC etc.

So for the map would be the following: Map<studentID, Map <classId, StudentData>>. Meaning that for every student, I need a map of his classId and all the information. How can I achieve this using kotlin ?

            var indexByCourse = Map<String, Map<Int, StudentData>>
            students.forEach {
                student -> indexByCourse.getOrPut(student.studentID, {mutableSetOf()}.add(??)
                    }

Not sure how to approach this problem. Any guidance is appreciated!

CodePudding user response:

You can try the below way to map classId with it's corresponding information.

fun testFunction() {
        //for holding student data
        val students = ArrayList<StudentData>()
        students.add(StudentData(1, 10, "course a", "course 3"))
        students.add(StudentData(2, 11, "course b", "course 6"))
        students.add(StudentData(3, 12, "course c", "course 2"))
        students.add(StudentData(4, 13, "course d", "course 5"))

        //For holding course
        val indexByCourse = hashMapOf<Int, HashMap<Int, StudentData>>()

        students.forEachIndexed { index, studentData ->
            val studentSubData = hashMapOf(studentData.classId to studentData)
            indexByCourse[studentData.studentID] = studentSubData
        }

        //Fetch the student data based on studentID
        println(indexByCourse[3])

    }

and StudentData class for holding the values

data class StudentData(var studentID: Int, var classId: Int, var courseA: String, var courseB: String)

CodePudding user response:

data class StudentData(
  val studentID: String,
  val classId: Int,
  val courseA: String? = "",
  val courseB: String? = "",
  val courseC: String? = ""
)

val students = listOf(
  StudentData("1", 100, "Course 1"),
  StudentData("2", 100, "Course 1", "Course 2", "Course 3"),
  StudentData("3", 100, "Course 2", "Course 3"),
  StudentData("4", 200, "Course 1", "Course 3", "Course 3"),
  StudentData("5", 200, "Course 1", "Course 2")
)

val result = students
  .groupBy { studentData -> studentData.studentID }
  .map { (studentID, values) ->
    studentID to values
      .groupBy { studentData -> studentData.classId }
      .map { (classId, values) -> classId to values.first() }
      .toMap()
  }
  .toMap()

result.forEach(::println)

Output:

1={100=StudentData(studentID=1, classId=100, courseA=Course 1, courseB=, courseC=)}
2={100=StudentData(studentID=2, classId=100, courseA=Course 1, courseB=Course 2, courseC=Course 3)}
3={100=StudentData(studentID=3, classId=100, courseA=Course 2, courseB=Course 3, courseC=)}
4={200=StudentData(studentID=4, classId=200, courseA=Course 1, courseB=Course 3, courseC=Course 3)}
5={200=StudentData(studentID=5, classId=200, courseA=Course 1, courseB=Course 2, courseC=)}
  • Related