{"id":1173,"date":"2016-08-18T17:37:28","date_gmt":"2016-08-18T09:37:28","guid":{"rendered":"http:\/\/www.yeetrack.com\/?p=1173"},"modified":"2016-08-18T23:02:17","modified_gmt":"2016-08-18T15:02:17","slug":"%e8%b5%b0%e8%bf%91guava%e4%ba%8c-%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.yeetrack.com\/?p=1173","title":{"rendered":"\u8d70\u8fd1Guava(\u4e8c): \u51fd\u6570\u5f0f\u7f16\u7a0b"},"content":{"rendered":"<div class=\"blog-body\">\n<div class=\"BlogContent\">\n<h1>\u51fd\u6570\u5f0f\u7f16\u7a0b\uff1a<\/h1>\n<h2>\u4f7f\u7528Function\u63a5\u53e3(jdk8\u4e2d\u5df2\u7ecf\u5b58\u5728)\uff1a<\/h2>\n<pre class=\"brush:java; toolbar: true; auto-links: false;\"><code>\/**\n * \u5176\u529f\u80fd\u5c31\u662f\u5c06\u8f93\u5165\u7c7b\u578b\u8f6c\u6362\u4e3a\u8f93\u51fa\u7c7b\u578b\n *\/\npublic interface Function&lt;F, T&gt; {\n  T apply(@Nullable F input);\n}<\/code><\/pre>\n<p>\u6bd4\u5982\u4e00\u4e2a\u7b80\u5355\u7684\u65e5\u671f\u8f6c\u6362\uff1a<!--more--><\/p>\n<pre class=\"brush:java; toolbar: true; auto-links: false;\"><code>\/**\n * \u65e5\u671f\u8f6c\u6362\n *\/\npublic class DateFormatFunction implements Function&lt;Date, String&gt; {\n\t@Override\n\tpublic String apply(Date input) {\n\t\tSimpleDateFormat dateFormat = new SimpleDateFormat(\"dd\/mm\/yyyy\");\n\t\treturn dateFormat.format(input);\n\t}\n}<\/code><\/pre>\n<h2>\u4f7f\u7528Functions\u7c7b\uff1a<\/h2>\n<ul>\n<li><span style=\"line-height: 1.5; font-size: 12.5px;\"><span style=\"color: #e53333; font-size: 16px;\">Functions.forMap()<\/span>\u65b9\u6cd5\uff1a<\/span><\/li>\n<\/ul>\n<pre class=\"brush:java; toolbar: true; auto-links: false;\"><code>\/**\n * \u5dde\u7c7b\n *\/\npublic class State {\n\tprivate String name;\n\tprivate String code;\n\tprivate Set&lt;City&gt; mainCities = new HashSet&lt;City&gt;();\n}<\/code><\/pre>\n<p>\u73b0\u5728\u4f60\u60f3\u5728\u4e00\u4e2aMap&lt;String, State&gt;(key\u4e3a\u5dde\u7684\u7f16\u53f7)\u5bf9\u8c61\u4e2d\u67e5\u627e\u67d0\u4e2akey, \u4f60\u53ef\u4ee5\uff1a<\/p>\n<pre class=\"brush:java; toolbar: true; auto-links: false;\"><code>Map&lt;String, State&gt; states = new HashMap&lt;String, State&gt;();\nFunction&lt;String, State&gt; lookup = Functions.forMap(states);\nSystem.out.println(lookup.apply(key));\/\/key\u4e0d\u5b58\u5728\u4f1a\u629b\u5f02\u5e38\n\n\/\/\u4f60\u4e5f\u53ef\u4ee5\u7ed9\u4e0d\u5b58\u5728\u7684key\u6307\u5b9a\u4e00\u4e2a\u9ed8\u8ba4\u503c\nFunction&lt;String, State&gt; lookup = Functions.forMap(states, null);<\/code><\/pre>\n<ul>\n<li><span style=\"line-height: 1.5; font-size: 12.5px;\"><span style=\"color: #e53333; font-size: 16px;\">Functions.compose()<\/span>\u65b9\u6cd5<\/span><\/li>\n<\/ul>\n<pre class=\"brush:java; toolbar: true; auto-links: false;\"><code>\/**\u57ce\u5e02\u7c7b**\/\npublic class City {\n\tprivate String name;\n\tprivate String zipCode;\n\tprivate int population;\n\n\t@Override\n\tpublic String toString() {\n\t\treturn name;\n\t}\n}<\/code><\/pre>\n<pre class=\"brush:java; toolbar: true; auto-links: false;\"><code>\/**\n * \u5c06\u5dde\u7684\u57ce\u5e02\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\n *\/\npublic class StateToCityString implements Function&lt;State, String&gt; {\n\t@Override\n\tpublic String apply(State input) {\n\t\treturn Joiner.on(\",\").join(input.getMainCities());\n\t}\n}<\/code><\/pre>\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u7ec4\u5408Function\uff0c\u67e5\u627e\u67d0\u5dde\u7684\u57ce\u5e02\u5217\u8868<\/p>\n<pre class=\"brush:java; toolbar: true; auto-links: false;\"><code>Function&lt;String, State&gt; lookup = Functions.forMap(states);\nFunction&lt;State, String&gt; stateFunction = new StateToCityString(); \/\/\u5dde\u5230\u57ce\u5e02\u7684\u8f6c\u6362\nFunction&lt;String, String&gt; stateCitiesFunction = Functions.compose(stateFunction, lookup); \/\/\u7ec4\u5408Function\nSystem.out.println(stateCitiesFunction.apply(key));<\/code><\/pre>\n<p>\u7b49\u4ef7\u4e8e\uff1a<\/p>\n<pre class=\"brush:java; toolbar: true; auto-links: false;\"><code>stateFunction.apply(lookup.apply(key));<\/code><\/pre>\n<h2>\u4f7f\u7528Predicate\u63a5\u53e3(jdk8\u4e2d\u5df2\u5b58\u5728)\uff1a<\/h2>\n<ul>\n<li><span style=\"line-height: 1.5; font-size: 12.5px;\">Predicate\u63a5\u53e3<\/span><\/li>\n<\/ul>\n<pre class=\"brush:java; toolbar: true; auto-links: false;\"><code>public interface Predicate&lt;T&gt; {\n     boolean apply(T input); \/\/\u4e0d\u540c\u4e8eFunction.apply, \u8be5apply\u7528\u4e8e\u8fc7\u6ee4\u5bf9\u8c61\n}<\/code><\/pre>\n<p>\u5982\uff1a<\/p>\n<pre class=\"brush:java; toolbar: true; auto-links: false;\"><code>\/**\n * \u8fc7\u6ee4\u4eba\u53e3\u5c0f\u4e8e500000\u7684\u57ce\u5e02\n *\/\npublic class PopulationPredicate implements Predicate&lt;City&gt; {\n\t@Override\n\tpublic boolean apply(City input) {\n\t\treturn input.getPopulation() &lt;= 500000;\n\t}\n}<\/code><\/pre>\n<h3>\u4f7f\u7528Predicates\u7c7b\uff1a<\/h3>\n<p>\u6709\u4e24\u4e2a\u8fc7\u6ee4\u6761\u4ef6\uff1a<\/p>\n<pre class=\"brush:java; toolbar: true; auto-links: false;\"><code>\/**\n * \u9009\u62e9\u6c14\u5019\u4e3aTEMPERATE\u7684\u57ce\u5e02\n *\/\npublic class TemperateClimatePredicate implements Predicate&lt;City&gt; {\n\t@Override\n\tpublic boolean apply(City input) {\n\t\treturn input.getClimate().equals(Climate.TEMPERATE);\n\t}\n}\n\n\/**\n * \u9009\u62e9\u96e8\u91cf\u5c0f\u4e8e45.7\u7684\u57ce\u5e02\n *\/\npublic class LowRainfallPredicate implements Predicate&lt;City&gt; {\n\t@Override\n\tpublic boolean apply(City input) {\n\t\treturn input.getAverageRainfall() &lt; 45.7;\n\t}\n}<\/code><\/pre>\n<p>\u4f60\u53ef\u4ee5\u8fd0\u7528\u4e0b\u9762\u7684\u65b9\u6cd5\u5b9e\u73b0\u8fc7\u6ee4\u7ec4\u5408\u7b49\uff1a<\/p>\n<pre class=\"brush:java; toolbar: true; auto-links: false;\"><code>Predicates.and(smallPopulationPredicate,lowRainFallPredicate);\/\/\u4e14\nPredicates.or(smallPopulationPredicate,temperateClimatePredicate);\/\/\u6216\nPredicate.not(smallPopulationPredicate);\/\/\u975e\nPredicates.compose(smallPopulationPredicate,lookup);\/\/\u7ec4\u5408\u8f6c\u6362\u518d\u8fc7\u6ee4<\/code><\/pre>\n<h2>\u4f7f\u7528Supplier\u63a5\u53e3\uff1a<\/h2>\n<ul>\n<li><span style=\"line-height: 1.5; font-size: 12.5px;\">Supplier\u63a5\u53e3<\/span><\/li>\n<\/ul>\n<pre class=\"brush:java; toolbar: true; auto-links: false;\"><code>public interface Supplier&lt;T&gt; {\n       T get(); \/\/\u7528\u4e8e\u521b\u5efa\u5bf9\u8c61\n}<\/code><\/pre>\n<h2>\u4f7f\u7528Suppliers\u7c7b\uff1a<\/h2>\n<ul>\n<li><span style=\"line-height: 1.5; font-size: 12.5px;\"><span style=\"color: #e53333; font-size: 16px;\">Suppliers.memorize()<\/span>\u65b9\u6cd5\uff1a<\/span><\/li>\n<\/ul>\n<pre class=\"brush:java; toolbar: true; auto-links: false;\"><code>SupplyCity sc = new SupplyCity();\nSystem.out.println(Suppliers.memoize(sc).get());\nSystem.out.println(Suppliers.memoize(sc).get());\/\/\u8fd4\u56de\u540c\u4e00\u5bf9\u8c61, \u5355\u4f8b<\/code><\/pre>\n<ul>\n<li><span style=\"line-height: 1.5; font-size: 12.5px;\"><span style=\"font-size: 16px; color: #e53333;\">Suppliers.memorizeWithExpiration()<\/span>\u65b9\u6cd5\uff1a<\/span><\/li>\n<\/ul>\n<pre class=\"brush:java; toolbar: true; auto-links: false;\"><code>SupplyCity sc = new SupplyCity(); \/\/\u8d85\u65f6\u518d\u65b0\u5efa\u5bf9\u8c61, \u7c7b\u4f3c\u7f13\u5b58\nSupplier&lt;City&gt; supplier = Suppliers.memoizeWithExpiration(sc, 5, TimeUnit.SECONDS);\nCity c = supplier.get();\nSystem.out.println(c); \nThread.sleep(3000);\nc = supplier.get();\nSystem.out.println(c); \/\/\u4e0e\u4e4b\u524d\u76f8\u7b49\nThread.sleep(2000);\nc = supplier.get();\nSystem.out.println(c); \/\/\u4e0e\u4e4b\u524d\u4e0d\u7b49<\/code><\/pre>\n<p>Guava\u51fd\u6570\u5f0f\u7f16\u7a0b\u57fa\u7840\uff0c\u540e\u9762\u96c6\u5408\u5904\u7406\u4e2d\uff0c\u5c06\u4f53\u73b0\u5f97\u66f4\u5f3a\u5927\u3002<\/p>\n<div class=\"video-container\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"4QvRM4qwnv\"><p><a href=\"https:\/\/www.yeetrack.com\/?p=1171\">\u8d70\u8fd1Guava(\u4e00): \u57fa\u672c\u5de5\u5177<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"\u300a\u8d70\u8fd1Guava(\u4e00): \u57fa\u672c\u5de5\u5177\u300b\u2014\u7a7a\u7a7a\u535a\u5ba2\" src=\"https:\/\/www.yeetrack.com\/?p=1171&#038;embed=true#?secret=6o4cXh1ztJ#?secret=4QvRM4qwnv\" data-secret=\"4QvRM4qwnv\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/div>\n<div class=\"video-container\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"E6el2EO5Hu\"><p><a href=\"https:\/\/www.yeetrack.com\/?p=1173\">\u8d70\u8fd1Guava(\u4e8c): \u51fd\u6570\u5f0f\u7f16\u7a0b<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"\u300a\u8d70\u8fd1Guava(\u4e8c): \u51fd\u6570\u5f0f\u7f16\u7a0b\u300b\u2014\u7a7a\u7a7a\u535a\u5ba2\" src=\"https:\/\/www.yeetrack.com\/?p=1173&#038;embed=true#?secret=flJ5jqyAPQ#?secret=E6el2EO5Hu\" data-secret=\"E6el2EO5Hu\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/div>\n<div class=\"video-container\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"OKmeSxt3wn\"><p><a href=\"https:\/\/www.yeetrack.com\/?p=1174\">\u8d70\u8fd1Guava(\u4e09): \u96c6\u5408<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"\u300a\u8d70\u8fd1Guava(\u4e09): \u96c6\u5408\u300b\u2014\u7a7a\u7a7a\u535a\u5ba2\" src=\"https:\/\/www.yeetrack.com\/?p=1174&#038;embed=true#?secret=zGwXkLqKuL#?secret=OKmeSxt3wn\" data-secret=\"OKmeSxt3wn\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/div>\n<div class=\"video-container\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"VC6McrjdZq\"><p><a href=\"https:\/\/www.yeetrack.com\/?p=1175\">\u8d70\u8fd1Guava(\u56db): \u5e76\u53d1<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"\u300a\u8d70\u8fd1Guava(\u56db): \u5e76\u53d1\u300b\u2014\u7a7a\u7a7a\u535a\u5ba2\" src=\"https:\/\/www.yeetrack.com\/?p=1175&#038;embed=true#?secret=dlQnKa0BkZ#?secret=VC6McrjdZq\" data-secret=\"VC6McrjdZq\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/div>\n<div class=\"video-container\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"Sp201E3NfC\"><p><a href=\"https:\/\/www.yeetrack.com\/?p=1176\">\u8d70\u8fd1Guava(\u4e94): \u7f13\u5b58<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"\u300a\u8d70\u8fd1Guava(\u4e94): \u7f13\u5b58\u300b\u2014\u7a7a\u7a7a\u535a\u5ba2\" src=\"https:\/\/www.yeetrack.com\/?p=1176&#038;embed=true#?secret=i4gIQS6GMn#?secret=Sp201E3NfC\" data-secret=\"Sp201E3NfC\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/div>\n<div class=\"video-container\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"nw8ryuc709\"><p><a href=\"https:\/\/www.yeetrack.com\/?p=1177\">\u8d70\u8fd1Guava(\u516d): \u4e8b\u4ef6\u603b\u7ebfEventBus<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"\u300a\u8d70\u8fd1Guava(\u516d): \u4e8b\u4ef6\u603b\u7ebfEventBus\u300b\u2014\u7a7a\u7a7a\u535a\u5ba2\" src=\"https:\/\/www.yeetrack.com\/?p=1177&#038;embed=true#?secret=BTuboqhYDn#?secret=nw8ryuc709\" data-secret=\"nw8ryuc709\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/div>\n<div class=\"video-container\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"2whhctklwI\"><p><a href=\"https:\/\/www.yeetrack.com\/?p=1178\">\u8d70\u8fd1Guava(\u4e03): \u6587\u4ef6\u64cd\u4f5c<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"\u300a\u8d70\u8fd1Guava(\u4e03): \u6587\u4ef6\u64cd\u4f5c\u300b\u2014\u7a7a\u7a7a\u535a\u5ba2\" src=\"https:\/\/www.yeetrack.com\/?p=1178&#038;embed=true#?secret=YGD7h7y61K#?secret=2whhctklwI\" data-secret=\"2whhctklwI\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/div>\n<p>\u6765\u6e90\uff1ahttp:\/\/my.oschina.net\/indestiny\/blog\/215041<\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u51fd\u6570\u5f0f\u7f16\u7a0b\uff1a \u4f7f\u7528Function\u63a5\u53e3(jdk8\u4e2d\u5df2\u7ecf\u5b58\u5728)\uff1a \/** * \u5176\u529f\u80fd\u5c31\u662f\u5c06\u8f93\u5165\u7c7b\u578b\u8f6c\u6362\u4e3a\u8f93\u51fa\u7c7b\u578b *\/ public interface Function&lt;F, T&gt; { T&#46;&#46;&#46;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[33],"tags":[65,8],"class_list":["post-1173","post","type-post","status-publish","format-standard","hentry","category-coding","tag-guava","tag-java"],"views":3126,"_links":{"self":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/1173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1173"}],"version-history":[{"count":3,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/1173\/revisions"}],"predecessor-version":[{"id":1185,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/1173\/revisions\/1185"}],"wp:attachment":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}