{"id":8150,"date":"2015-12-24T09:00:08","date_gmt":"2015-12-24T00:00:08","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=8150"},"modified":"2018-11-14T16:33:46","modified_gmt":"2018-11-14T07:33:46","slug":"java-8-lambda-collection-2","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2015\/12\/24\/java-8-lambda-collection-2\/","title":{"rendered":"5\u5206\u3067\u8aad\u3080\u5165\u9580\u7de8\uff1aJava 8 \u30e9\u30e0\u30c0\u5f0f \u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u7de8\uff082\uff09\u30ea\u30b9\u30c8\u306e\u691c\u7d22"},"content":{"rendered":"

\u3053\u3093\u306b\u3061\u306f\u3001\u68b6\u539f\u3067\u3059\u3002
\n\u3053\u308c\u306f TECHSCORE Advent Calendar 2015<\/a> \u306e24\u65e5\u76ee\u306e\u8a18\u4e8b\u3067\u3059\u3002<\/p>\n

\u30ea\u30b9\u30c8\u306e\u691c\u7d22<\/h2>\n

Java 8 \u30e9\u30e0\u30c0\u5f0f\u3092\u5229\u7528\u3057\u305f\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306e\u51e6\u7406\u306b\u3064\u3044\u3066\u8003\u3048\u308b\u3001\u7b2c2\u56de\u76ee\u3067\u3059\u3002
\n\u3053\u308c\u307e\u3067\u306e\u8a18\u4e8b\u306f\u4ee5\u4e0b\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n

5\u5206\u3067\u8aad\u3080\u5165\u9580\u7de8\uff1aJava 8 \u30e9\u30e0\u30c0\u5f0f<\/a>
\n
5\u5206\u3067\u8aad\u3080\u5165\u9580\u7de8\uff1aJava 8 \u30e9\u30e0\u30c0\u5f0f \u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u7de8\uff081\uff09\u30ea\u30b9\u30c8\u306e\u5909\u63db<\/a><\/p>\n

\u8981\u7d20\u306e\u691c\u7d22<\/h2>\n

\u7279\u5b9a\u306e\u6587\u5b57\u304b\u3089\u59cb\u307e\u308b\u8981\u7d20\u3092\u62bd\u51fa\u3059\u308b\u5834\u5408\u306b\u3064\u3044\u3066\u8003\u3048\u307e\u3059\u3002
\n\u307e\u305a\u306f\u5730\u540d\u3092\u683c\u7d0d\u3057\u305f\u30ea\u30b9\u30c8\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n

final List cities = Arrays.asList(\"Kyoto\", \"Osaka\", \"Kobe\");<\/pre>\n

\u304a\u984c\u76ee\uff1a\u8981\u7d20\u3092\u691c\u7d22\u3057\u3066\u65b0\u3057\u3044\u914d\u5217\u306b\u66f8\u304d\u51fa\u3059<\/h4>\n

\u300cK\u300d\u304b\u3089\u59cb\u307e\u308b\u5730\u540d\u3092\u691c\u7d22\u3059\u308b\u3053\u3068\u306b\u3057\u307e\u3059\u3002\u691c\u7d22\u7d50\u679c\u306b\u8a72\u5f53\u3059\u308b\u5730\u540d\u306f\u8907\u6570\u5b58\u5728\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u3053\u3068\u3092\u524d\u63d0\u3068\u3057\u307e\u3059\u3002
\n\u307e\u305a\u306f\u5f93\u6765\u306e\u66f8\u304d\u65b9\u3068\u3057\u3066 for \u30eb\u30fc\u30d7\u6587\uff08\u62e1\u5f35 for \u6587\uff09\u3067\u66f8\u3044\u3066\u307f\u305f\u5834\u5408\u3002<\/p>\n

final List startWithK = new ArrayList<>(); \/\/ \u65b0\u3057\u3044\u914d\u5217\u306e\u5909\u6570\u3092\u5ba3\u8a00\u3057\u3066\u751f\u6210\uff01\r\nfor (String city : cities) {                       \/\/ \u30eb\u30fc\u30d7\u3057\u3066\u30ea\u30b9\u30c8\u3092\u8aad\u307f\u8fbc\u307f\u30fb\u30fb\u30fb\r\n  if (city.startsWith(\"K\")) {                      \/\/ \u300cK\u300d\u304b\u3089\u59cb\u307e\u308b\u5834\u5408\u306f\u30fb\u30fb\u30fb\r\n    startWithK.add(city);                          \/\/ \u914d\u5217\u306b\u8ffd\u52a0\uff01\r\n  }\r\n}\r\n\/\/ \u7d50\u679c\u3092\u78ba\u8a8d\r\nfor (String city : startWithK) {\r\n  System.out.println(city);                        \/\/ \u6a19\u6e96\u51fa\u529b\r\n}<\/pre>\n

\u51fa\u529b\u7d50\u679c\u306f\u4ee5\u4e0b\u3067\u3059\u3002<\/p>\n

Kyoto\r\nKobe<\/pre>\n

\u3044\u304f\u3064\u3082\u6bb5\u968e\u3092\u7d4c\u3066\u691c\u7d22\u7d50\u679c\u3092\u53d6\u5f97\u3057\u3066\u307e\u3059\u3002\u3071\u3063\u3068\u898b\u3067\u300c\u3053\u306e\u76ee\u7684\u3092\u9054\u6210\u3059\u308b\u305f\u3081\u306e\u51e6\u7406\u3067\u3059\uff01\u300d\u304c\u30b9\u30c8\u30ec\u30fc\u30c8\u306b\u5fc3\u306b\u523a\u3055\u308b\u30b3\u30fc\u30c9\u3067\u306f\u306a\u3044\u3082\u306e\u306e\u3001\u3084\u308a\u305f\u3044\u3053\u3068\u306f\u3067\u304d\u3066\u3044\u307e\u3059\u306d\u3002
\n\u3067\u306f\u3001\u3053\u3053\u304b\u3089\u5f93\u6765\u306e\u8a18\u6cd5\u3067\u3042\u308b\u547d\u4ee4\u578b\u3067\u3042\u308b\u5916\u90e8\u30a4\u30c6\u30ec\u30fc\u30bf\u304b\u3089\u95a2\u6570\u578b\u306e\u5185\u90e8\u30a4\u30c6\u30ec\u30fc\u30bf\u3078\u5909\u63db\u3055\u305b\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n

\u305d\u306e\u524d\u306b\u3002
\n\u4e0a\u8a18\u306e\u30b3\u30fc\u30c9\u4e2d\u3067\u691c\u7d22\u3092\u5b9f\u65bd\u3057\u3066\u3044\u308b if \u6587\u3001\u3044\u304b\u306b\u3082\u300c\u53e4\u3044\u300d\u3068\u3044\u3046\u5370\u8c61\u304c\u3042\u308a\u307e\u305b\u3093\u304b\uff1f
\njava.util.stream \u306e Stream \u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u3067\u306f\u3001\u691c\u7d22\u3092\u884c\u3046\u76ee\u7684\u3067 filter() \u30e1\u30bd\u30c3\u30c9\u304c\u63d0\u4f9b\u3055\u308c\u3066\u3044\u307e\u3059\u3002
\nfilter() \u30e1\u30bd\u30c3\u30c9\u3092\u5229\u7528\u3059\u308b\u3088\u3046\u306b\u66f8\u304d\u63db\u3048\u3001\u30e9\u30e0\u30c0\u5f0f\u3082\u5f53\u3066\u306f\u3081\u3066\u307f\u307e\u3059\u3002<\/p>\n

final List startWithK = \r\n  cities.stream()\r\n    .filter(city -> city.startsWith(\"K\"))   \/\/ boolean\u578b\u3092\u8fd4\u3059\u30e9\u30e0\u30c0\u5f0f\u3092\u671f\u5f85\r\n    .collect(Collectors.toList());          \/\/ \u7d50\u679c\u3092\u30ea\u30b9\u30c8\u306b\u5909\u63db\r\n<\/pre>\n

cities \u304b\u3089 \u201c\u300cK\u300d\u304b\u3089\u59cb\u307e\u308b\u201d \u6761\u4ef6\u306b\u4e00\u81f4\u3059\u308b\u5730\u540d\u3092\u53d6\u5f97\u3057\u3066\u30ea\u30b9\u30c8\u306b\u5909\u63db\u3057\u3001\u7d50\u679c\u3092\u683c\u7d0d\u3059\u308b\u5909\u6570\u306b\u6e21\u3059\u3002\u307e\u308b\u3067\u4ed5\u69d8\u3092\u305d\u306e\u307e\u307e\u30b3\u30fc\u30c9\u306b\u843d\u3068\u3057\u8fbc\u3093\u3060\u3088\u3046\u3067\u3059\u306d\u3002<\/p>\n

Stream \u306b\u3064\u3044\u3066\u306f\u6a5f\u4f1a\u304c\u3042\u308c\u3070\u307e\u3068\u3081\u308b\u3053\u3068\u306b\u3059\u308b\u306e\u3067\u3001\u3053\u3053\u3067\u306f\u300c\u8981\u7d20\u3092\u4fdd\u5b58\u3057\u306a\u3044\u300d\u300c\u5143\u30c7\u30fc\u30bf\u3092\u5909\u66f4\u3057\u306a\u3044\u300d\u300c\u4ed5\u69d8\u3092\u305d\u306e\u307e\u307e\u30b3\u30fc\u30c9\u306b\u843d\u3068\u3057\u8fbc\u3080\u3053\u3068\u304c\u3067\u304d\u308b\u300d\u4ed5\u7d44\u307f\u3092\u63d0\u4f9b\u3057\u3066\u3044\u308b\u3001\u3068\u3044\u3046\u3050\u3089\u3044\u3067\u3055\u3089\u3063\u3068\u6d41\u3057\u307e\u3059\u3002
\nstream() \u30e1\u30bd\u30c3\u30c9\u306f\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u3092 Stream \u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3067\u30e9\u30c3\u30d4\u30f3\u30b0\u3057\u307e\u3059\u3002<\/p>\n

filter() \u30e1\u30bd\u30c3\u30c9\u306e Javadoc<\/a> \u3092\u78ba\u8a8d\u3059\u308b\u3068\u300c\u3053\u306e\u30b9\u30c8\u30ea\u30fc\u30e0\u306e\u8981\u7d20\u306e\u3046\u3061\u3001\u6307\u5b9a\u3055\u308c\u305f\u8ff0\u8a9e\u306b\u4e00\u81f4\u3059\u308b\u3082\u306e\u304b\u3089\u69cb\u6210\u3055\u308c\u308b\u30b9\u30c8\u30ea\u30fc\u30e0\u3092\u8fd4\u3057\u307e\u3059\u3002\u300d\u3068\u89e3\u8aac\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u623b\u308a\u5024\u304c true \u306b\u306a\u3063\u305f\u5834\u5408\u306e\u307f\u7d50\u679c\u306b\u8ffd\u52a0\u3055\u308c\u307e\u3059\u3002
\n\u3042\u308b\u6761\u4ef6\u306b\u5f93\u3063\u3066\u691c\u7d22\u3092\u884c\u3046\u5834\u5408\u306b\u306f\u3001\u4e00\u81f4\u3059\u308b\u5834\u5408\uff0f\u4e00\u81f4\u3057\u306a\u3044\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u5f53\u7136\u3067\u3059\u3088\u306d\u3002
\n\u4eca\u56de\u306e\u4f8b\u3067\u306f\u3001\u5165\u529b\u5024\u3067\u3042\u308b cities \u3068\u691c\u7d22\u7d50\u679c startWithK \u306e\u8981\u7d20\u6570\u306f\u4e00\u81f4\u3059\u308b\u3068\u306f\u9650\u308a\u307e\u305b\u3093\u3002<\/p>\n

\u4e00\u65b9\u3001 \u524d\u56de<\/a> \u300c\u30ea\u30b9\u30c8\u306e\u5909\u63db\u300d\u306b\u5229\u7528\u3057\u305f map() \u30e1\u30bd\u30c3\u30c9\u306f\u300c\u3053\u306e\u30b9\u30c8\u30ea\u30fc\u30e0\u306e\u8981\u7d20\u306b\u6307\u5b9a\u3055\u308c\u305f\u95a2\u6570\u3092\u9069\u7528\u3057\u305f\u7d50\u679c\u304b\u3089\u69cb\u6210\u3055\u308c\u308b\u30b9\u30c8\u30ea\u30fc\u30e0\u3092\u8fd4\u3057\u307e\u3059\u3002\u300d\u3068\u89e3\u8aac\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u5165\u529b\u5024\u3068\u691c\u7d22\u7d50\u679c\u306e\u8981\u7d20\u6570\u306f\u4e00\u81f4\u3059\u308b\u3053\u3068\u304c\u4fdd\u8a3c\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n

\u305d\u308c\u305e\u308c\u306e\u7279\u5fb4\u3092\u304d\u3063\u3061\u308a\u3068\u628a\u63e1\u3059\u308b\u3053\u3068\u304c\u3001\u5b89\u5168\u306a\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u3053\u3068\u306b\u3082\u3064\u306a\u304c\u308a\u307e\u3059\u3002<\/p>\n

\u8981\u7d20\u3092 1 \u3064\u691c\u7d22<\/h2>\n

\u304a\u984c\u76ee\uff1a\u8981\u7d20\u3092\u691c\u7d22\u3057\u3066\u6700\u521d\u306b\u30d2\u30c3\u30c8\u3057\u305f\u8981\u7d20\u3092\u51fa\u529b\u3057\u3001\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u306f\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u51fa\u529b\u3059\u308b<\/h4>\n

\u691c\u7d22\u7d50\u679c\u306e\u8981\u7d20\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u306b\u5b89\u5168\u306b\u51e6\u7406\u3059\u308b\u65b9\u6cd5\u306b\u3064\u3044\u3066\u8003\u3048\u307e\u3059\u3002
\n\u4eca\u56de\u3082\u300cK\u300d\u304b\u3089\u59cb\u307e\u308b\u5730\u540d\u3092\u5f93\u6765\u306e\u8a18\u6cd5\u3067\u691c\u7d22\u3059\u308b\u3053\u3068\u306b\u3057\u307e\u3059\u3002<\/p>\n

String foundCity = null;\r\nfor (String city : cities) {\r\n  if (city.startsWith(\"K\")) {\r\n    foundCity = city;\r\n    break;\r\n  }\r\n}\r\nif (foundCity != null) {\r\n  System.out.println(foundCity);    \/\/ \u6a19\u6e96\u51fa\u529b\uff1a\u5b58\u5728\u3059\u308b\u5834\u5408\r\n} else {\r\n  System.out.println(\"Not found!\"); \/\/ \u6a19\u6e96\u51fa\u529b\uff1a\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\r\n}<\/pre>\n

\u51fa\u529b\u7d50\u679c\u306f\u4ee5\u4e0b\u3067\u3059\u3002<\/p>\n

Kyoto<\/pre>\n

\u3084\u308a\u305f\u3044\u3053\u3068\u306f\u3067\u304d\u3066\u3044\u307e\u3059\u306d\u3002
\n\u3068\u3053\u308d\u3067\u3001\u300c\u30b3\u30fc\u30c9\u306e\u81ed\u3044\u300d\u306b\u6c17\u304c\u4ed8\u3044\u305f\u3067\u3057\u3087\u3046\u304b\uff1f
\n\u53d6\u5f97\u3057\u305f\u5730\u540d\u3092\u683c\u7d0d\u3057\u3066\u304a\u304f\u305f\u3081\u306b\u5909\u6570\u3092\u5ba3\u8a00\u3057\u3066\u3044\u307e\u3059\u3002\u7d50\u679c\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u3060\u3051\u306e\u5909\u6570\u306e\u5ba3\u8a00\u306f\u3001\u4f8b\u3048\u3070\u3053\u306e\u51e6\u7406\u3092\u4e26\u5217\u5316\u3057\u305f\u3044\u5834\u5408\u306b\u30d0\u30b0\u306e\u539f\u56e0\u306b\u306a\u308a\u305d\u3046\u306a\u5371\u306a\u3044\u611f\u3058\u304c\u3057\u307e\u3059\u3002\u307e\u305f\u3001null \u3067\u521d\u671f\u5316\u3057\u3066\u3044\u308b\u305f\u3081\u300c\u3053\u306e\u5909\u6570\u306f null \u3067\u306f\u306a\u3044\u304b\uff1f\u300d\u3068\u3044\u3046\u78ba\u8a8d\u51e6\u7406\u3082\u5fc5\u8981\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n

\u307e\u305a\u306f\u3001\u691c\u7d22\u3092\u5b9f\u65bd\u3057\u3066\u3044\u308b\u90e8\u5206\u306b\u30e9\u30e0\u30c0\u5f0f\u3092\u5f53\u3066\u306f\u3081\u3066\u66f8\u304d\u63db\u3048\u3066\u307f\u307e\u3059\u3002
\nStream \u3092\u5229\u7528\u3059\u308b\u3068 \u623b\u308a\u5024\u3092\u305d\u306e\u307e\u307e final \u3067\u5ba3\u8a00\u3057\u305f\u5909\u6570\u306b\u6e21\u3059\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002\u5909\u6570\u306e\u53ef\u5909\u6027\u3092\u53d6\u308a\u9664\u304d\u3001\u81ed\u3044\u306e\u539f\u56e0\u3092\u53d6\u308a\u9664\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n

final Optional foundCity = \r\n  cities.stream()\r\n    .filter(city -> city.startsWith(\"K\"))\r\n    .findFirst(); \/\/ \u6700\u521d\u306e\u8981\u7d20\u307e\u305f\u306f\u7a7a<\/pre>\n

cities \u304b\u3089 \u201c\u300cK\u300d\u304b\u3089\u59cb\u307e\u308b\u201d \u6761\u4ef6\u306b\u4e00\u81f4\u3059\u308b\u5730\u540d\u3092\u53d6\u5f97\u3057\u3066\u6700\u521d\u306e 1 \u4ef6\u3092\u7d50\u679c\u3092\u683c\u7d0d\u3059\u308b\u5909\u6570\u306b\u6e21\u3059\u3002\u4ed5\u69d8\u3092\u305d\u306e\u307e\u307e\u30b3\u30fc\u30c9\u306b\u843d\u3068\u3057\u8fbc\u3093\u3060\u3088\u3046\u306b\u66f8\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n

findFirst() \u30e1\u30bd\u30c3\u30c9\u306e Javadoc<\/a> \u3092\u78ba\u8a8d\u3059\u308b\u3068\u300c\u3053\u306e\u30b9\u30c8\u30ea\u30fc\u30e0\u306e\u6700\u521d\u306e\u8981\u7d20\u3092\u8a18\u8ff0\u3059\u308b Optional \u307e\u305f\u306f\u7a7a\u306e Optional (\u30b9\u30c8\u30ea\u30fc\u30e0\u304c\u7a7a\u306e\u5834\u5408)\u3092\u8fd4\u3057\u307e\u3059\u3002\u30b9\u30c8\u30ea\u30fc\u30e0\u304c\u691c\u51fa\u9806\u5e8f\u3092\u6301\u305f\u306a\u3044\u5834\u5408\u306f\u3001\u4efb\u610f\u306e\u8981\u7d20\u304c\u8fd4\u3055\u308c\u307e\u3059\u3002\u300d\u3068\u89e3\u8aac\u3055\u308c\u3066\u3044\u307e\u3059\u3002
\n\u623b\u308a\u5024\u3067\u3042\u308b\u7279\u5225\u306a\u30af\u30e9\u30b9 Optional \u306f Java8 \u306e\u65b0\u6a5f\u80fd\u3067\u3059\u3002\u30b3\u30fc\u30c9\u306e\u8aad\u307f\u624b\u306b\u300c\u7d50\u679c\u5024\u304c\u8fd4\u3055\u308c\u306a\u3044\u5834\u5408\u304c\u3042\u308b\u300d\u3053\u3068\u3092\u660e\u78ba\u306b\u4f1d\u3048\u307e\u3059\u3002
\norElse() \u3084 ifPresent() \u3068\u3044\u3063\u305f\u30e1\u30bd\u30c3\u30c9\u3067\u7d50\u679c\u5024\u306e\u6709\u7121\u306b\u5fdc\u3058\u3066\u51e6\u7406\u3092\u5909\u66f4\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n

\u7d50\u679c\u5024\u306e\u6709\u7121\u306b\u5fdc\u3058\u305f\u6a19\u6e96\u51fa\u529b\u3092\u5b9f\u65bd\u3059\u308b\u90e8\u5206\u3092\u66f8\u304d\u63db\u3048\u3066\u307f\u307e\u3059\u3002<\/p>\n

System.out.println(foundCity.orElse(\"Not found!\"));<\/pre>\n

\u53e4\u3044\u8a18\u6cd5\u3067\u306f\u300c\u3053\u306e\u5909\u6570\u306f null \u3067\u306f\u306a\u3044\u304b\uff1f\u300d\u3068\u3044\u3046\u78ba\u8a8d\u51e6\u7406\u304b\u3089\u59cb\u307e\u3063\u3066\u3044\u305f\u90e8\u5206\u304c\u3001\u305f\u3063\u305f 1 \u884c\u3067\u66f8\u3051\u3066\u3044\u307e\u3059\u3002
\norElse() \u30e1\u30bd\u30c3\u30c9\u306f\u3001\u5024\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u306b\u4f55\u3092\u8fd4\u3059\u306e\u304b\u3092\u63d0\u6848\u3057\u3066\u3044\u307e\u3059\u3002
\n\u51fa\u529b\u7d50\u679c\u306f\u300cKyoto\u300d\u3067\u3059\u3002\u3082\u3057\u3082\u300cK\u300d\u304b\u3089\u59cb\u307e\u308b\u5730\u540d\u304c\u5b58\u5728\u3057\u306a\u304b\u3063\u305f\u5834\u5408\u306b\u306f\u300cNot found!\u300d\u304c\u51fa\u529b\u3055\u308c\u307e\u3059\u3002<\/p>\n

\u4eca\u56de\u306e\u304a\u984c\u76ee\u3067\u306f\u300c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u306f\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u51fa\u529b\u3059\u308b\u300d\u304c\u6c42\u3081\u3089\u308c\u3066\u3044\u308b\u306e\u3067 orElse() \u30e1\u30bd\u30c3\u30c9\u3092\u5229\u7528\u3057\u307e\u3057\u305f\u304c\u3001\u300c\u5b58\u5728\u3059\u308b\u5834\u5408\u3060\u3051\u6a19\u6e96\u51fa\u529b\u3059\u308b\u300d\u304c\u6c42\u3081\u3089\u308c\u3066\u3044\u305f\u306e\u3067\u3042\u308c\u3070 ifPresent() \u30e1\u30bd\u30c3\u30c9\u304c\u5229\u7528\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n

foundCity.ifPresent(System.out::println);<\/pre>\n

\u6a19\u6e96\u51fa\u529b\u51e6\u7406\u306f\u30e1\u30bd\u30c3\u30c9\u53c2\u7167\u3067\u8a18\u8f09\u3057\u3066\u307f\u307e\u3057\u305f\u3002
\n\u51fa\u529b\u7d50\u679c\u306f\u300cKyoto\u300d\u3067\u3059\u3002\u3082\u3057\u3082\u300cK\u300d\u304b\u3089\u59cb\u307e\u308b\u5730\u540d\u304c\u5b58\u5728\u3057\u306a\u304b\u3063\u305f\u5834\u5408\u306b\u306f\u6a19\u6e96\u51fa\u529b\u51e6\u7406\u306f\u5b9f\u884c\u3055\u308c\u307e\u305b\u3093\u3002<\/p>\n

\u5b89\u5168\u306a\u4fdd\u5b88\u3092\u53ef\u80fd\u306b\u3059\u308b\u305f\u3081\u306e\u5de5\u592b<\/h2>\n

\u300c\u30ea\u30b9\u30c8\u306e\u691c\u7d22\u3092\u8003\u3048\u308b\u300d\u3068\u3044\u3046\u5185\u5bb9\u304b\u3089\u306f\u5916\u308c\u307e\u3059\u304c\u3001\u5b9f\u969b\u306b\u304a\u5ba2\u69d8\u306b\u7d0d\u54c1\u3059\u308b\u5834\u5408\u306b\u4eca\u56de\u306e\u3088\u3046\u306a\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u3060\u308d\u3046\u304b\uff1f\u3068\u8003\u3048\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n