{"id":203687,"date":"2025-05-22T08:50:22","date_gmt":"2025-05-22T00:50:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203687\/"},"modified":"2025-05-22T08:50:22","modified_gmt":"2025-05-22T00:50:22","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8-java-%e5%b0%86%e5%ad%a6%e7%94%9f%e6%88%90%e7%bb%a9%e6%95%b0%e6%8d%ae%e4%bb%8e%e8%a1%8c%e6%a0%bc%e5%bc%8f%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%88%97%e6%a0%bc%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203687\/","title":{"rendered":"\u5982\u4f55\u5229\u7528 Java \u5c06\u5b66\u751f\u6210\u7ee9\u6570\u636e\u4ece\u884c\u683c\u5f0f\u8f6c\u6362\u4e3a\u5217\u683c\u5f0f\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u5229\u7528 Java \u5c06\u5b66\u751f\u6210\u7ee9\u6570\u636e\u4ece\u884c\u683c\u5f0f\u8f6c\u6362\u4e3a\u5217\u683c\u5f0f\uff1f<\/h1>\n<p>\u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5229\u7528 Java \u5c06\u5b66\u751f\u6210\u7ee9\u6570\u636e\u4ece\u884c\u683c\u5f0f\u8f6c\u6362\u4e3a\u5217\u683c\u5f0f\uff1f\u300b<\/span>\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241118\/1731898292673aabb4a480a.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>java \u6570\u636e\u8f6c\u6362\uff1a\u884c\u8f6c\u5217<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u5982\u4f55\u5c06\u4ee5\u4e0b\u6e90\u6570\u636e\u8f6c\u5316\u4e3a\u671f\u671b\u7684\u8f93\u51fa\u683c\u5f0f\uff1f<\/p>\n<p>\u6e90\u6570\u636e\uff1a<\/p>\n<pre>class    studentid    subject    score\nclass one    1    english    84\nclass one    1    math    77\nclass one    1    pe    69\nclass one    2    english    81\nclass one    2    math    80\n...<\/pre>\n<p>\u671f\u671b\u8f93\u51fa\uff1a<\/p>\n<pre>class    studentid    english    math    pe\nclass one    1    84    77    69<\/pre>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7\u884c\u8f6c\u5217\u7684\u64cd\u4f5c\u5b9e\u73b0\u6b64\u8f6c\u6362\u3002 voici \u4f7f\u7528 java \u4ee3\u7801\u6f14\u793a\u5982\u4f55\u5b8c\u6210\uff1a<\/p>\n<pre>import java.util.hashmap;\nimport java.util.list;\nimport java.util.map;\nimport java.util.stream.collectors;\n\nclass rowtocolumntransformer {\n\n    public static void main(string[] args) {\n        \/\/ \u6e90\u6570\u636e\n        list&lt;studentdata&gt; studentdatalist = list.of(\n                new studentdata(\"class one\", 1, \"english\", 84),\n                new studentdata(\"class one\", 1, \"math\", 77),\n                new studentdata(\"class one\", 1, \"pe\", 69),\n                new studentdata(\"class one\", 2, \"english\", 81),\n                new studentdata(\"class one\", 2, \"math\", 80)\n        );\n\n        \/\/ \u521b\u5efa\u4e00\u4e2a\u6620\u5c04\uff0c\u4f7f\u7528 class \u548c studentid \u4f5c\u4e3a\u952e\uff0csubject \u548c score \u4f5c\u4e3a\u503c\n        map&lt;string, map&lt;string, integer&gt;&gt; subjectscoremap = new hashmap&lt;&gt;();\n        for (studentdata studentdata : studentdatalist) {\n            string classandstudentid = studentdata.getclassandstudentid();\n            map&lt;string, integer&gt; scoremap = subjectscoremap.getordefault(classandstudentid, new hashmap&lt;&gt;());\n            scoremap.put(studentdata.getsubject(), studentdata.getscore());\n            subjectscoremap.put(classandstudentid, scoremap);\n        }\n\n        \/\/ \u521b\u5efa\u671f\u671b\u7684\u8f93\u51fa\n        list&lt;studentdata&gt; transformeddatalist = subjectscoremap.entryset().stream()\n                .map(entry -&gt; new studentdata(entry.getkey(), entry.getvalue()))\n                .collect(collectors.tolist());\n\n        \/\/ \u6253\u5370\u8f93\u51fa\n        for (studentdata transformeddata : transformeddatalist) {\n            system.out.println(transformeddata);\n        }\n    }\n\n    private static class studentdata {\n        private string classandstudentid;\n        private map&lt;string, integer&gt; subjectscoremap;\n\n        public studentdata(string classandstudentid, int studentid, string subject, int score) {\n            this.classandstudentid = classandstudentid + \"-\" + studentid;\n            this.subjectscoremap = map.of(subject, score);\n        }\n\n        public studentdata(string classandstudentid, map&lt;string, integer&gt; subjectscoremap) {\n            this.classandstudentid = classandstudentid;\n            this.subjectscoremap = subjectscoremap;\n        }\n\n        public string getclassandstudentid() {\n            return classandstudentid;\n        }\n\n        public map&lt;string, integer&gt; getsubjectscoremap() {\n            return subjectscoremap;\n        }\n\n        @override\n        public string tostring() {\n            return classandstudentid + \"\\t\" + \n                   subjectscoremap.getordefault(\"english\", 0) + \"\\t\" + \n                   subjectscoremap.getordefault(\"math\", 0) + \"\\t\" + \n                   subjectscoremap.getordefault(\"pe\", 0);\n        }\n    }\n}<\/pre>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>Class one-1    84    77    69\nClass one-2    81    80    0<\/pre>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u5229\u7528 Java \u5c06\u5b66\u751f\u6210\u7ee9\u6570\u636e\u4ece\u884c\u683c\u5f0f\u8f6c\u6362\u4e3a\u5217\u683c\u5f0f\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5229\u7528 Java \u5c06\u5b66\u751f\u6210\u7ee9\u6570&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-203687","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203687","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=203687"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203687\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203687"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}