{"id":1298,"date":"2022-10-29T22:50:52","date_gmt":"2022-10-29T14:50:52","guid":{"rendered":"https:\/\/qaqaq.top\/?p=1298"},"modified":"2022-12-19T20:17:13","modified_gmt":"2022-12-19T12:17:13","slug":"io%e6%b5%81-%e5%a4%8d%e4%b9%a0","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=1298","title":{"rendered":"IO\u6d41-\u590d\u4e60"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>1. \u5982\u4f55\u904d\u5386 Map \u7684 Key \u96c6\uff0cvalue \u96c6\uff0c key-value \u96c6\uff0c\u4f7f\u7528\u4e0a\u6cdb\u578b\nMap&lt;String,Integer&gt; map = new HashMap&lt;String,Integer&gt;();\nmap.put(); ....\n\/\/\u904d\u5386key\nSet&lt;String&gt; keySet = map.keySet();\nfor(String key : keySet){\n\tSystem.out.println(key);\n}\n\n\/\/\u904d\u5386value\nCollection&lt;Integer&gt; values =  map.values();\nIterator&lt;Integer&gt; iterator = values.iterator():\nwhile(iterator.hasNext()){\n\tSystem.out.println(iterator.next());\n}\n\n\/\/\u904d\u5386key-value\nSet&lt;Map.Entry&lt;String,Integer&gt;&gt; entrySet = map.entrySet();\nIterator&lt;Map.Entry&lt;String,Integer&gt;&gt; iterator = entrySet.iterator();\nwhile(iterator.hasNext()){\n\tMap.Entry&lt;String,Integer&gt; entry = iterator.next();\n\tString key = entry.getkey();\n\tInteger value = entry.getValue();\n\tSystem.out.println(key + \"---&gt;\" + value);\n}\n2. \u5199\u51fa\u4f7f\u7528Iterator \u548c \u589e\u5f3afor\u5faa\u73af\u904d\u5386List&lt;String&gt;\u7684\u4ee3\u7801\uff0c\u4f7f\u7528\u4e0a\u6cdb\u578b\n\n3. \u63d0\u4f9b\u4e00\u4e2a\u65b9\u6cd5\uff0c\u7528\u4e8e\u904d\u5386\u83b7\u53d6HashMap&lt;String,String&gt;\u4e2d\u7684\u6240\u6709value\uff0c\u5e76\u5b58\u653e\u5728List\u4e2d\u8fd4\u56de\u3002\u8003\u8651\u4e0a\u96c6\u5408\u4e2d\u6cdb\u578b\u7684\u4f7f\u7528\u3002\npublic List&lt;String&gt; getValueList(HashMap&lt;String,String&gt; map){\n\tArrayList&lt;String&gt; valueList = new ArrayList&lt;&gt;();\n\tCollcetion&lt;String&gt; values = map.values();\n\tfor(String value \uff1a values){\n\t\tvalueList.add(value);\n\t}\n\treturn valueList;\t\n}\n\n\n4. \u521b\u5efa\u4e00\u4e2a\u4e0ea.txt\u6587\u4ef6\u540c\u76ee\u5f55\u4e0b\u7684\u53e6\u5916\u4e00\u4e2a\u6587\u4ef6b.txt\nFile file1 = new File(\"d:\\\\a\\\\a.txt\");\nFile file2 = new File(file1.getParent(),\"b.txt\");\n\n5. Map\u63a5\u53e3\u4e2d\u7684\u5e38\u7528\u65b9\u6cd5\u6709\u54ea\u4e9b\n\u589e\uff1aput(K k, V v)\n\u5220\uff1aV remove(K k)\n\u6539\uff1aput(K k, V v)\n\u67e5\uff1aV get(K k)\n\u957f\u5ea6\uff1aint size()\n\u904d\u5386\uff1akeySet()\uff0cvalues()\uff0centrySet()\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P581;\n\nimport org.junit.jupiter.api.Test;\n\nimport java.io.File;\nimport java.io.IOException;\nimport java.util.*;\n\n\/**\n * @author RichieZhang\n * @create 2022-10-29 \u4e0b\u5348 4:43\n *\/\npublic class MapTest {\n\n    @Test\n    public void test1(){\n        Map&lt;String,Integer&gt; map = new HashMap&lt;&gt;();\n        map.put(\"AA\",123);\n        map.put(\"BB\",111);\n        map.put(\"CC\",222);\n\n        Set&lt;String&gt; keySet = map.keySet();\n        for (String s : keySet) {\n            System.out.println(s);\n        }\n\n        Collection&lt;Integer&gt; values = map.values();\n        for (Integer value : values) {\n            System.out.println(value);\n        }\n\n        Set&lt;Map.Entry&lt;String, Integer&gt;&gt; entrySet = map.entrySet();\n        Iterator&lt;Map.Entry&lt;String, Integer&gt;&gt; iterator = entrySet.iterator();\n        while (iterator.hasNext()){\n            Map.Entry&lt;String, Integer&gt; entry = iterator.next();\n            String key = entry.getKey();\n            Integer value = entry.getValue();\n            System.out.println(key + \"===\" + value);\n        }\n    }\n\n    @Test\n    public void test2(){\n        List&lt;String&gt; list = new ArrayList&lt;&gt;();\n        list.add(\"AA\");\n        list.add(\"BB\");\n        list.add(\"CC\");\n\n        Iterator&lt;String&gt; iterator = list.iterator();\n        while (iterator.hasNext()){\n            System.out.println(iterator.next());\n\n        }\n    }\n\n    @Test\n    public void test3(){\n        HashMap&lt;String,String&gt; map = new HashMap&lt;&gt;();\n        map.put(\"AA\",\"123\");\n        map.put(\"BB\",\"111\");\n        map.put(\"CC\",\"222\");\n        System.out.println(getValueList(map));\n\n    }\n\n    public List&lt;String&gt; getValueList(HashMap&lt;String,String&gt; map){\n        ArrayList&lt;String&gt; valueList = new ArrayList&lt;&gt;();\n        Collection&lt;String&gt; values = map.values();\n        for (String value : values){\n            valueList.add(value);\n        }\n\n        return valueList;\n\n    }\n\n    @Test\n    public void test4() throws IOException {\n        File file = new File(\"hello.txt\");\n\n        File destFile = new File(file.getParent(),\"he.txt\");\n        boolean newFile = destFile.createNewFile();\n        if (newFile){\n            System.out.println(\"\u521b\u5efa\u6210\u529f\uff01\");\n        }\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[46],"class_list":["post-1298","post","type-post","status-publish","format-standard","hentry","category-java-","tag-java"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1298"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1298"}],"version-history":[{"count":2,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1298\/revisions"}],"predecessor-version":[{"id":2668,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1298\/revisions\/2668"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1298"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1298"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1298"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}