{"id":3984,"date":"2018-06-14T21:39:13","date_gmt":"2018-06-14T13:39:13","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=3984"},"modified":"2018-06-14T21:39:13","modified_gmt":"2018-06-14T13:39:13","slug":"java%e4%b8%ad%e7%9a%84%e6%b3%9b%e5%9e%8b%e7%bc%96%e7%a8%8b-%e4%bb%a5%e6%89%93%e5%8d%b0%e6%95%b0%e7%bb%84%e4%b8%ba%e4%be%8b","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/3984.html","title":{"rendered":"Java\u4e2d\u7684\u6cdb\u578b\u7f16\u7a0b-\u4ee5\u6253\u5370\u6570\u7ec4\u4e3a\u4f8b"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u5f88\u65e9\u4e4b\u524d\u6709\u4e86\u89e3\u8fc7\u4ec0\u4e48\u662f\u6cdb\u578b\u7f16\u7a0b\uff0c\u4f46\u4e00\u76f4\u90fd\u6ca1\u6709\u673a\u4f1a\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u5b9e\u8df5\uff0c\u521a\u597d\u6700\u8fd1\u5728\u5b66Java\uff0c\u7167\u7740\u6559\u7a0b\u91cc\u7684\u4ee3\u7801\u8bd5\u7740\u6539\u4e86\u4e00\u4e0b\uff0c\u52a0\u6df1\u4e86\u5bf9\u6cdb\u578b\u7f16\u7a0b\u7684\u7406\u89e3\uff0c\u4ee5\u53ca\u5b9e\u9645\u7f16\u5199\u4e2d\u8981\u6ce8\u610f\u7684\u4e00\u4e9b\u70b9\u3002<\/p>\n<h4 id=\"id-\u6a21\u677f-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<p>Java \u6cdb\u578b\uff08generics\uff09\u662f JDK 5 \u4e2d\u5f15\u5165\u7684\u4e00\u4e2a\u65b0\u7279\u6027\uff0c\u6cdb\u578b\u63d0\u4f9b\u4e86\u7f16\u8bd1\u65f6\u7c7b\u578b\u5b89\u5168\u68c0\u6d4b\u673a\u5236\uff0c\u8be5\u673a\u5236\u5141\u8bb8\u7a0b\u5e8f\u5458\u5728\u7f16\u8bd1\u65f6\u68c0\u6d4b\u5230\u975e\u6cd5\u7684\u7c7b\u578b\u3002<\/p>\n<p><strong>\u6cdb\u578b\u7684\u672c\u8d28\u662f\u53c2\u6570\u5316\u7c7b\u578b\uff0c\u4e5f\u5c31\u662f\u8bf4\u6240\u64cd\u4f5c\u7684\u6570\u636e\u7c7b\u578b\u88ab\u6307\u5b9a\u4e3a\u4e00\u4e2a\u53c2\u6570\u3002<\/strong><\/p>\n<p><strong>\u6cdb\u578b\u65b9\u6cd5\u2014\u2014\u8be5\u65b9\u6cd5\u5728\u8c03\u7528\u65f6\u53ef\u4ee5\u63a5\u6536\u4e0d\u540c\u7c7b\u578b\u7684\u53c2\u6570\u3002\u6839\u636e\u4f20\u9012\u7ed9\u6cdb\u578b\u65b9\u6cd5\u7684\u53c2\u6570\u7c7b\u578b\uff0c\u7f16\u8bd1\u5668\u9002\u5f53\u5730\u5904\u7406\u6bcf\u4e00\u4e2a\u65b9\u6cd5\u8c03\u7528\u3002<\/strong><\/p>\n<p>\u5b9a\u4e49\u6cdb\u578b\u65b9\u6cd5\u7684\u89c4\u5219\uff1a<\/p>\n<ul>\n<li><span id=\"result_box\">\u6240\u6709<\/span>\u6cdb\u578b\u65b9\u6cd5\u58f0\u660e\u90fd\u6709<strong>\u4e00\u4e2a\u7c7b\u578b\u53c2\u6570\u58f0\u660e\u90e8\u5206\uff08\u7531\u5c16\u62ec\u53f7\u5206\u9694\uff09<\/strong>\uff0c\u8be5\u7c7b\u578b\u53c2\u6570\u58f0\u660e\u90e8\u5206\u5728\u65b9\u6cd5\u8fd4\u56de\u7c7b\u578b\u4e4b\u524d\uff08\u5728\u4e0b\u9762\u4f8b\u5b50\u4e2d\u7684&lt;E&gt;\uff09\u3002<\/li>\n<li>\u6bcf\u4e00\u4e2a\u7c7b\u578b\u53c2\u6570\u58f0\u660e\u90e8\u5206\u5305\u542b\u4e00\u4e2a\u6216\u591a\u4e2a\u7c7b\u578b\u53c2\u6570\uff0c\u53c2\u6570\u95f4\u7528\u9017\u53f7\u9694\u5f00\u3002\u4e00\u4e2a\u6cdb\u578b\u53c2\u6570\uff0c\u4e5f\u88ab\u79f0\u4e3a\u4e00\u4e2a\u7c7b\u578b\u53d8\u91cf\uff0c\u662f\u7528\u4e8e\u6307\u5b9a\u4e00\u4e2a\u6cdb\u578b\u7c7b\u578b\u540d\u79f0\u7684\u6807\u8bc6\u7b26\u3002<\/li>\n<li><strong>\u7c7b\u578b\u53c2\u6570\u80fd\u88ab\u7528\u6765\u58f0\u660e\u8fd4\u56de\u503c\u7c7b\u578b\uff0c\u5e76\u4e14\u80fd\u4f5c\u4e3a\u6cdb\u578b\u65b9\u6cd5\u5f97\u5230\u7684\u5b9e\u9645\u53c2\u6570\u7c7b\u578b\u7684\u5360\u4f4d\u7b26\u3002<\/strong><\/li>\n<li>\u6cdb\u578b\u65b9\u6cd5\u4f53\u7684\u58f0\u660e\u548c\u5176\u4ed6\u65b9\u6cd5\u4e00\u6837\u3002<strong>\u6ce8\u610f\u7c7b\u578b\u53c2\u6570\u53ea\u80fd\u4ee3\u8868\u5f15\u7528\u578b\u7c7b\u578b\uff0c\u4e0d\u80fd\u662f\u539f\u59cb\u7c7b\u578b\uff08\u50cfint,double,char\u7684\u7b49\uff09\u3002<\/strong><\/li>\n<\/ul>\n<pre class=\"lang:default decode:true \">package com.ixyzero.learn.misc;\r\n\r\nimport java.util.HashMap;\r\nimport java.util.Map;\r\n\r\n\/**\r\n * http:\/\/www.runoob.com\/java\/java-generics.html\r\n * https:\/\/stackoverflow.com\/questions\/109383\/sort-a-mapkey-value-by-values\r\n *\/\r\npublic class TestGenericMethod {\r\n    \/\/ \u6cdb\u578b\u65b9\u6cd5 printArray\r\n    public static &lt; E &gt; void printArray( E[] inputArray )\r\n    {\r\n        \/\/ \u8f93\u51fa\u6570\u7ec4\u5143\u7d20\r\n        for ( E element : inputArray ){\r\n            System.out.printf( \"%s \", element );\r\n        }\r\n        System.out.println();\r\n    }\r\n\r\n    \/\/ \u6cdb\u578b\u65b9\u6cd5 printMap\r\n    public static &lt;K, V&gt; void printMap( Map&lt;K, V&gt; inputMap )\r\n    {\r\n        \/\/ \u8f93\u51fa Map (Key, Value)\r\n        for ( Map.Entry&lt;K, V&gt; element : inputMap.entrySet() ){\r\n            System.out.println( \"Key: \" + element.getKey() + \"\\tValue: \" + element.getValue() );\r\n        }\r\n        System.out.println();\r\n    }\r\n\r\n    public static void main( String args[] )\r\n    {\r\n        \/\/ \u521b\u5efa\u4e0d\u540c\u7c7b\u578b\u6570\u7ec4\uff1a Integer, Double \u548c Character\r\n        Integer[] intArray = { 1, 2, 3, 4, 5 };\r\n        Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 };\r\n        Character[] charArray = { 'H', 'E', 'L', 'L', 'O' };\r\n\r\n        System.out.println( \"\u6574\u578b\u6570\u7ec4\u5143\u7d20\u4e3a:\" );\r\n        printArray( intArray  ); \/\/ \u4f20\u9012\u4e00\u4e2a\u6574\u578b\u6570\u7ec4\r\n\r\n        System.out.println( \"\\n\u53cc\u7cbe\u5ea6\u578b\u6570\u7ec4\u5143\u7d20\u4e3a:\" );\r\n        printArray( doubleArray ); \/\/ \u4f20\u9012\u4e00\u4e2a\u53cc\u7cbe\u5ea6\u578b\u6570\u7ec4\r\n\r\n        System.out.println( \"\\n\u5b57\u7b26\u578b\u6570\u7ec4\u5143\u7d20\u4e3a:\" );\r\n        printArray( charArray ); \/\/ \u4f20\u9012\u4e00\u4e2a\u5b57\u7b26\u578b\u6570\u7ec4\r\n\r\n        System.out.println();\r\n        \/\/ \u521b\u5efa\u4e0d\u540c\u7c7b\u578bMap\uff1a&lt;String, Integer&gt;, &lt;String, Double&gt; \u548c &lt;String, String&gt;\r\n        Map&lt;String, Integer&gt; strIntMap = new HashMap&lt;&gt;();\r\n        strIntMap.put(\"B\", 55);\r\n        strIntMap.put(\"A\", 80);\r\n        strIntMap.put(\"D\", 20);\r\n        strIntMap.put(\"C\", 70);\r\n        printMap(strIntMap);\r\n\r\n        Map&lt;String, Double&gt; strDoubleMap = new HashMap&lt;&gt;();\r\n        strDoubleMap.put(\"a\", 5.5);\r\n        strDoubleMap.put(\"c\", 8.0);\r\n        strDoubleMap.put(\"m\", 2.0);\r\n        strDoubleMap.put(\"k\", 7.0);\r\n        printMap(strDoubleMap);\r\n    }\r\n}<\/pre>\n<p>\u4e00\u4e2a\u6cdb\u578b\u53cb\u597d\u7684Map\u6392\u5e8f\u65b9\u6cd5\uff1a<\/p>\n<pre class=\"lang:default decode:true \">public class MapUtil {\r\n    \/\/ \u8fd9\u91cc\u7684 V \u5373 map \u7684 value \u7ee7\u627f\u81ea Comparable \uff0c\u8868\u793a\u53ef\u4ee5\u88ab\u6bd4\u8f83\r\n    public static &lt;K, V extends Comparable&lt;? super V&gt;&gt; Map&lt;K, V&gt; sortByValue(Map&lt;K, V&gt; map) {\r\n        List&lt;Entry&lt;K, V&gt;&gt; list = new ArrayList&lt;&gt;(map.entrySet());\r\n        list.sort(Entry.comparingByValue());\r\n\r\n        Map&lt;K, V&gt; result = new LinkedHashMap&lt;&gt;();\r\n        for (Entry&lt;K, V&gt; entry : list) {\r\n            result.put(entry.getKey(), entry.getValue());\r\n        }\r\n\r\n        return result;\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"http:\/\/www.runoob.com\/java\/java-generics.html\">http:\/\/www.runoob.com\/java\/java-generics.html<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/109383\/sort-a-mapkey-value-by-values\">https:\/\/stackoverflow.com\/questions\/109383\/sort-a-mapkey-value-by-values<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u5f88\u65e9\u4e4b\u524d\u6709\u4e86\u89e3\u8fc7\u4ec0\u4e48\u662f\u6cdb\u578b\u7f16\u7a0b\uff0c\u4f46\u4e00\u76f4\u90fd\u6ca1\u6709\u673a\u4f1a\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u5b9e\u8df5\uff0c\u521a\u597d\u6700\u8fd1\u5728\u5b66Java [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[1157,61,1158],"class_list":["post-3984","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-generics","tag-java","tag-1158"],"views":2632,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3984","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=3984"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3984\/revisions"}],"predecessor-version":[{"id":3985,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3984\/revisions\/3985"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=3984"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=3984"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=3984"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}