{"id":2897,"date":"2021-05-10T23:34:32","date_gmt":"2021-05-10T14:34:32","guid":{"rendered":"https:\/\/loglog.xyz\/?p=2897"},"modified":"2025-05-22T19:54:34","modified_gmt":"2025-05-22T10:54:34","slug":"map_stream","status":"publish","type":"post","link":"https:\/\/loglog.xyz\/programming\/map_stream","title":{"rendered":"\u3010Java\u3011Map\u3092Stream\u3067\u51e6\u7406\u3059\u308b\u65b9\u6cd5\u307e\u3068\u3081\uff5c\u30d5\u30a3\u30eb\u30bf\u30fc\u30fb\u30bd\u30fc\u30c8\u30fb\u5909\u63db\u306e\u30b3\u30fc\u30c9\u4f8b\u4ed8\u304d"},"content":{"rendered":"<h2>\u6982\u8981<\/h2>\n<p><a href=\"https:\/\/loglog.xyz\/programming\/stream_list\" title=\"\u3010Java\u3011List\u3092Stream\u3067\u51e6\u7406\u3059\u308b\u65b9\u6cd5 7\u9078\uff01\" rel=\"noopener\" target=\"_blank\">\u3010Java\u3011List\u3092Stream\u3067\u51e6\u7406\u3059\u308b\u65b9\u6cd5 7\u9078\uff01<\/a><br \/>\n<a href=\"https:\/\/loglog.xyz\/programming\/stream_set\" title=\"\u3010Java\u3011Set\u3092Stream\u3067\u51e6\u7406\u3059\u308b\u65b9\u6cd5 7\u9078\uff01\" rel=\"noopener\" target=\"_blank\">\u3010Java\u3011Set\u3092Stream\u3067\u51e6\u7406\u3059\u308b\u65b9\u6cd5 7\u9078\uff01<\/a><br \/>\n\u306b\u7d9a\u304d\u3001Map\u7de8\u3067\u3059\u3002<\/p>\n<p>Map\u3092stream\u3067\u6271\u3046\u5834\u5408\u3001entrySet\u307e\u305f\u306fKeySet\u3067Set\u30d3\u30e5\u30fc\u3092\u8fd4\u3057\u3066\u304bstream\u3092\u547c\u3076\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u307e\u305f\u3001HashSet\u3068\u540c\u69d8\u306bHashMap\u306f\u9806\u5e8f\u304c\u4fdd\u8a3c\u3055\u308c\u3066\u3044\u306a\u3044\u305f\u3081\u3001\u6700\u521d\u304b\u3089\u9806\u5e8f\u3092\u4fdd\u8a3c\u3057\u3066\u304a\u304d\u305f\u3044\u5834\u5408\u306fLinkedHashMap\u3092\u4f7f\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u5b9f\u88c5\u4f8b\u306f\u3001Set\u306e\u6642\u3068\u540c\u69d8\u306b\u3088\u304f\u4f7f\u3046\u3067\u3042\u308d\u3046HashMap\u3067\u89e3\u8aac\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<h2>\u5b9f\u88c5\u4f8b<\/h2>\n<h3>\u7279\u5b9a\u306e\u8981\u7d20\u306e\u307f\u3092\u53d6\u5f97\u3059\u308b<\/h3>\n<p>\u4e2d\u9593\u51e6\u7406\uff1a\u30ab\u30c6\u30b4\u30ea\u30fc\u304c&#8221;\u73a9\u5177&#8221;\u3060\u3051\u306eProduct\u306b\u7d5e\u308a\u8fbc\u307f\u307e\u3059\u3002<br \/>\n\u7d42\u7aef\u51e6\u7406\uff1aHashMap\u306b\u5909\u63db\u3057\u3066\u7d50\u679c\u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:java decode:true \">@Test\nvoid Map_\u7279\u5b9a\u306e\u8981\u7d20\u306e\u307f\u3092\u53d6\u5f97\u3059\u308b() {\n\tMap&lt;Integer, Product&gt; products = new HashMap&lt;&gt;() {\n\t\t{\n\t\t\tput(1, new Product(\"\u8863\u985e\", \"\u9774\u4e0b\", 200));\n\t\t\tput(2, new Product(\"\u98df\u54c1\", \"\u3075\u308a\u304b\u3051\", 100));\n\t\t\tput(3, new Product(\"\u73a9\u5177\", \"\u30d7\u30e9\u30e2\u30c7\u30eb\", 1000));\n\t\t}\n\t};\n\tHashMap&lt;Integer, Product&gt; toys = products.entrySet().stream()\n\t\t.filter(product -&gt; \"\u73a9\u5177\".equals(product.getValue().category()))\n\t\t.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,\n                (e1, e2) -&gt; e1, HashMap::new));\n\tassertThat(toys.get(3).name(), is(\"\u30d7\u30e9\u30e2\u30c7\u30eb\"));\n}<\/pre>\n<h3>\u5408\u8a08\u3092\u7b97\u51fa\u3059\u308b<\/h3>\n<p>\u4e2d\u9593\u51e6\u7406\uff1aIntStream\u306b\u5909\u63db\u3057\u307e\u3059\u3002<br \/>\n\u7d42\u7aef\u51e6\u7406\uff1a\u5408\u8a08\u3057\u305f\u7d50\u679c\u3092\u8fd4\u3057\u307e\u3059<\/p>\n<pre class=\"lang:java decode:true \">@Test\nvoid Map_\u5408\u8a08\u3092\u7b97\u51fa\u3059\u308b() {\n\tMap&lt;Integer, Product&gt; products = new HashMap&lt;&gt;() {\n\t\t{\n\t\t\tput(1, new Product(\"\u8863\u985e\", \"\u9774\u4e0b\", 200));\n\t\t\tput(2, new Product(\"\u98df\u54c1\", \"\u3075\u308a\u304b\u3051\", 100));\n\t\t\tput(3, new Product(\"\u73a9\u5177\", \"\u30d7\u30e9\u30e2\u30c7\u30eb\", 1000));\n\t\t}\n\t};\n\tint sumAmount = products.entrySet().stream()\n\t\t.mapToInt(product -&gt; product.getValue().price())\n\t\t.sum();\n\tassertThat(sumAmount, is(1300));\n}<\/pre>\n<h3>\u4e26\u3079\u66ff\u3048\u305f\u7d50\u679c\u3092\u53d6\u5f97\u3059\u308b(\u6607\u9806)<\/h3>\n<p>\u4e2d\u9593\u51e6\u7406\uff1a\u91d1\u984d\u306e\u6607\u9806\u3067\u4e26\u3073\u66ff\u3048\u308b\u3002\uff08\u30c6\u30b9\u30c8\u7528\u306b\u6700\u521d\u306e1\u4ef6\u3060\u3051\u53d6\u5f97\uff09<br \/>\n\u7d42\u7aef\u51e6\u7406\uff1aLinkedHashMap\u306b\u5909\u63db\u3057\u3066\u7d50\u679c\u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:java decode:true \">@Test\nvoid Map_\u4e26\u3079\u66ff\u3048\u305f\u7d50\u679c\u3092\u53d6\u5f97\u3059\u308b_\u6607\u9806() {\n\tMap&lt;Integer, Product&gt; products = new HashMap&lt;&gt;() {\n\t\t{\n\t\t\tput(1, new Product(\"\u8863\u985e\", \"\u9774\u4e0b\", 200));\n\t\t\tput(2, new Product(\"\u98df\u54c1\", \"\u3075\u308a\u304b\u3051\", 100));\n\t\t\tput(3, new Product(\"\u73a9\u5177\", \"\u30d7\u30e9\u30e2\u30c7\u30eb\", 1000));\n\t\t}\n\t};\n\t\/\/ \u30c6\u30b9\u30c8\u306e\u305f\u3081\u306b\u3001\u4e26\u3073\u5909\u3048\u305f\u7d50\u679c\u306e\u6700\u521d\u306e1\u8981\u7d20\u3092limit\u3067\u53d6\u5f97\n\tLinkedHashMap&lt;Integer, Product&gt; ascOrderOfPrice = products.entrySet().stream()\n\t\t\t.sorted(Map.Entry.comparingByValue(\n\t                Comparator.comparing(Product::price)))\n\t\t\t.limit(1)\n\t        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,\n\t                (e1, e2) -&gt; e1, LinkedHashMap::new));\n\t\/\/ \u6700\u3082\u91d1\u984d\u306e\u5c0f\u3055\u3044\u300c\"\u98df\u54c1\", \"\u3075\u308a\u304b\u3051\u300d\u304c\u53d6\u5f97\u3067\u304d\u3066\u3044\u308b\n\tassertThat(ascOrderOfPrice.containsKey(2), is(true));\n}<\/pre>\n<h3>\u4e26\u3079\u66ff\u3048\u305f\u7d50\u679c\u3092\u53d6\u5f97\u3059\u308b(\u964d\u9806)<\/h3>\n<p>\u4e2d\u9593\u51e6\u7406\uff1a\u91d1\u984d\u306e\u964d\u9806\u3067\u4e26\u3073\u66ff\u3048\u308b\u3002\uff08\u30c6\u30b9\u30c8\u7528\u306b\u6700\u521d\u306e1\u4ef6\u3060\u3051\u53d6\u5f97\uff09<br \/>\n\u7d42\u7aef\u51e6\u7406\uff1aLinkedHashMap\u306b\u5909\u63db\u3057\u3066\u7d50\u679c\u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:java decode:true \">@Test\nvoid Map_\u4e26\u3079\u66ff\u3048\u305f\u7d50\u679c\u3092\u53d6\u5f97\u3059\u308b_\u964d\u9806() {\n\tMap&lt;Integer, Product&gt; products = new HashMap&lt;&gt;() {\n\t\t{\n\t\t\tput(1, new Product(\"\u8863\u985e\", \"\u9774\u4e0b\", 200));\n\t\t\tput(2, new Product(\"\u98df\u54c1\", \"\u3075\u308a\u304b\u3051\", 100));\n\t\t\tput(3, new Product(\"\u73a9\u5177\", \"\u30d7\u30e9\u30e2\u30c7\u30eb\", 1000));\n\t\t}\n\t};\n\t\/\/ \u30c6\u30b9\u30c8\u306e\u305f\u3081\u306b\u3001\u4e26\u3073\u5909\u3048\u305f\u7d50\u679c\u306e\u6700\u521d\u306e1\u8981\u7d20\u3092\uff4c\uff49\uff4d\uff49\uff54\u3067\u53d6\u5f97\n\tLinkedHashMap&lt;Integer, Product&gt; descOrderOfPrice = products.entrySet().stream()\n\t\t\t.sorted(Map.Entry.comparingByValue(\n\t                Comparator.comparing(Product::price).reversed()))\n\t\t\t.limit(1)\n\t        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,\n\t                (e1, e2) -&gt; e1, LinkedHashMap::new));\n\t\/\/ \u6700\u3082\u91d1\u984d\u306e\u5927\u304d\u3044\u300c\"\u73a9\u5177\", \"\u30d7\u30e9\u30e2\u30c7\u30eb\"\u300d\u304c\u53d6\u5f97\u3067\u304d\u3066\u3044\u308b\n\tassertThat(descOrderOfPrice.containsKey(3), is(true));\n}<\/pre>\n<h3>\u6700\u521d\u306e\u8981\u7d20\u3092\u53d6\u5f97\u3059\u308b<\/h3>\n<p>\u4e2d\u9593\u51e6\u7406\uff1aKey\u306e\u6607\u9806\u3067\u4e26\u3073\u66ff\u3048\u308b\u3002<br \/>\n\u7d42\u7aef\u51e6\u7406\uff1a\u6700\u521d\u306e1\u4ef6\u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:java decode:true \">@Test\nvoid Map_\u6700\u521d\u306e\u8981\u7d20\u3092\u53d6\u5f97\u3059\u308b() {\n\tMap&lt;Integer, Product&gt; products = new HashMap&lt;&gt;() {\n\t\t{\n\t\t\tput(1, new Product(\"\u8863\u985e\", \"\u9774\u4e0b\", 200));\n\t\t\tput(2, new Product(\"\u98df\u54c1\", \"\u3075\u308a\u304b\u3051\", 100));\n\t\t\tput(3, new Product(\"\u73a9\u5177\", \"\u30d7\u30e9\u30e2\u30c7\u30eb\", 1000));\n\t\t}\n\t};\n\tEntry&lt;Integer, Product&gt; firstOfAscPrice = products.entrySet().stream()\n\t\t\t.sorted(Map.Entry.&lt;Integer, Product&gt;comparingByKey())\n\t\t\t.findFirst()\n\t\t\t.orElse(null);\n\tassertThat(firstOfAscPrice.getValue().name(), is(\"\u9774\u4e0b\"));\n}\n<\/pre>\n<h3>\u6700\u5f8c\u306e\u8981\u7d20\u3092\u53d6\u5f97\u3059\u308b<\/h3>\n<p>\u4e2d\u9593\u51e6\u7406\uff1aKey\u306e\u964d\u9806\u3067\u4e26\u3073\u66ff\u3048\u308b\u3002<br \/>\n\u7d42\u7aef\u51e6\u7406\uff1a\u6700\u521d\u306e1\u4ef6\u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:java decode:true \">@Test\nvoid Map_\u6700\u5f8c\u306e\u8981\u7d20\u3092\u53d6\u5f97\u3059\u308b() {\n\tMap&lt;Integer, Product&gt; products = new HashMap&lt;&gt;() {\n\t\t{\n\t\t\tput(1, new Product(\"\u8863\u985e\", \"\u9774\u4e0b\", 200));\n\t\t\tput(2, new Product(\"\u98df\u54c1\", \"\u3075\u308a\u304b\u3051\", 100));\n\t\t\tput(3, new Product(\"\u73a9\u5177\", \"\u30d7\u30e9\u30e2\u30c7\u30eb\", 1000));\n\t\t}\n\t};\n\tEntry&lt;Integer, Product&gt; firstOfDescPrice = products.entrySet().stream()\n\t\t\t.sorted(Map.Entry.&lt;Integer, Product&gt;comparingByKey().reversed())\n\t\t\t.findFirst()\n\t\t\t.orElse(null);\n\tassertThat(firstOfDescPrice.getValue().name(), is(\"\u30d7\u30e9\u30e2\u30c7\u30eb\"));\n}<\/pre>\n<h3>\u4efb\u610f\u306e\u5024\u306b\u5909\u63db\u3057\u305f\u5024\u3092\u53d6\u5f97\u3059\u308b<\/h3>\n<p>\u4e2d\u9593\u51e6\u7406\uff1aFormatter\u3067\u5909\u63db\u3057\u305f\u6587\u5b57\u5217\u3092\u751f\u6210\u3059\u308b\u3002<br \/>\n\u7d42\u7aef\u51e6\u7406\uff1aList\u306b\u5909\u63db\u3057\u305f\u7d50\u679c\u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:java decode:true\">@Test\nvoid Map_\u4efb\u610f\u306e\u5024\u306b\u5909\u63db\u3057\u305f\u5024\u3092\u53d6\u5f97\u3059\u308b() {\n\tMap&lt;Integer, Product&gt; products = new HashMap&lt;&gt;() {\n\t\t{\n\t\t\tput(1, new Product(\"\u8863\u985e\", \"\u9774\u4e0b\", 200));\n\t\t\tput(2, new Product(\"\u98df\u54c1\", \"\u3075\u308a\u304b\u3051\", 100));\n\t\t\tput(3, new Product(\"\u73a9\u5177\", \"\u30d7\u30e9\u30e2\u30c7\u30eb\", 1000));\n\t\t}\n\t};\n\tList&lt;String&gt; formatedList = products.entrySet().stream()\n\t\t\t.map(entry -&gt; new Formatter().format(\"[key: %d, value: %s, %s, %d]\",\n\t\t\t\t\tentry.getKey(), entry.getValue().category(), entry.getValue().name(), entry.getValue().price())\n\t\t\t\t\t.toString())\n\t\t\t.collect(Collectors.toList());\n\n\t\/\/ HashMap\u306f\u9806\u5e8f\u304c\u4fdd\u8a3c\u3055\u308c\u3066\u3044\u306a\u3044\u306e\u3067\u3001\u73a9\u5177\u3092\u542b\u3093\u3060\u6587\u5b57\u5217\u306e\u307f\u3092\u62bd\u51fa\u3057\u3066\u30c6\u30b9\u30c8\u3059\u308b\n\tassertThat(formatedList.stream()\n\t\t\t\t.filter(str -&gt; str.toString().contains(\"\u73a9\u5177\"))\n\t\t\t\t.findFirst()\n\t\t\t\t.orElse(null), is(\"[key: 3, value: \u73a9\u5177, \u30d7\u30e9\u30e2\u30c7\u30eb, 1000]\"));\n}<\/pre>\n<h2>\u307e\u3068\u3081<\/h2>\n<ul>\n<li>Map\u306fentrySet\u307e\u305f\u306fkeySet\u3067Set\u30d3\u30e5\u30fc\u3092\u8fd4\u3057\u3066\u304b\u3089Stream\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3059\u3002<\/li>\n<li>HashMap\u3082HashSet\u3068\u540c\u69d8\u306b\u9806\u5e8f\u304c\u4fdd\u8a3c\u3055\u308c\u3066\u3044\u306a\u3044\u306e\u3067\u3001\u6700\u521d\u304b\u3089\u9806\u5e8f\u3092\u4fdd\u8a3c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u306fLinkedHashMap\u3092\u4f7f\u7528\u3059\u308b\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Java\u306eMap\u306b\u5bfe\u3057\u3066Stream API\u3092\u4f7f\u3046\u65b9\u6cd5\u3092\u308f\u304b\u308a\u3084\u3059\u304f\u89e3\u8aac\u3002entrySet\u304b\u3089\u306e\u51e6\u7406\u3001\u30ad\u30fc\u3084\u5024\u306e\u62bd\u51fa\u3001\u30d5\u30a3\u30eb\u30bf\u30ea\u30f3\u30b0\u3001\u4e26\u3073\u66ff\u3048\u3001\u5909\u63db\u306a\u3069\u3001\u5b9f\u52d9\u3067\u4f7f\u3048\u308b\u4f8b\u3092\u591a\u6570\u7d39\u4ecb\uff01<\/p>\n","protected":false},"author":1,"featured_media":2284,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"swell_btn_cv_data":"","footnotes":""},"categories":[2,65],"tags":[54,142,172],"class_list":["post-2897","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming","category-java","tag-java","tag-stream","tag-hashmap"],"_links":{"self":[{"href":"https:\/\/loglog.xyz\/wp-json\/wp\/v2\/posts\/2897","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/loglog.xyz\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/loglog.xyz\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/loglog.xyz\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/loglog.xyz\/wp-json\/wp\/v2\/comments?post=2897"}],"version-history":[{"count":11,"href":"https:\/\/loglog.xyz\/wp-json\/wp\/v2\/posts\/2897\/revisions"}],"predecessor-version":[{"id":28388,"href":"https:\/\/loglog.xyz\/wp-json\/wp\/v2\/posts\/2897\/revisions\/28388"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/loglog.xyz\/wp-json\/wp\/v2\/media\/2284"}],"wp:attachment":[{"href":"https:\/\/loglog.xyz\/wp-json\/wp\/v2\/media?parent=2897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/loglog.xyz\/wp-json\/wp\/v2\/categories?post=2897"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/loglog.xyz\/wp-json\/wp\/v2\/tags?post=2897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}