我有一个名为Student的类,有三个字段,我需要将该类转换为GroupedStudent。如何实现这一目标?
<script>
window.onload = function() {
var elements = document.querySelectorAll('.Header-branding-logo,.Mobile-bar-branding-logo');
elements.forEach((element) => {
element.src = "https://static1.squarespace.com/static/5a9e8da0aa49a18379207907/t/5ac52f062b6a28a603df30cf/1522872070340/GBL+Shop+-+Black+1500.png"
})
};
</script>
我需要将上面的列表转换为GroupedStudent List,我该怎么做?
它们应按studentName分组,并将SubjectMarks列为列表。
var students: List<Student> = mutableListOf(Student("A", "X", 1), Student("A", "Y", 2), Student("B", "X", 2), Student("B", "Y", 2))
答案 0 :(得分:3)
使用Kotlin stdlib Collections functions:
var students: List<Student> = mutableListOf(Student("A", "X", 1), Student("A", "Y", 2), Student("B", "X", 2), Student("B", "Y", 2))
val groupedMap = students.groupBy { it.name }
val groupedStudents = mutableListOf<GroupedStudent>()
groupedMap.forEach { key, value ->
val groupedStudent = GroupedStudent(key, value.map { SubjectMarks(it.subject, it.marks) }.toMutableList())
groupedStudents.add(groupedStudent)
}
这将产生List
GroupedStudent
,每个GroupedStudent
包含一个学生列表&#39}。标记谁有这个名字。
答案 1 :(得分:1)
如果您的Student类是正确的,即标记应该是Int而不是数组或整数列表,并且您真的想将其转换为&#34; GroupedStudent List&#34;你可以使用sortedWith和map方法:
class Student(var name: String, var subject: String, var marks: Int) {
override fun toString(): String = "name: $name, subject: $subject, marks: $marks"
}
class GroupedStudent(var name: String, var subMarks: MutableList<SubjectMarks>) {
override fun toString(): String = "name: $name, subject: $subMarks"
}
class SubjectMarks(var subject: String, var marks: Int) {
override fun toString(): String = "subject name: $subject, marks: $marks"
}
fun main(args: Array<String>) {
val students: List<Student> = mutableListOf(Student("B", "Y", 2),
Student("A", "X", 1),
Student("B", "X", 2),
Student("A", "Y", 2))
val groupedStudents: List<GroupedStudent> = students.sortedWith(compareBy({ it.name }))
.map { student -> GroupedStudent(student.name, mutableListOf(SubjectMarks(student.subject, student.marks))) }
println(groupedStudents)
}
[name: A, subject: [subject name: X, marks: 1], name: A, subject: [subject name: Y, marks: 2], name: B, subject: [subject name: Y, marks: 2], name: B, subject: [subject name: X, marks: 2]]
但是如果要将学生对象分组到名称为关键的地图中,并将主题/标记列表作为对子列表,则可以使用groupBy:
class Student(var name: String, var subject: String, var marks: Int) {
override fun toString(): String = "name: $name, subject: $subject, marks: $marks"
}
class GroupedStudent(var name: String, var subMarks: MutableList<SubjectMarks>) {
override fun toString(): String = "name: $name, subject: $subMarks"
}
class SubjectMarks(var subject: String, var marks: Int) {
override fun toString(): String = "subject name: $subject, marks: $marks"
}
fun main(args: Array<String>) {
val students: List<Student> = mutableListOf(Student("B", "Y", 2),
Student("A", "X", 1),
Student("B", "X", 2),
Student("A", "Y", 2))
val groupedStudentsMap = mutableMapOf<String, List<Pair<String, Int>>>()
students.groupBy { it.name }
.forEach({ (key, value) ->
groupedStudentsMap[key] = value.map { element -> Pair(element.subject, element.marks) } })
println(groupedStudentsMap)
}
{B=[(Y, 2), (X, 2)], A=[(X, 1), (Y, 2)]}
答案 2 :(得分:1)
您可以按学生的姓名分组,然后执行两个简单的映射:
students.groupBy(Student::name).map { (name, students) ->
GroupedStudent(name, students.map { SubjectMarks(it.subject, it.marks) }.toMutableList())
}