如何利用 Java 将学生成绩数据从行格式转换为列格式?

如何利用 java 将学生成绩数据从行格式转换为列格式?

java 数据转换:行转列

问题:

如何将以下源数据转化为期望的输出格式?

源数据:

class    studentid    subject    score
class one    1    english    84
class one    1    math    77
class one    1    pe    69
class one    2    english    81
class one    2    math    80
...

期望输出:

class    studentid    english    math    pe
class one    1    84    77    69

解决方案:

可以通过行转列的操作实现此转换。 voici 使用 java 代码演示如何完成:

import java.util.hashmap;
import java.util.list;
import java.util.map;
import java.util.stream.collectors;

class rowtocolumntransformer {

    public static void main(string[] args) {
        // 源数据
        list<studentdata> studentdatalist = list.of(
                new studentdata("class one", 1, "english", 84),
                new studentdata("class one", 1, "math", 77),
                new studentdata("class one", 1, "pe", 69),
                new studentdata("class one", 2, "english", 81),
                new studentdata("class one", 2, "math", 80)
        );

        // 创建一个映射,使用 class 和 studentid 作为键,subject 和 score 作为值
        map<string, map<string, integer>> subjectscoremap = new hashmap<>();
        for (studentdata studentdata : studentdatalist) {
            string classandstudentid = studentdata.getclassandstudentid();
            map<string, integer> scoremap = subjectscoremap.getordefault(classandstudentid, new hashmap<>());
            scoremap.put(studentdata.getsubject(), studentdata.getscore());
            subjectscoremap.put(classandstudentid, scoremap);
        }

        // 创建期望的输出
        list<studentdata> transformeddatalist = subjectscoremap.entryset().stream()
                .map(entry -> new studentdata(entry.getkey(), entry.getvalue()))
                .collect(collectors.tolist());

        // 打印输出
        for (studentdata transformeddata : transformeddatalist) {
            system.out.println(transformeddata);
        }
    }

    private static class studentdata {
        private string classandstudentid;
        private map<string, integer> subjectscoremap;

        public studentdata(string classandstudentid, int studentid, string subject, int score) {
            this.classandstudentid = classandstudentid + "-" + studentid;
            this.subjectscoremap = map.of(subject, score);
        }

        public studentdata(string classandstudentid, map<string, integer> subjectscoremap) {
            this.classandstudentid = classandstudentid;
            this.subjectscoremap = subjectscoremap;
        }

        public string getclassandstudentid() {
            return classandstudentid;
        }

        public map<string, integer> getsubjectscoremap() {
            return subjectscoremap;
        }

        @override
        public string tostring() {
            return classandstudentid + "	" + 
                   subjectscoremap.getordefault("english", 0) + "	" + 
                   subjectscoremap.getordefault("math", 0) + "	" + 
                   subjectscoremap.getordefault("pe", 0);
        }
    }
}

输出:

Class one-1    84    77    69
Class one-2    81    80    0

以上就是如何利用 Java 将学生成绩数据从行格式转换为列格式?的详细内容,更多请关注其它相关文章!