{"id":7664,"date":"2015-06-30T13:00:13","date_gmt":"2015-06-30T04:00:13","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=7664"},"modified":"2018-11-14T16:33:47","modified_gmt":"2018-11-14T07:33:47","slug":"java-8-lambda-collection-1","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2015\/06\/30\/java-8-lambda-collection-1\/","title":{"rendered":"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"},"content":{"rendered":"

\u3053\u3093\u306b\u3061\u306f\u3001\u68b6\u539f\u3067\u3059\u3002
\n\u300cJava 8 \u306e\u30b3\u30fc\u30c9\u3092\u30e9\u30e0\u30c0\u5f0f\u3092\u306f\u3058\u3081\u3068\u3059\u308b\u65b0\u6a5f\u80fd\u3092\u5229\u7528\u3057\u3066\u30ab\u30c3\u30b3\u3088\u304f\u4e14\u3064\u5b89\u5168\u306b\u66f8\u3051\u308b\u3088\u3046\u306b\u306a\u308a\u305f\u3044\u300d\u3092\u76ee\u6a19\u306b\u52c9\u5f37\u4e2d\u3067\u3059\u3002
\n\u524d\u56de\u306e\u8a18\u4e8b\u300c5\u5206\u3067\u8aad\u3080\u5165\u9580\u7de8\uff1aJava8\u30e9\u30e0\u30c0\u5f0f<\/a>\u300d\u3067\u306f\u30b3\u30fc\u30c9\u306e\u8a18\u6cd5\u306e\u8ee2\u63db\uff08\u300c\u3069\u306e\u3088\u3046\u306b\u5b9f\u73fe\u3059\u308b\u304b\u300d\u304b\u3089\u300c\u4f55\u3092\u5b9f\u73fe\u3055\u305b\u308b\u306e\u304b\u300d\uff09\u306b\u3064\u3044\u3066\u8003\u3048\u307e\u3057\u305f\u3002\u4eca\u56de\u306f\u30e9\u30e0\u30c0\u5f0f\u3092\u5229\u7528\u3057\u305f\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306e\u51e6\u7406\u306b\u3064\u3044\u3066\u8003\u3048\u307e\u3059\u3002<\/p>\n

\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306b\u3064\u3044\u3066\u306f3\u56de\u306b\u5206\u3051\u308b\u4e88\u5b9a\u3067\u3059\u3002<\/p>\n

    \n
  1. \u30ea\u30b9\u30c8\u306e\u5909\u63db<\/li>\n
  2. \u30ea\u30b9\u30c8\u306e\u691c\u7d22<\/li>\n
  3. \u30ea\u30b9\u30c8\u306e\u96c6\u7d04\u30fb\u8a08\u7b97<\/li>\n<\/ol>\n

    1\u3064\u306e\u8a18\u4e8b\u3068\u3057\u3066\u66f8\u3044\u3066\u3057\u307e\u304a\u3046\u3068\u601d\u3063\u305f\u306e\u3067\u3059\u304c\u3001\u3069\u3046\u3057\u3066\u30825\u5206\u4ee5\u5185\u3067\u8aad\u3081\u308b\u91cf\u306b\u307e\u3068\u3081\u3089\u308c\u306a\u304b\u3063\u305f\u306e\u3067\u30fb\u30fb\u30fb\u3002\u4f8b\u984c\u306e\u30b3\u30fc\u30c9\u91cf\u3082\u591a\u304f\u306a\u308a\u305d\u3046\u3067\u3059\u3002\u4eca\u56de\u306f1\u56de\u76ee\u3002\u91cf\u306f\u5c11\u306a\u3081\u306b\u306a\u308a\u307e\u3057\u305f\u3002
    \n\u3082\u3057\u3082\u66f8\u304d\u305f\u3044\u3053\u3068\u304c\u5897\u3048\u305f\u3089\u300c\u756a\u5916\u7de8\u300d\u300c\u7279\u5225\u7de8\u300d\u3068\u304b\u300c\u6700\u7d42\u7ae0\u300d\u3067\u5bfe\u5fdc\u3059\u308b\u3053\u3068\u306b\u306a\u308b\u3068\u601d\u3044\u307e\u3059\u3002\u3053\u3046\u3044\u3046\u7de9\u3044\u611f\u3058\u3067\u66f8\u304d\u307e\u3059\u306e\u3067\u3001\u3088\u308d\u3057\u304f\u304a\u9858\u3044\u3057\u307e\u3059\u3002<\/p>\n

    \u30ea\u30b9\u30c8\u306e\u5909\u63db<\/h2>\n

    \u8981\u7d20\u306e\u6587\u5b57\u5217\u3092\u5927\u6587\u5b57\u30fb\u5c0f\u6587\u5b57\u306b\u5909\u63db\u3057\u3066\u3001\u5143\u306e\u30c7\u30fc\u30bf\u306f\u5909\u66f4\u3057\u306a\u3044\u3067\u65b0\u3057\u3044\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u3092\u4f5c\u6210\u30fb\u51fa\u529b\u3059\u308b\u5834\u5408\u306b\u3064\u3044\u3066\u8003\u3048\u307e\u3059\u3002<\/p>\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\u3053\u306e\u8981\u7d20\u30921\u3064\u305a\u3064\u53d6\u308a\u51fa\u3057\u3066\u5927\u6587\u5b57\u306b\u5909\u63db\u3057\u3001\u6a19\u6e96\u51fa\u529b\u306b\u51fa\u529b\u3059\u308b<\/h4>\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 uppercaseCities = new ArrayList();\r\nfor(String city : cities) {\r\n  uppercaseCities.add(city.toUpperCase());  \/\/ \u5927\u6587\u5b57\u306b\u5909\u63db\r\n}\r\nfor(String city : uppercaseCities) {\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\nOSAKA\r\nKOBE<\/pre>\n

    \u3084\u308a\u305f\u3044\u3053\u3068\u306f\u51fa\u6765\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\u3002for \u30eb\u30fc\u30d7\u6587\u306e\u4ee3\u308f\u308a\u306b\u5185\u90e8\u30a4\u30c6\u30ec\u30fc\u30bf forEach() \u3092\u5229\u7528\u3059\u308b\u3088\u3046\u306b\u66f8\u304d\u63db\u3048\u307e\u3059\u3002<\/p>\n

    final List uppercaseCities = new ArrayList();\r\ncities.forEach(new Consumer() {\r\n  public void accept(final String city) {\r\n    uppercaseCities.add(city.toUpperCase());\r\n  }\r\n});<\/pre>\n

    \uff08\u6a19\u6e96\u51fa\u529b\u51e6\u7406\u306f\u7701\u7565\u3057\u3066\u3044\u307e\u3059\u3002\uff09
    \nJava8 \u306e\u65b0\u6a5f\u80fd\u30fbIterable \u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u306b\u8ffd\u52a0\u3055\u308c\u305f forEach(Consumer action) \u30e1\u30bd\u30c3\u30c9\u306f java.util.function.Consumer \u578b\u3092\u5f15\u6570\u306b\u3068\u308a\u307e\u3059\u3002Consumer \u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306f accept() \u30e1\u30bd\u30c3\u30c9\u306b\u3088\u3063\u3066\u4e0e\u3048\u3089\u308c\u308b\u3082\u306e\u3092\u30b5\u30af\u30b5\u30af\u6d88\u8cbb\u3057\u307e\u3059\u3002<\/p>\n

    \u524d\u56de\u3068\u540c\u3058\u304f\u3001\u30e9\u30e0\u30c0\u5f0f\u3092\u3042\u3066\u306f\u3081\u3066\u95a2\u6570\u5316\u3057\u307e\u3059\u3002<\/p>\n

    final List uppercaseCities = new ArrayList();\r\ncities.forEach(city -> uppercaseCities.add(city.toUpperCase()));\r\n<\/pre>\n

    \uff08\u6a19\u6e96\u51fa\u529b\u51e6\u7406\u306f\u7701\u7565\u3057\u3066\u3044\u307e\u3059\u3002\uff09
    \n\u30d1\u30e9\u30e1\u30fc\u30bf\u30fb\u623b\u308a\u5024\u306e\u578b \uff1cString\uff1e \u306e\u8a18\u8ff0\u3092\u7701\u7565\u3057\u3066\u3044\u308b\u306e\u3067\u3001\u4e00\u6c17\u306b\u30b3\u30fc\u30c9\u91cf\u304c\u6e1b\u308a\u307e\u3057\u305f\u3002
    \n\u305f\u3060\u3057\u3001\u5927\u6587\u5b57\u306b\u5909\u63db\u3055\u308c\u305f\u30ea\u30b9\u30c8\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306e\u5909\u6570\u304c\u5ba3\u8a00\u3055\u308c\u3066\u304a\u308a\u3001\u5909\u6570\u3078\u306e\u8981\u7d20\u306e\u8ffd\u52a0\u51e6\u7406\u3082\u66f8\u304b\u306a\u3044\u3068\u3044\u3051\u307e\u305b\u3093\u3002\u7279\u306b\u5909\u6570\u304c\u5ba3\u8a00\u3055\u308c\u3066\u3044\u308b\u5834\u5408\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\u3044\u308f\u3086\u308b\u300c\u30b3\u30fc\u30c9\u306e\u81ed\u3044\u300d\u3067\u3059\u3002<\/p>\n

    \u305d\u3053\u3067\u3001java.util.stream \u306e Stream \u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u3092\u5229\u7528\u3057\u3066\u307f\u307e\u3059\u3002\u5148\u306e\u30b3\u30fc\u30c9\u4f8b\u3067\u306f\u6a19\u6e96\u51fa\u529b\u51e6\u7406\u306f\u7701\u7565\u3057\u307e\u3057\u305f\u304c\u6b21\u306e\u4f8b\u3067\u306f\u3061\u3083\u3093\u3068\u66f8\u304d\u307e\u3059\u3002\u305d\u308c\u3067\u3082\u3001\u304b\u306a\u308a\u30b3\u30fc\u30c9\u91cf\u304c\u6e1b\u3063\u3066\u3044\u308b\u3053\u3068\u304c\u5206\u304b\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n

    cities.stream().map(city -> city.toUpperCase()).forEach(city -> System.out.println(city));<\/pre>\n

    cities \u304b\u3089\u8981\u7d20\u3092\u53d6\u5f97 \u2192 \u5927\u6587\u5b57\u306b\u5909\u63db\u3057\u305f\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u3092\u4f5c\u6210 \u2192 \u6a19\u6e96\u51fa\u529b\u3059\u308b\u3002\u307e\u308b\u3067\u4ed5\u69d8\u3092\u8aad\u3093\u3060\u305d\u306e\u307e\u307e\u3092\u30b3\u30fc\u30c9\u306b\u66f8\u3044\u305f\u3088\u3046\u3067\u3059\u306d\u3002<\/p>\n

    Stream \u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u3092\u5229\u7528\u3057\u305f\u3053\u3068\u3067\u3001\u9023\u7d9a\u3057\u3066\u95a2\u6570\u3092\u547c\u3073\u51fa\u3059\u69cb\u6210\u306e\u30b3\u30fc\u30c9\u306b\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3057\u305f\u3002 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\u8aad\u3080\u3088\u3046\u306b\u30b3\u30fc\u30c9\u66f8\u304f\u3053\u3068\u304c\u51fa\u6765\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

    map() \u30e1\u30bd\u30c3\u30c9\u306f\u9023\u7d9a\u3057\u305f\u5165\u529b\u3092\u9023\u7d9a\u3057\u305f\u51fa\u529b\u306b\u5909\u63db\u3057\u307e\u3059\u3002\u3053\u3053\u3067\u306f cities \u306e\u5404\u8981\u7d20\u306b\u5927\u6587\u5b57\u5909\u63db\u3059\u308b\u95a2\u6570\u3092\u9069\u7528\u3057\u3001\u65b0\u305f\u306b\u4f5c\u6210\u3055\u308c\u305f Stream \u306b\u96c6\u7d04\u3057\u3066\u3044\u307e\u3059\u3002\u307e\u305f\u3001\u5165\u529b\u5024\u3068\u51fa\u529b\u5024\u306e\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306e\u8981\u7d20\u6570\u304c\u540c\u3058\u3067\u3042\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3059\u308b\u306e\u3067\u3001\u5b89\u5168\u306a\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u4e0a\u3067\u3082\u305c\u3072\u5229\u7528\u3057\u3066\u3044\u304d\u305f\u3044\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002\u306a\u304a\u3001\u5165\u529b\u5024\u3068\u51fa\u529b\u5024\u306e\u578b\u306f\u540c\u3058\u3067\u3042\u308b\u5fc5\u8981\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u5165\u529b\u5024\u304c\u6587\u5b57\u5217 \u2192 \u51fa\u529b\u5024\u304c\u6587\u5b57\u6570\u3001\u3068\u3044\u3046\u4ed5\u69d8\u306b\u3082\u5bfe\u5fdc\u3067\u304d\u307e\u3059\u3002<\/p>\n

    \u7d9a\u304f forEach() \u30e1\u30bd\u30c3\u30c9\u3067\u65b0\u305f\u306b\u4f5c\u6210\u3055\u308c\u305f\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u304b\u3089\u8981\u7d20\u30921\u3064\u305a\u3064\u53d6\u5f97\u3057\u3066\u6a19\u6e96\u51fa\u529b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n

    \u307e\u305f\u307e\u305f\u524d\u56de\u306e\u8a18\u4e8b\u306e\u8a71\u306b\u306a\u308a\u307e\u3059\u304c\u3001\u30e1\u30bd\u30c3\u30c9\u53c2\u7167\u3092\u5229\u7528\u3059\u308b\u3068\u30b3\u30fc\u30c9\u3092\u3055\u3089\u306b\u7701\u7565\u3067\u304d\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u4eca\u56de\u3082\u51e6\u7406\u304c\u975e\u5e38\u306b\u30b7\u30f3\u30d7\u30eb\u306a\u306e\u3067\u3001\u30b3\u30fc\u30c9\u91cf\u3092\u524a\u6e1b\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3059\u3002\u8981\u7d20\u3092\u5c0f\u6587\u5b57\u5316\u3059\u308b\u4f8b\u3068\u3042\u308f\u305b\u3066\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n

    \/\/ \u5927\u6587\u5b57\u5316\r\ncities.stream().map(String::toUpperCase).forEach(System.out::println);\r\n\/\/ \u5c0f\u6587\u5b57\u5316\r\ncities.stream().map(String::toLowerCase).forEach(System.out::println);<\/pre>\n

    \u300c\u30c6\u30b9\u30c8\u306b\u51fa\u308b\u300d\u3068\u8a00\u308f\u308c\u305f\u3089\u305d\u306e\u307e\u307e\u899a\u3048\u3066\u3057\u307e\u3044\u305d\u3046\u306a\u3050\u3089\u3044\u306b\u3001\u30b7\u30f3\u30d7\u30eb\u306a\u30b3\u30fc\u30c9\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n

    \u307e\u3068\u3081<\/h2>\n