{"id":422,"date":"2014-08-14T14:37:17","date_gmt":"2014-08-14T05:37:17","guid":{"rendered":"http:\/\/www.typea.info\/blog\/index.php\/2014\/08\/14\/java_8_java_7\/"},"modified":"2014-08-14T14:37:17","modified_gmt":"2014-08-14T05:37:17","slug":"java_8_java_7","status":"publish","type":"post","link":"https:\/\/www.typea.info\/blog\/index.php\/2014\/08\/14\/java_8_java_7\/","title":{"rendered":"Java 8 \u306b\u5165\u308b\u524d\u306b \u3044\u307e\u3055\u3089\u306a\u304c\u3089 Java 7 \u3092\u304b\u3051\u8db3\u3067\u304a\u3055\u3089\u3044"},"content":{"rendered":"<p>Java 8 \u304c\u30ea\u30ea\u30fc\u30b9\u3055\u308c\u305f\u76f4\u5f8c\u306e\u55a7\u566a\u3082\u843d\u3061\u7740\u304d\u3064\u3064\u3042\u308a\u307e\u3059\u304c\u3001\u5b9f\u969b\u306e\u3068\u3053\u308d\u4ed5\u4e8b\u3067\u306f\u307e\u3060Java6\u3092\u4f7f\u3063\u3066\u3044\u308b\u305f\u3081\u3001\u3042\u307e\u308a\u8a71\u306b\u3064\u3044\u3066\u884c\u3051\u3066\u3044\u307e\u305b\u3093\u3002<\/p>\n<p>\u304a\u3082\u3057\u308d\u305d\u3046\u3060\u3057\u3001\u305d\u308d\u305d\u308d\u89e6\u3063\u3066\u307f\u3088\u3046\u3002\u3068\u601d\u3063\u305f\u306e\u3067\u3059\u304c\u3001\u30e9\u30e0\u30c0\u5f0f\u3084\u3001Stream API\u3082\u3044\u3044\u3051\u3069\u3001\u305d\u306e\u524d\u306b\u3001Java 7 \u307e\u3068\u3082\u306b\u628a\u63e1\u3057\u3066\u3044\u306a\u3044\u3053\u3068\u306b\u6c17\u3065\u3044\u305f\u3002\u3002\u3002<\/p>\n<p>\u306e\u3067\u3001\u304b\u3051\u8db3\u3067Java7\u306e\u6982\u8981\u3060\u3051\u306a\u3081\u3066\u304a\u3053\u3046\u3002<\/p>\n<p>Java8\u306eSJC-P\u3001\u3058\u3083\u306a\u306a\u3044\u3001<a href=\"http:\/\/education.oracle.com\/pls\/web_prod-plq-dad\/db_pages.getpage?page_id=516&amp;p_org_id=70\">OJCP<\/a> \u304c\u51fa\u305f\u3089\u66f4\u65b0\u3057\u3088\u3046\u3002<\/p>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u53c2\u7167\u3057\u305fKindle\u672c\u3002<\/p>\n<p><iframe style=\"height: 240px; width: 120px\" marginheight=\"0\" src=\"http:\/\/rcm-fe.amazon-adsystem.com\/e\/cm?lt1=_blank&amp;bc1=FFFFFF&amp;IS2=1&amp;bg1=FFFFFF&amp;fc1=000000&amp;lc1=0000FF&amp;t=typea09-22&amp;o=9&amp;p=8&amp;l=as4&amp;m=amazon&amp;f=ifr&amp;ref=ss_til&amp;asins=B005XSS8VC\" frameborder=\"0\" marginwidth=\"0\" scrolling=\"no\"><\/iframe> <\/p>\n<p>\u306a\u3093\u3068\u30010\u5186(\u82f1\u8a9e)\u3002<\/p>\n<p>Java 7 \u3067\u65b0\u3057\u304f\u306a\u3063\u305f\u500b\u6240\u304c\u7aef\u7684\u306b\u307e\u3068\u307e\u3063\u3066\u3044\u308b\u3002 &#8230;\u306f\u305a\u3068\u4fe1\u3058\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u307e\u3041\u3001\u5927\u67a0\u308f\u304b\u308c\u3070\u3088\u3044\u306e\u3067\u3002<\/p>\n<p>\u3067\u306f\u3001\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3092\u66f8\u304d\u306a\u304c\u3089\u78ba\u8a8d\u3002\u7d50\u5c40\u624b\u3092\u52d5\u304b\u3055\u306a\u3044\u3068\u3001\u81ea\u5206\u306e\u3082\u306e\u306b\u306a\u3089\u306a\u3044\u3093\u3060\u3088\u306a\u3002<\/p>\n<h3>\u81ea\u52d5\u30ea\u30bd\u30fc\u30b9\u7ba1\u7406<\/h3>\n<p>\u30ea\u30bd\u30fc\u30b9\u306e\u7ba1\u7406\u304c\u5927\u5206\u697d\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<ul>\n<li>\u30ea\u30bd\u30fc\u30b9\u306eclose \u3092\u81ea\u5206\u3067\u884c\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u305b\u3093\u3002\n<li>; \u3067\u8907\u6570\u306e\u30ea\u30bd\u30fc\u30b9\u3092\u540c\u6642\u306b\u7ba1\u7406\u3067\u304d\u307e\u3059\u3002\n<li>close() \u30e1\u30bd\u30c3\u30c9\u306e\u307f\u3092\u3082\u3064Closeable \u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u306e\u5b50\u3067\u3042\u308b\u3001java.lang.AutoCloseable \u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u3092\u5b9f\u88c5\u3057\u3066\u3044\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/li>\n<\/ul>\n<pre class=\"prettyprint linenums\">import java.io.BufferedReader;\r\nimport java.io.BufferedWriter;\r\nimport java.io.File;\r\nimport java.io.FileNotFoundException;\r\nimport java.io.FileReader;\r\nimport java.io.FileWriter;\r\nimport java.io.IOException;\r\n \r\npublic class AutomaticResourceManagement {\r\n    public static void main(String[] args) {\r\n        \r\n        try (BufferedReader reader \r\n                = new BufferedReader(new FileReader(new File(\"\/home\/piroto\/work\/test.txt\")));\r\n              BufferedWriter writer\r\n                = new BufferedWriter(new FileWriter(new File(\"\/home\/piroto\/work\/out.txt\")))\r\n                ) {\r\n    \r\n            String line = null;\r\n            while((line = reader.readLine()) != null) {\r\n                writer.write(line);\r\n                writer.write(\"\\n\");\r\n            }\r\n            \r\n        } catch (IOException e) {\r\n            \/\/ TODO Auto-generated catch block\r\n            e.printStackTrace();\r\n        }\r\n    }\r\n}<\/pre>\n<h3>\u30c0\u30a4\u30e4\u30e2\u30f3\u30c9\u6f14\u7b97\u5b50<\/h3>\n<p>\u30b8\u30a7\u30cd\u30ea\u30c3\u30af\u30b9\u578b\u306e\u5ba3\u8a00\u306f\u3001\u3044\u307e\u307e\u3067\u3001<\/p>\n<pre class=\"prettyprint linenums\">Map&lt;string ,INTEGER&gt; numMap = new HashMap&lt;string , Integer&gt;();<\/pre>\n<p>\u3053\u3093\u306a\u611f\u3058\u3067\u3057\u305f\u304c\u3001&lt;&gt;(\u30c0\u30a4\u30a2\u30e2\u30f3\u30c9\u6f14\u7b97\u5b50) \u3092\u5229\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u3059\u3079\u3066\u306e\u578b\u30ea\u30b9\u30c8\u3092\u66f8\u304f\u5fc5\u8981\u304c\u306a\u304f\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"prettyprint linenums\">import java.util.HashMap;\r\nimport java.util.Map;\r\n \r\npublic class DiamondOperator {\r\n    public static void main(String[] args) {\r\n        Map&lt;String,Integer&gt; numMap = new HashMap&lt;&gt;();\r\n    }\r\n}<\/pre>\n<h3>\u4f8b\u5916\u51e6\u7406<\/h3>\n<p>| \u6f14\u7b97\u5b50\u3067\u3001\u8907\u6570\u306e\u4f8b\u5916\u3092\u30ad\u30e3\u30c3\u30c1\u3067\u304d\u308b\u69d8\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"prettyprint linenums\">import java.io.FileNotFoundException;\r\nimport java.nio.file.FileAlreadyExistsException;\r\n \r\n \r\npublic class ExceptionHandling {\r\n    public static void main(String[] args) {\r\n \r\n        try {\r\n            doSomethingWithMultiException();\r\n        } catch (FileAlreadyExistsException | FileNotFoundException e) {\r\n            \/\/ TODO Auto-generated catch block\r\n            e.printStackTrace();\r\n        }\r\n    }\r\n    \r\n    private static void doSomethingWithMultiException() \r\n            throws FileNotFoundException, FileAlreadyExistsException {\r\n    }\r\n}<\/pre>\n<h3>\u6570\u5024\u30ea\u30c6\u30e9\u30eb<\/h3>\n<ul>\n<li>\u6570\u5b57\u30ea\u30c6\u30e9\u30eb _ \u3092\u5165\u308c\u3089\u308c\u308b(\u5024\u3068\u3057\u3066\u306f\u7121\u8996\u3055\u308c\u308b)\n<li>0b\u3067\u4e8c\u9032\u30ea\u30c6\u30e9\u30eb\u3092\u8a18\u8ff0\u3067\u304d\u307e\u3059\u3002<\/li>\n<\/ul>\n<pre class=\"prettyprint linenums\">public class NumericLiteralWIthUnderScore {\r\n    \r\n    public static void main(String[] args){\r\n        \r\n        final int million = 1_000_000;\r\n        final int binary = 0b101;\r\n        \r\n        System.out.println(million);\r\n        System.out.println(binary);\r\n    }\r\n}<\/pre>\n<h3>\u6587\u5b57\u5217\u3067switch<\/h3>\n<p>\u6587\u5b57\u5217\u3067switch \u69cb\u6587\u3092\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002case \u30e9\u30d9\u30eb\u3067\u306f\u3001equals() \u3067\u5224\u5b9a\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"prettyprint linenums\">public class StringSwitch {\r\n   public static void main(String[] args) {\r\n       \r\n       String[] status = {\"new\",\"old\",\"New\",\"Old\"};\r\n       int idx = (int)(Math.random()*10d % status.length);\r\n       \r\n       switch (status[idx]) {\r\n       case \"new\":\r\n           System.out.println(\"new:\" + status[idx]);\r\n           break;\r\n       case \"old\":\r\n           System.out.println(\"old:\" + status[idx]);\r\n           break;\r\n       default:\r\n           System.out.println(\"other:\" + status[idx]);\r\n           break;\r\n       }\r\n   }\r\n}<\/pre>\n<h3>Path\u3068\u304b<\/h3>\n<p>*<a title=\"http:\/\/docs.oracle.com\/javase\/jp\/7\/api\/\" href=\"http:\/\/docs.oracle.com\/javase\/jp\/7\/api\/\">http:\/\/docs.oracle.com\/javase\/jp\/7\/api\/<\/a><\/p>\n<p>\u30d5\u30a1\u30a4\u30eb\u30b7\u30b9\u30c6\u30e0\u5185\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u7279\u5b9a\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u53ef\u80fd\u3002java.io.File \u3068\u540c\u7b49\u3002\u8ffd\u52a0\u6a5f\u80fd\u3092\u6301\u3064<\/p>\n<pre class=\"prettyprint linenums\">import java.nio.file.Path;\r\nimport java.nio.file.Paths;\r\n \r\npublic class WorkingWitPath {\r\n \r\n    public static void main(String[] args) {\r\n        \r\n        Path path = Paths.get(\"\/home\");\r\n        Path source = path.resolve(\"piroto\/work\/test.txt\");\r\n        System.out.println(source);\r\n        \r\n        Path dist = source.resolveSibling(\"out.txt\");\r\n        System.out.println(dist);\r\n    }\r\n}<\/pre>\n<h3>\u30d5\u30a1\u30a4\u30eb\u3084\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306e\u5909\u66f4\u3092\u691c\u77e5<\/h3>\n<p>\u4ee5\u4e0b\u306e\u624b\u9806\u3067\u3001\u30d5\u30a1\u30a4\u30eb\u3084\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306e\u5909\u66f4\u3092\u691c\u77e5\u3067\u304d\u308b<\/p>\n<ul>\n<li>WatchService \u306e\u4f5c\u6210\u3002\u3053\u306e\u30b5\u30fc\u30d3\u30b9\u306fWatchKey\u3078\u306e\u30ad\u30e5\u30fc\u304b\u3089\u306a\u308b\n<li>WatchService \u3067\u30e2\u30cb\u30bf\u30fc\u3057\u305f\u3044\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3001\u30d5\u30a1\u30a4\u30eb\u3092\u767b\u9332\n<li>\u30a4\u30d9\u30f3\u30c8\u3092\u30ea\u30c3\u30b9\u30f3\u3059\u308b\u30eb\u30fc\u30d7\u306e\u958b\u59cb\n<li>\u30a4\u30d9\u30f3\u30c8\u304c\u767a\u751f\u3059\u308b\u3068\u3001WatchKey \u304c\u30ad\u30e5\u30fc\u306b\u5165\u308c\u3089\u308c\u308b\n<li>WatchKey\u3092\u6d88\u8cbb\u3057\u3001\u554f\u3044\u5408\u308f\u305b\u3092\u5b9f\u884c<\/li>\n<\/ul>\n<pre class=\"prettyprint linenums\">import java.nio.file.FileSystems;\r\nimport java.nio.file.Path;\r\nimport java.nio.file.Paths;\r\nimport java.nio.file.StandardWatchEventKinds;\r\nimport java.nio.file.WatchEvent;\r\nimport java.nio.file.WatchEvent.Kind;\r\nimport java.nio.file.WatchKey;\r\nimport java.nio.file.WatchService;\r\n \r\npublic class FileChangeNotifications {\r\n \r\n    public static void main(String[] args) {\r\n        try {\r\n            WatchService watchService = FileSystems.getDefault().newWatchService();\r\n            Path watchDir = Paths.get(\"\/home\/piroto\/work\");\r\n            watchDir.register(watchService, \r\n                    StandardWatchEventKinds.ENTRY_CREATE,\r\n                    StandardWatchEventKinds.ENTRY_MODIFY,\r\n                    StandardWatchEventKinds.ENTRY_DELETE);\r\n            while(true) {\r\n                WatchKey watchKey = watchService.take();\r\n                for (WatchEvent&lt;?&gt; event : watchKey.pollEvents()) {\r\n                    Kind&lt;?&gt; kind = event.kind();\r\n                    System.out.println(event.context().toString() + \" is \" + kind);\r\n                }\r\n            }\r\n        \r\n        } catch(Exception e) {\r\n            e.printStackTrace();\r\n        }\r\n    }\r\n}<\/pre>\n<h3>\u4e26\u884c\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0<\/h3>\n<p>\u30de\u30eb\u30c1\u30d7\u30ed\u30bb\u30c3\u30b5\u30fc\u3067\u6709\u5229\u306b\u306a\u308b\u3001\u30d5\u30a9\u30fc\u30af\/\u30b8\u30e7\u30a4\u30f3 \u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u5b9f\u88c5<\/p>\n<p><a href=\"http:\/\/www.amazon.co.jp\/gp\/product\/4797337206\/ref=as_li_ss_il?ie=UTF8&amp;camp=247&amp;creative=7399&amp;creativeASIN=4797337206&amp;linkCode=as2&amp;tag=typea09-22\"><img decoding=\"async\" border=\"0\" src=\"http:\/\/ws-fe.amazon-adsystem.com\/widgets\/q?_encoding=UTF8&amp;ASIN=4797337206&amp;Format=_SL160_&amp;ID=AsinImage&amp;MarketPlace=JP&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=typea09-22\"><\/a><img loading=\"lazy\" decoding=\"async\" style=\"border-top-style: none !important; border-left-style: none !important; border-bottom-style: none !important; border-right-style: none !important; margin: 0px\" border=\"0\" alt=\"\" src=\"http:\/\/ir-jp.amazon-adsystem.com\/e\/ir?t=typea09-22&amp;l=as2&amp;o=9&amp;a=4797337206\" width=\"1\" height=\"1\"> <\/p>\n<pre class=\"prettyprint linenums\">import java.util.concurrent.ForkJoinPool;\r\nimport java.util.concurrent.RecursiveTask;\r\n \r\npublic class ForkAndJoin {\r\n    public static void main(String[] args) {\r\n        try {\r\n            \/\/ n\u756a\u76ee\u306e\u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u3092\u6c42\u3081\u308b\r\n            Fibonacci fnc = new Fibonacci(15);\r\n            ForkJoinPool pool = new ForkJoinPool();\r\n            pool.invoke(fnc);\r\n            \r\n            System.out.println(\"RESULT:\" + fnc.get());\r\n        } catch (Exception e){\r\n            e.printStackTrace();\r\n        }\r\n    }\r\n}\r\n\/**\r\n * @see http:\/\/docs.oracle.com\/javase\/jp\/7\/api\/java\/util\/concurrent\/RecursiveTask.html\r\n *\/\r\nclass Fibonacci extends RecursiveTask<integer> {\r\n    final int n;\r\n \r\n    public Fibonacci(int n) {\r\n        System.out.println(this.toString());\r\n        this.n = n;\r\n    }\r\n \r\n    @Override\r\n    protected Integer compute() {\r\n            if (n &lt;= 1) {\r\n                return n;\r\n            }\r\n            Fibonacci f1 = new Fibonacci(n - 1);\r\n            f1.fork();\r\n            Fibonacci f2 = new Fibonacci(n - 2);\r\n            return f2.compute() + f1.join();\r\n    }\r\n}<\/pre>\n<p>\u5927\u304d\u304f\u306f\u305d\u3093\u306a\u3068\u3053\u308d\u304b\uff1f<\/p>\n<p>\u3042\u3068\u3001\u3053\u306e\u3078\u3093\u304b\u306a\u3002<\/p>\n<p><a title=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/webnotes\/adoptionGuide\/index.html\" href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/webnotes\/adoptionGuide\/index.html\">http:\/\/docs.oracle.com\/javase\/7\/docs\/webnotes\/adoptionGuide\/index.html<\/a><\/p>\n<p>\u305d\u306e\u3046\u3061\u3001\uff2a\uff41\uff56\uff41 8 \u8a66\u3057\u3066\u307f\u3088\u3046\u3002<\/p>\n<p><iframe style=\"height: 240px; width: 120px\" marginheight=\"0\" src=\"http:\/\/rcm-fe.amazon-adsystem.com\/e\/cm?lt1=_blank&amp;bc1=FFFFFF&amp;IS2=1&amp;bg1=FFFFFF&amp;fc1=000000&amp;lc1=0000FF&amp;t=typea09-22&amp;o=9&amp;p=8&amp;l=as4&amp;m=amazon&amp;f=ifr&amp;ref=ss_til&amp;asins=4501552603\" frameborder=\"0\" marginwidth=\"0\" scrolling=\"no\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java 8 \u304c\u30ea\u30ea\u30fc\u30b9\u3055\u308c\u305f\u76f4\u5f8c\u306e\u55a7\u566a\u3082\u843d\u3061\u7740\u304d\u3064\u3064\u3042\u308a\u307e\u3059\u304c\u3001\u5b9f\u969b\u306e\u3068\u3053\u308d\u4ed5\u4e8b\u3067\u306f\u307e\u3060Java6\u3092\u4f7f\u3063\u3066\u3044\u308b\u305f\u3081\u3001\u3042\u307e\u308a\u8a71\u306b\u3064\u3044\u3066\u884c\u3051\u3066\u3044\u307e\u305b\u3093\u3002 \u304a\u3082\u3057\u308d\u305d\u3046\u3060\u3057\u3001\u305d\u308d\u305d\u308d\u89e6\u3063\u3066\u307f\u3088\u3046\u3002\u3068\u601d\u3063\u305f\u306e\u3067\u3059\u304c\u3001\u30e9\u30e0\u30c0\u5f0f\u3084 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"","footnotes":""},"categories":[6],"tags":[],"class_list":["post-422","post","type-post","status-publish","format-standard","hentry","category-java"],"veu_head_title_object":{"title":"","add_site_title":""},"_links":{"self":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/422","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=422"}],"version-history":[{"count":0,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/422\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}