{"id":486,"date":"2019-03-06T10:52:21","date_gmt":"2019-03-06T02:52:21","guid":{"rendered":"https:\/\/fick707.com\/?p=486"},"modified":"2019-03-06T10:52:21","modified_gmt":"2019-03-06T02:52:21","slug":"java-new-features-891011","status":"publish","type":"post","link":"https:\/\/fick707.com\/?p=486","title":{"rendered":"java new features 891011"},"content":{"rendered":"<h1 id=\"toc_0\">Java\u65b0\u7279\u6027<\/h1>\n<h2 id=\"toc_1\">java 8<\/h2>\n<h3 id=\"toc_2\">lambda\u8868\u8fbe\u5f0f<\/h3>\n<p>Lambda\u8868\u8fbe\u5f0f\u53ef\u4ee5\u4ee3\u66ff\u533f\u540d\u7c7b\uff0c\u5b83\u5c06\u529f\u80fd\uff08\u51fd\u6570\uff09\u5f53\u6210\u65b9\u6cd5\u7684\u53c2\u6570\uff0c\u6216\u8005\u5c06\u4ee3\u7801\u5f53\u6210\u6570\u636e\uff1b\u5b9e\u73b0\u9ad8\u6548\u4e14\u4ee3\u7801\u7d27\u51d1\uff1b<br \/>\njava.util.function\u5305\u4e2d\u5b9a\u4e49\u4e86\u597d\u591a\u53ea\u5305\u542b\u4e00\u4e2a\u65b9\u6cd5\u7684\u7b80\u5355\u51fd\u6570\u63a5\u53e3\uff1b<br \/>\nLambda\u8868\u8fbe\u5f0f\u914d\u5408stream\u65b9\u4fbf\u9ad8\u6548\u7d27\u51d1\uff1b<br \/>\nLambda \u8868\u8fbe\u5f0f\u805a\u5408\u64cd\u4f5c(Aggregate Operations)\u529f\u80fd\u5f3a\u5927\uff1b<br \/>\nLambda\u53ef\u4ee5\u88ab\u5e8f\u5217\u5316\uff0c\u53ea\u8981\u5176\u76ee\u6807\u7c7b\u578b\u548c\u53c2\u6570\u53ef\u4ee5\u88ab\u5e8f\u5217\u5316\uff0c\u53ea\u662f\u4e0d\u5efa\u8bae\u5e8f\u5217\u5316\u3002\u56e0\u4e3a\u6b63\u5982\u5185\u90e8\u7c7b\u5e8f\u5217\u5316\uff0c\u53ef\u80fd\u4f1a\u6f5c\u5728\u4e00\u4e9b\u95ee\u9898\uff0c\u5199\u4ee3\u7801\u8981\u6ce8\u610f\u5f88\u591a\uff1b<\/p>\n<div>\n<pre class=\" language-none\"><code class=\" language-none\">Arrays.sort(rosterAsArray, Person::compareByAge);<\/code><\/pre>\n<\/div>\n<h3 id=\"toc_3\">\u65b9\u6cd5\u5f15\u7528<\/h3>\n<p>\u65b9\u6cd5\u5f15\u7528\u652f\u6301\u4ee5\u4e0b\u65b9\u5f0f\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u7c7b\u578b<\/th>\n<th>\u793a\u4f8b<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Reference to a static method<\/td>\n<td>ContainingClass::staticMethodName<\/td>\n<\/tr>\n<tr>\n<td>Reference to an instance method of a particular object<\/td>\n<td>containingObject::instanceMethodName<\/td>\n<\/tr>\n<tr>\n<td>Reference to an instance method of an arbitrary object of a particular type<\/td>\n<td>ContainingType::methodName<\/td>\n<\/tr>\n<tr>\n<td>Reference to a constructor<\/td>\n<td>ClassName::new<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3 id=\"toc_4\">Default Methods<\/h3>\n<p>\u9ed8\u8ba4\u65b9\u6cd5\u3002 \u8bd5\u60f3\uff0c\u6709\u4e00\u4e2a\u65e7\u63a5\u53e3\uff0c\u9700\u8981\u6dfb\u52a0\u4e00\u4e2a\u65b9\u6cd5\u3002\u90a3\u53ef\u9ebb\u70e6\u4e86\uff0c\u56e0\u4e3a\u9664\u4e86\u4fee\u6539\u63a5\u53e3\u672c\u8eab\uff0c\u5b83\u6240\u6709\u7684\u5b9e\u73b0\u7c7b\u90fd\u8981\u53bb\u4fee\u6539\u3002 \u54c8\u54c8\uff0cjdk8\u4ee5\u540e\uff0c\u9ed8\u8ba4\u65b9\u6cd5\u89e3\u51b3\u4e86\u8fd9\u4e2a\u95ee\u9898\u3002\u7ed9\u65e7\u63a5\u53e3\u6dfb\u52a0\u65b0\u65b9\u6cd5\u65f6\uff0c\u6307\u5b9a\u9ed8\u8ba4\u5b9e\u73b0\uff0c\u5b83\u7684\u65e7\u5b9e\u73b0\u7c7b\u4e0d\u7528\u52a8\u5c31\u80fd\u517c\u5bb9\uff0c\u662f\u4e0d\u662f\u5f88\u5f00\u5fc3\u3002<\/p>\n<div>\n<pre class=\" language-none\"><code class=\" language-none\">public interface TimeClient {\n    void setTime(int hour, int minute, int second);\n    void setDate(int day, int month, int year);\n    void setDateAndTime(int day, int month, int year,\n                               int hour, int minute, int second);\n    LocalDateTime getLocalDateTime();\n        \n    static ZoneId getZoneId (String zoneString) {\n        try {\n            return ZoneId.of(zoneString);\n        } catch (DateTimeException e) {\n            System.err.println(\"Invalid time zone: \" + zoneString +\n                    \"; using default time zone instead.\");\n            return ZoneId.systemDefault();\n        }\n    }\n            \n    default ZonedDateTime getZonedDateTime(String zoneString) {\n        return ZonedDateTime.of(getLocalDateTime(), getZoneId(zoneString));\n    }\n}<\/code><\/pre>\n<\/div>\n<h3 id=\"toc_5\">Static Methods<\/h3>\n<p>\u63a5\u53e3\u91cc\u4e5f\u53ef\u4ee5\u6709\u9759\u6001\u65b9\u6cd5\u4e86\u3002\u5982\u4e0a<\/p>\n<h3 id=\"toc_6\">\u7c7b\u578b\u6ce8\u89e3<\/h3>\n<p>\u4e4b\u524d\u7684\u6ce8\u89e3\u53ea\u80fd\u7528\u4e8e\u5b9a\u4e49\u7684\u5730\u65b9\uff0c\u800c1.8\u5f00\u59cb\uff0c\u5219\u53ef\u80fd\u5728\u7c7b\u578b\u5f15\u7528\u7684\u4efb\u4f55\u5730\u65b9\u3002 \u4f8b\u5982 @NotNull String name;<\/p>\n<h3 id=\"toc_7\">\u91cd\u590d\u6ce8\u89e3<\/h3>\n<p>\u73b0\u5728\u652f\u6301\u4e86\u76f8\u540c\u7684\u6ce8\u89e3\u5728\u540c\u4e00\u4e2a\u5730\u65b9\u51fa\u73b0\u591a\u6b21\u3002<\/p>\n<h3 id=\"toc_8\">stream\u548c\u805a\u5408\u529f\u80fd<\/h3>\n<p>\u5f52\u7ea6\u662f\u5c06\u96c6\u5408\u4e2d\u7684\u6240\u6709\u5143\u7d20\u7ecf\u8fc7\u6307\u5b9a\u8fd0\u7b97\uff0c\u6298\u53e0\u6210\u4e00\u4e2a\u5143\u7d20\u8f93\u51fa\uff0c\u5982\uff1a\u6c42\u6700\u503c\u3001\u5e73\u5747\u6570\u7b49\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u90fd\u662f\u5c06\u4e00\u4e2a\u96c6\u5408\u7684\u5143\u7d20\u6298\u53e0\u6210\u4e00\u4e2a\u5143\u7d20\u8f93\u51fa\u3002<\/p>\n<div>\n<pre class=\" language-none\"><code class=\" language-none\">1.Intermdiate\uff1a\u4e00\u4e2aStream\u540e\u9762\u53ef\u4ee5\u8c03\u7528\u4efb\u610f\u591a\u4e2aIntermdiate\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5176\u76ee\u7684\u4e3b\u8981\u662f\u6253\u5f00\u6d41\uff0c\u4f5c\u51fa\u67d0\u79cd\u7a0b\u5ea6\u7684\u6570\u636e\u6620\u5c04\/\u8fc7\u6ee4\uff0c\u7136\u540e\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6d41\u4ea4\u7ed9\u4e0b\u4e00\u6b65\u64cd\u4f5c\uff0c\u4f46\u662f\u8fd9\u4e00\u7cfb\u5217\u7684\u64cd\u4f5c\u662flazy\u7684\uff0c\u5e76\u4e0d\u662f\u6bcf\u4e2a\u64cd\u4f5c\u4e00\u4e00\u8c03\u7528\u3002\n2.terminal\uff1a\u987e\u540d\u601d\u4e49\uff0cterminal\u64cd\u4f5c\u5c31\u662f\u5c06Stream\u5173\u95ed\uff0c\u4e00\u4e2aStream\u6700\u591a\u53ea\u80fd\u6709\u4e00\u4e2aterminal\u64cd\u4f5c\u3002\n\nIntermediate\uff1a\nmap (mapToInt, flatMap \u7b49)\u3001 filter\u3001 distinct\u3001 sorted\u3001 peek\u3001 limit\u3001 skip\u3001 parallel\u3001 sequential\u3001 unordered\n\nTerminal\uff1a\nforEach\u3001 forEachOrdered\u3001 toArray\u3001 reduce\u3001 collect\u3001 min\u3001 max\u3001 count\u3001 anyMatch\u3001 allMatch\u3001 noneMatch\u3001 findFirst\u3001 findAny\u3001 iterator\n\nShort-circuiting\uff1a\nanyMatch\u3001 allMatch\u3001 noneMatch\u3001 findFirst\u3001 findAny\u3001 limit<\/code><\/pre>\n<\/div>\n<h3 id=\"toc_9\">Pipeline and Stream<\/h3>\n<p>\u4e00\u4e2apipeline\u5305\u542b\uff1a\u6e90\u3001\u64cd\u4f5c\u3001\u7ec8\u7ed3\u64cd\u4f5c A pipeline contains the following components:<\/p>\n<div>\n<pre class=\" language-none\"><code class=\" language-none\">    A source: This could be a collection, an array, a generator function, or an I\/O channel. \n\n    Zero or more intermediate operations. An intermediate operation, such as filter, produces a new stream.\n\n    A terminal operation. A terminal operation, such as forEach, produces a non-stream result, such as a primitive value (like a double value), a collection, or in the case of forEach, no value at all. In this example, the parameter of the forEach operation is the lambda expression e -&gt; System.out.println(e.getName()), which invokes the method getName on the object e. (The Java runtime and compiler infer that the type of the object e is Person.)<\/code><\/pre>\n<\/div>\n<h3 id=\"toc_10\">Compact Profiles<\/h3>\n<p>jdk8\u63d0\u4f9b\u4e86compact1, compact2, \u548c compact3\u4e09\u4e2a\u7248\u672c\u7684\u7d27\u51d1\u7248\u672c\uff0c\u8fd0\u884c\u5728\u4e0d\u540c\u7c7b\u578b\u7684\u7ec8\u7aef\u4e0a\u3002<\/p>\n<h3 id=\"toc_11\">try-with-resources<\/h3>\n<h3 id=\"toc_12\">Optional\u89e3\u51b3\u7a7a\u6307\u9488<\/h3>\n<div>\n<pre class=\" language-none\"><code class=\" language-none\">return Optional.ofNullable(u)\n                    .map(user-&gt;user.name)\n                    .orElse(\"Unknown\");<\/code><\/pre>\n<\/div>\n<h2 id=\"toc_13\">java 9<\/h2>\n<p><a href=\"https:\/\/www.cnblogs.com\/lufeiludaima\/p\/pz20190216.html\">what\u2019s new in jdk9<\/a><\/p>\n<h3 id=\"toc_14\">jshell<\/h3>\n<h3 id=\"toc_15\">\u591a\u4e2a\u7248\u672cjar\u517c\u5bb9<\/h3>\n<h3 id=\"toc_16\">\u63a5\u53e3\u7684\u79c1\u6709\u65b9\u6cd5<\/h3>\n<h3 id=\"toc_17\">\u6539\u8fdbtry-with-resources<\/h3>\n<h3 id=\"toc_18\">\u94bb\u77f3\u64cd\u4f5c\u7b26<\/h3>\n<p>\u94bb\u77f3\u64cd\u4f5c\u7b26\u53ef\u4ee5\u4f7f\u7528\u533f\u540d\u5b9e\u73b0\u7c7b\uff0c\u53ef\u4ee5\u5728\u533f\u540d\u5b9e\u73b0\u7c7b\u4e2d\u91cd\u5199\u65b9\u6cd5\u7b49\u64cd\u4f5c\u3002<\/p>\n<h3 id=\"toc_19\">\u9650\u5236\u4f7f\u7528\u5355\u72ec\u4e0b\u5212\u7ebf\u6807\u8bc6\u7b26<\/h3>\n<h3 id=\"toc_20\">\u96c6\u5408\u63a5\u53e3\u7684of\u65b9\u6cd5\u521b\u5efa\u53ea\u8bfb\u96c6\u5408\uff1b<\/h3>\n<h2 id=\"toc_21\">java 10<\/h2>\n<h3 id=\"toc_22\">\u5c40\u90e8\u53d8\u91cf\u7c7b\u578b\u63a8\u65ad<\/h3>\n<h2 id=\"toc_23\">java 11<\/h2>\n","protected":false},"excerpt":{"rendered":"<p>Java\u65b0\u7279\u6027 java 8 lambda\u8868\u8fbe\u5f0f Lambda\u8868\u8fbe\u5f0f\u53ef\u4ee5\u4ee3\u66ff\u533f\u540d\u7c7b\uff0c\u5b83\u5c06\u529f\u80fd\uff08\u51fd\u6570\uff09 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"series":[],"class_list":["post-486","post","type-post","status-publish","format-standard","hentry","category-programmers"],"_links":{"self":[{"href":"https:\/\/fick707.com\/index.php?rest_route=\/wp\/v2\/posts\/486"}],"collection":[{"href":"https:\/\/fick707.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fick707.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fick707.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fick707.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=486"}],"version-history":[{"count":2,"href":"https:\/\/fick707.com\/index.php?rest_route=\/wp\/v2\/posts\/486\/revisions"}],"predecessor-version":[{"id":488,"href":"https:\/\/fick707.com\/index.php?rest_route=\/wp\/v2\/posts\/486\/revisions\/488"}],"wp:attachment":[{"href":"https:\/\/fick707.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fick707.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fick707.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=486"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/fick707.com\/index.php?rest_route=%2Fwp%2Fv2%2Fseries&post=486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}