{"id":36509,"date":"2024-11-26T17:43:53","date_gmt":"2024-11-26T09:43:53","guid":{"rendered":"https:\/\/fwq.ai\/blog\/36509\/"},"modified":"2024-11-26T17:43:53","modified_gmt":"2024-11-26T09:43:53","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:\/\/fwq.ai\/blog\/36509\/","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><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173128393899403.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5229\u7528 Java \u5c06\u5b66\u751f\u6210\u7ee9\u6570\u636e\u4ece\u884c\u683c\u5f0f\u8f6c\u6362\u4e3a\u5217\u683c\u5f0f\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5229\u7528 Java \u5c06\u5b66\u751f\u6210\u7ee9\u6570\u636e\u4ece\u884c\u683c\u5f0f\u8f6c\u6362\u4e3a\u5217\u683c\u5f0f\uff1f\u63d2\u56fe\" \/><\/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<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u671f\u671b\u8f93\u51fa\uff1a<\/p>\n<pre>class    studentid    english    math    pe\nclass one    1    84    77    69<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5229\u7528 Java \u5c06\u5b66\u751f\u6210\u7ee9\u6570\u636e\u4ece\u884c\u683c\u5f0f\u8f6c\u6362\u4e3a\u5217\u683c\u5f0f\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>java \u6570\u636e\u8f6c\u6362\uff1a\u884c\u8f6c\u5217 \u95ee\u9898\uff1a \u5982\u4f55\u5c06\u4ee5\u4e0b\u6e90\u6570\u636e\u8f6c\u5316\u4e3a\u671f\u671b\u7684\u8f93\u51fa\u683c\u5f0f\uff1f \u6e90\u6570\u636e\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b 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 &#8230; \u767b\u5f55\u540e\u590d\u5236 \u671f\u671b\u8f93\u51fa\uff1a class studentid english math pe class one 1 84 77 69 \u767b\u5f55\u540e\u590d\u5236 \u89e3\u51b3\u65b9\u6848\uff1a \u53ef\u4ee5\u901a\u8fc7\u884c\u8f6c\u5217\u7684\u64cd\u4f5c\u5b9e\u73b0\u6b64\u8f6c\u6362\u3002 voici \u4f7f\u7528 java [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-36509","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36509","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=36509"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36509\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=36509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=36509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=36509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}