{"id":1300,"date":"2019-01-22T12:00:31","date_gmt":"2019-01-22T03:00:31","guid":{"rendered":"https:\/\/www.bold.ne.jp\/engineer-club\/?p=1300"},"modified":"2025-09-17T14:29:07","modified_gmt":"2025-09-17T05:29:07","slug":"java-optional","status":"publish","type":"post","link":"https:\/\/www.bold.ne.jp\/engineer-club\/java-optional","title":{"rendered":"Java\u306eOptional\uff5cOptional\u306fnull\u304b\u3082\u3057\u308c\u306a\u3044\u3068\u4f1d\u3048\u308b\u3082\u306e"},"content":{"rendered":"<p><strong>Java\u306ejava.util.Optional\u306f\u300cnull\u304b\u3082\u3057\u308c\u306a\u3044\u5024\u300d\u3092\u4e0a\u624b\u306b\u53d6\u308a\u6271\u3046\u305f\u3081\u306e\u30af\u30e9\u30b9\u3067\u3059\u3002<\/strong><br \/>\n<span>Java 8<\/span>\u3067\u8ffd\u52a0\u3055\u308c\u305f\u3001\u307e\u3060\u6bd4\u8f03\u7684\u65b0\u3057\u3044\u30af\u30e9\u30b9\u3067\u3059\u3002<\/p>\n<p>\u300c\u3048\u3001\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u3063\u3066\u623b\u308a\u5024\u304c<span>null<\/span>\u306b\u306a\u308b\u3053\u3068\u304c\u3042\u308b\u306e<span>?<\/span>\u300d\u3068\u3044\u3046\u3053\u3068\u306f\u3001<span>Java<\/span>\u30d7\u30ed\u30b0\u30e9\u30de\u306a\u3089\u4e00\u5ea6\u306f\u7d4c\u9a13\u304c\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u305d\u3046\u3044\u3046\u6642\u306b\u9650\u3063\u3066<span>null<\/span>\u30c1\u30a7\u30c3\u30af\u3092\u5fd8\u308c\u3066\u3057\u307e\u3063\u3066\u3001\u30d0\u30b0\u3092\u4f5c\u308a\u8fbc\u3093\u3067\u6012\u3089\u308c\u3066\u3057\u307e\u3046\u306e\u3067\u3059<span>\u2026<\/span>\u3002<\/p>\n<p>Optional\u3092\u4f7f\u3046\u3068\u3001\u305d\u3046\u3044\u3046\u30df\u30b9\u3092\u4e8b\u524d\u306b\u9632\u3050\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u5024\u304c<span>null<\/span>\u304b\u3082\u3057\u308c\u306a\u3044\u3068\u610f\u8b58\u3067\u304d\u307e\u3059\u3057\u3001<span>null<\/span>\u3060\u3063\u305f\u5834\u5408\u306b\u3042\u308a\u304c\u3061\u306a\u51e6\u7406\u3082\u3001\u5927\u5909\u7c21\u5358\u306b\u66f8\u3051\u308b\u306e\u3067\u3059\u3002<\/p>\n<p>\u3053\u306e\u8a18\u4e8b\u3067\u306f\u300c<span>Optional<\/span>\u3063\u3066\u306a\u306b\u305d\u308c<span>? <\/span>\u304a\u3044\u3057\u3044\u306e<span>?<\/span>\u300d\u306a\u4eba\u5411\u3051\u306b\u3001<span>Optional<\/span>\u306e\u4f7f\u3044\u65b9\u306b\u3064\u3044\u3066\u304a\u4f1d\u3048\u3057\u307e\u3059\u3002<\/p>\n<p>\u203b\u3053\u306e\u8a18\u4e8b\u306e\u30b5\u30f3\u30d7\u30eb\u306f\u3001<span>Java 10<\/span>\u306e\u74b0\u5883\u3067\u52d5\u4f5c\u78ba\u8a8d\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<div id=\"toc_container\" class=\"no_bullets\"><p class=\"toc_title\">\u76ee\u6b21<\/p><ul class=\"toc_list\"><li><a href=\"#JavaOptionalnull\">\uff11\uff0e\u3010Java\u3011Optional\u306fnull\u304b\u3082\u3057\u308c\u306a\u3044\u3068\u4f1d\u3048\u308b\u3082\u306e<\/a><\/li><li><a href=\"#JavaOptional\">\uff12\uff0e\u3010Java\u3011Optional\u306e\u4f7f\u3044\u65b9<\/a><ul><li><a href=\"#-null\">\uff12-\uff11\uff0enull\u30c1\u30a7\u30c3\u30af\u3092\u3057\u306a\u3044\u3068\u75db\u3044\u76ee\u3092\u898b\u308b\u4f8b<\/a><\/li><li><a href=\"#-Optional\">\uff12-\uff12\uff0eOptional\u3092\u4f7f\u3063\u3066\u307f\u308b<\/a><\/li><\/ul><\/li><li><a href=\"#JavaOptional-2\">\uff13\uff0e\u3010Java\u3011Optional\u3092\u4e0a\u624b\u306b\u4f7f\u3046<\/a><ul><li><a href=\"#-ifPresentifPresentOrElse\">\uff13-\uff11\uff0e\u5024\u304c\u3042\u308b\u6642\u3060\u3051\u51e6\u7406\u3092\u3059\u308bifPresent\/ifPresentOrElse<\/a><\/li><li><a href=\"#-orElseorElseGet\">\uff13-\uff12\uff0e\u5024\u304c\u306a\u3044\u6642\u306b\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u3092\u6307\u5b9a\u3067\u304d\u308borElse\u3068orElseGet<\/a><\/li><li><a href=\"#-map\">\uff13-\uff13\uff0e\u5024\u304c\u3042\u308b\u6642\u3060\u3051\u5024\u3092\u5909\u63db\u3059\u308bmap<\/a><\/li><li><a href=\"#-filter\">\uff13-\uff14\uff0e\u6761\u4ef6\u306b\u30de\u30c3\u30c1\u3057\u305f\u5024\u3060\u3051\u306b\u3059\u308bfilter<\/a><\/li><li><a href=\"#-OptionalflatMap\">\uff13-\uff15\uff0e\u5024\u5909\u63db\u306e\u7d50\u679c\u304cOptional\u306e\u5834\u5408\u306b\u4fbf\u5229\u306aflatMap<\/a><\/li><li><a href=\"#-Streamstream\">\uff13-\uff16\uff0e\u5024\u3067Stream\u3092\u4f5c\u308bstream<\/a><\/li><\/ul><\/li><li><a href=\"#i\">\uff14\uff0e\u307e\u3068\u3081<\/a><\/li><\/ul><\/div>\n<h2><span id=\"JavaOptionalnull\">\uff11\uff0e\u3010Java\u3011Optional\u306fnull\u304b\u3082\u3057\u308c\u306a\u3044\u3068\u4f1d\u3048\u308b\u3082\u306e<\/span><\/h2>\n<p>Optional\u306f\u4f55\u304b\u306e\u5024\u3092\u5305\u307f\u8fbc\u3080\u30af\u30e9\u30b9\u3067\u3042\u308a\u3001\u540c\u6642\u306b\u300c\u5024\u306f<span>null<\/span>\u304b\u3082\u3057\u308c\u306a\u3044\u3088\u3001\u6c17\u3092\u4ed8\u3051\u3066<span>!<\/span>\u300d\u3068\u4f1d\u3048\u308b\u305f\u3081\u306e\u30af\u30e9\u30b9\u3067\u3059\u3002<\/p>\n<p>Optional\u306f\u30e1\u30bd\u30c3\u30c9\u306e\u623b\u308a\u5024\u3067\u4f7f\u308f\u308c\u308b\u306e\u304c\u666e\u901a\u3067\u3059\u304c\u3001<span>null<\/span>\u304b\u3082\u3057\u308c\u306a\u3044\u5024\u306b\u4f55\u304b\u51e6\u7406\u3092\u3057\u305f\u3044\u6642\u306b\u3082\u4fbf\u5229\u306b\u4f7f\u3048\u307e\u3059\u3002<\/p>\n<h2><span id=\"JavaOptional\">\uff12\uff0e\u3010Java\u3011Optional\u306e\u4f7f\u3044\u65b9<\/span><\/h2>\n<h3><span id=\"-null\">\uff12-\uff11\uff0enull\u30c1\u30a7\u30c3\u30af\u3092\u3057\u306a\u3044\u3068\u75db\u3044\u76ee\u3092\u898b\u308b\u4f8b<\/span><\/h3>\n<p>\u4ee5\u4e0b\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u3042\u3063\u305f\u3068\u3057\u307e\u3059\u3002<span>getSample<\/span>\u3067<span>OptionalSample<\/span>\u3092\u53d6\u5f97\u3057\u3066\u3001<span>hello<\/span>\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3076\u3082\u306e\u3067\u3059\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">class OptionalSample {\r\n\tstatic OptionalSample getSample() {\r\n\t\tOptionalSample sample = null;\r\n\r\n\t\tif (new java.util.Random().nextBoolean()) {\r\n\t\t\tsample = new OptionalSample();\r\n\t\t}\r\n\r\n\t\treturn sample;\r\n\t}\r\n\r\n\tString hello() {\r\n\t\treturn \"Hello, World.\";\r\n\t}\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tOptionalSample sample = getSample();\r\n\t\tSystem.out.println(sample.hello()); \/\/ \u2192 NullPointerException\u304c\u767a\u751f\u3059\u308b\u304b\u3069\u3046\u304b\u306f\u4e94\u5206\u4e94\u5206!!\r\n\t}\r\n}<\/pre>\n<p>\u30e1\u30bd\u30c3\u30c9<span>getSample<\/span>\u3067<span>OptionalSample<\/span>\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u623b\u3063\u3066\u304f\u308b\u304b\u306f\u30e9\u30f3\u30c0\u30e0\u3067\u3059\u3002<span>hello<\/span>\u3067\u304d\u308b\u304b\u306f\u3001\u307e\u3055\u306b\u904b\u6b21\u7b2c\u3002\u6c17\u3092\u4ed8\u3051\u306a\u3044\u3068<span>NullPointerException<\/span>\u304c\u7c21\u5358\u306b\u767a\u751f\u3057\u3066\u3057\u307e\u3046\u3001\u3068\u3066\u3082\u5371\u967a\u306a\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u3059\u3002<\/p>\n<p>\u3053\u308c\u3092\u3069\u3046\u306b\u304b\u3057\u3088\u3046\u3068\u3059\u308c\u3070\u3001\u666e\u901a\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u30e1\u30bd\u30c3\u30c9\u306e\u623b\u308a\u5024\u304c<span>null<\/span>\u304b\u3069\u3046\u304b\u3001<span>if<\/span>\u6587\u3067\u30c1\u30a7\u30c3\u30af\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">OptionalSample sample = getSample();\r\n\r\nif (sample != null) {\r\n\tSystem.out.println(sample.hello());\r\n}<\/pre>\n<p>\u3067\u3082\u3001\u3053\u3046\u3057\u3066\u623b\u308a\u5024\u3092<span>null<\/span>\u30c1\u30a7\u30c3\u30af\u3057\u3066\u304f\u308c\u308b\u304b\u306f\u3001\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u300c\u547c\u3073\u51fa\u3059\u300d\u30d7\u30ed\u30b0\u30e9\u30de\u6b21\u7b2c\u3067\u3059\u3002\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4e00\u56de\u52d5\u304b\u3057\u3066\u3001\u4e0a\u624b\u304f\u52d5\u3051\u3070\u305d\u306e\u307e\u307e\u3001\u3068\u3044\u3046\u4eba\u3082\u591a\u3044\u3067\u3057\u3087\u3046\u3002\u3067\u3082\u3001\u3053\u306e\u4f8b\u306e\u3088\u3046\u306b\u5b9f\u306f<span>null<\/span>\u304c\u623b\u308b\u3053\u3068\u304c\u3042\u308b\u3068\u3059\u308c\u3070<span>\u2026?<\/span><\/p>\n<p>\u300c\u30e1\u30bd\u30c3\u30c9\u3092\u4f5c\u308b\u300d\u30d7\u30ed\u30b0\u30e9\u30de\u306f\u3001\u3069\u3046\u3084\u308c\u3070\u300c\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3059\u300d\u30d7\u30ed\u30b0\u30e9\u30de\u306b\u300c\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u306f<span>null<\/span>\u304c\u623b\u308b\u304b\u3082\u3057\u308c\u306a\u3044\u300d\u3068\u6c17\u4ed8\u3044\u3066\u3082\u3089\u3048\u308b\u306e\u3067\u3057\u3087\u3046\u304b\u3002\u8a2d\u8a08\u66f8\u3084<span>Javadoc<\/span>\u3092\u304d\u3061\u3093\u3068\u66f8\u3044\u3066\u3082\u3001\u8aad\u307e\u306a\u3044\u4eba\u306f\u5168\u7136\u8aad\u307f\u307e\u305b\u3093\u3088\u306d\u3002\u6b63\u76f4\u8a00\u3063\u3066\u3001\u79c1\u3082\u305d\u3046\u3067\u3059\u3002<\/p>\n<p>\u3053\u308c\u3092<span>Optional<\/span>\u3067\u3069\u3046\u306b\u304b\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<h3><span id=\"-Optional\">\uff12-\uff12\uff0eOptional\u3092\u4f7f\u3063\u3066\u307f\u308b<\/span><\/h3>\n<h4>\uff12-\uff12-\uff11\uff0e\u30e1\u30bd\u30c3\u30c9\u306e\u623b\u308a\u5024\u3092<span>Optional<\/span>\u306b\u3059\u308b<\/h4>\n<p>\u3067\u306f\u3001\u3055\u3063\u305d\u304f<span>Optional<\/span>\u3092\u4f7f\u3063\u3066\u307f\u307e\u3059\u3002<\/p>\n<p>\u307e\u305a\u6700\u521d\u306b\u3084\u308b\u3053\u3068\u306f\u3001\u5024\u304c\u306a\u3044\u304b\u3082\u3057\u308c\u306a\u3044\u30e1\u30bd\u30c3\u30c9\u306e\u623b\u308a\u5024\u306e\u578b\u3092\u3001\u623b\u308a\u5024\u306b\u3057\u305f\u3044\u30af\u30e9\u30b9\u305d\u306e\u3082\u306e\u3067\u306f\u306a\u304f\u3001<span>Optional<\/span>\u306b\u3059\u308b\u3053\u3068\u3067\u3059\u3002<\/p>\n<p>Optional\u306e\u578b\u5f15\u6570<span>(&lt;&gt;<\/span>\u3067\u304f\u304f\u3063\u3066\u3042\u308b\u90e8\u5206<span>)<\/span>\u306f\u3001<span>Optional<\/span>\u3067\u5305\u307f\u8fbc\u307f\u305f\u3044\u30af\u30e9\u30b9\u3092\u66f8\u304d\u307e\u3057\u3087\u3046\u3002\u578b\u5f15\u6570\u3092\u4f7f\u3048\u3070\u3001\u4f55\u304c\u672c\u5f53\u306e\u5024\u306a\u306e\u304b\u547c\u3073\u51fa\u3059\u30d7\u30ed\u30b0\u30e9\u30de\u306b\u7c21\u5358\u306b\u4f1d\u308f\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">\/\/ \u307e\u305a\u306f\u30e1\u30bd\u30c3\u30c9\u306e\u30b7\u30b0\u30cd\u30c1\u30e3\u90e8\u5206\u3060\u3051\u5909\u3048\u3066\u307f\u308b\r\nstatic Optional&lt;OptionalSample&gt; getSample() {<\/pre>\n<p>\u3053\u3046\u3059\u308b\u3068\u3001\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3059\u4eba\u306f\u300c<span>Optional<\/span>\u304c\u623b\u308b\u306a\u3089\u3001\u5024\u304c\u306a\u3044\u304b\u3082\u3057\u308c\u306a\u3044\u3093\u3060\u306a\u300d\u3068\u6c17\u4ed8\u3044\u3066\u304f\u308c\u307e\u3059\u3002<span>Java<\/span>\u30d7\u30ed\u30b0\u30e9\u30de\u306e\u9593\u306b\u306f\u3001<span>Optional<\/span>\u3068\u306f\u305d\u3046\u3044\u3046\u3082\u306e\u3060\u3001\u3068\u3044\u3046\u5171\u901a\u8a8d\u8b58\u304c\u3042\u308b\u304b\u3089\u3067\u3059\u3002<\/p>\n<h4>\uff12-\uff12-\uff12\uff0eOptional\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u308b<\/h4>\n<p>\u6b21\u306b\u3001\u30e1\u30bd\u30c3\u30c9\u304b\u3089<span>Optional<\/span>\u3092<span>return<\/span>\u3059\u308b\u305f\u3081\u306b\u3001<span>Optional<\/span>\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306e\u4f5c\u308a\u65b9\u3092\u77e5\u308a\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>Optional\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u308b\u306b\u306f\u3001<span>Optional.of<\/span>\u304b<span>Optinal.ofNullable<\/span>\u3092\u3001\u5305\u307f\u8fbc\u307f\u305f\u3044\u5024\u3092\u5f15\u6570\u306b\u4f7f\u3044\u307e\u3059\u3002<span>of<\/span>\u306f<span>null<\/span>\u3067\u306a\u3044\u5024\u5c02\u7528\u3067\u3001<span>ofNullable<\/span>\u306f\u5024\u304c<span>null<\/span>\u304b\u3082\u3057\u308c\u306a\u3044\u5834\u5408\u306b\u4f7f\u3044\u307e\u3059\u3002\u5024\u304c<span>null<\/span>\u306e\u5834\u5408\u306f\u3001<span>Optional.empty<\/span>\u3067\u3082\u3088\u3044\u3067\u3059\u306d\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">public static &lt;T&gt; Optional&lt;T&gt; of(T value)\r\n\u578b\u30d1\u30e9\u30e1\u30fc\u30bf:\r\n\tT - \u5024\u306e\u578b\r\n\u30d1\u30e9\u30e1\u30fc\u30bf:\r\n\tvalue - \u8aac\u660e\u3059\u308b\u5024\u3002\u975enull\u3067\u3042\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\r\n\u623b\u308a\u5024:\r\n\t\u5b58\u5728\u3059\u308b\u5024\u3067\u306eOptional\r\n\u4f8b\u5916:\r\n\tNullPointerException - value\u304cnull\u306e\u5834\u5408<\/pre>\n<pre class=\"prettyprint lang:default decode:true\">public static &lt;T&gt; Optional&lt;T&gt; ofNullable(T value)\r\n\u578b\u30d1\u30e9\u30e1\u30fc\u30bf:\r\n\tT - \u5024\u306e\u578b\r\n\u30d1\u30e9\u30e1\u30fc\u30bf:\r\n\tvalue - \u8aac\u660e\u3059\u308bnull\u306e\u5024\r\n\u623b\u308a\u5024:\r\n\t\u6307\u5b9a\u3055\u308c\u305f\u5024\u304c\u975enull\u306e\u5834\u5408\u306f\u73fe\u5728\u306e\u5024\u3092\u6301\u3064Optional\u3001\u305d\u308c\u4ee5\u5916\u306e\u5834\u5408\u306f\u7a7a\u306eOptional<\/pre>\n<pre class=\"prettyprint lang:default decode:true\">public static &lt;T&gt; Optional&lt;T&gt; empty()\r\n\u578b\u30d1\u30e9\u30e1\u30fc\u30bf:\r\n\tT - \u5b58\u5728\u3057\u306a\u3044\u5024\u306e\u578b\r\n\u623b\u308a\u5024:\r\n\t\u7a7a\u306eOptional\u3002<\/pre>\n<p>\u3067\u306f\u3001\u30b5\u30f3\u30d7\u30eb\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u66f8\u304d\u63db\u3048\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u30b5\u30f3\u30d7\u30eb\u3067\u306f\u5024\u306f\u300c<span>null<\/span>\u304b\u3082\u3057\u308c\u306a\u3044\u300d\u306e\u3067\u3001<span>ofNullable<\/span>\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">static Optional&lt;OptionalSample&gt; getSample() {\r\n\tOptionalSample sample = null;\r\n\r\n\tif (new java.util.Random().nextBoolean()) {\r\n\t\tsample = new OptionalSample();\r\n\t}\r\n\r\n\treturn Optional.ofNullable(sample); \/\/ Optional\u3092\u623b\u3059\u3088\u3046\u306b\u3057\u305f\r\n}<\/pre>\n<p>\u3053\u308c\u3067\u30e1\u30bd\u30c3\u30c9\u304c<span>Optional<\/span>\u3092<span>return<\/span>\u3059\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002\u6b21\u306f\u3001\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3057\u305f\u5074\u306e\u51e6\u7406\u3092\u5909\u66f4\u3057\u307e\u3059\u3002<\/p>\n<h4>\uff12-\uff12-\uff13\uff0eOptional\u304b\u3089\u5024\u3092\u53d6\u308a\u51fa\u3059\u3001\u5024\u304c\u3042\u308b\u304b\u30c1\u30a7\u30c3\u30af\u3059\u308b<\/h4>\n<p>Optional\u304b\u3089\u5024\u3092\u53d6\u308a\u51fa\u3059\u306b\u306f\u3001<span>Optional.get<\/span>\u3092\u3057\u307e\u3059\u3002<span>get<\/span>\u3059\u308b\u3068<span>Optional<\/span>\u304c\u6301\u3063\u3066\u3044\u308b\u5024\u3092\u53d6\u308a\u51fa\u305b\u308b\u306e\u3067\u3059\u304c\u3001\u5024\u304c\u306a\u3044\u306a\u3089<span>NoSuchElementException<\/span>\u304c<span>throw<\/span>\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>\u306a\u304a\u3001<span>Optional<\/span>\u3067\u6c17\u3092\u4ed8\u3051\u305f\u3044\u306e\u306f\u3001<span>Optional<\/span>\u3092<span>null<\/span>\u3067\u4f5c\u3063\u305f\u5834\u5408\u3067\u3082\u3001<span>null<\/span>\u3068\u3044\u3046\u5024\u3092\u6301\u3063\u3066\u3044\u308b\u308f\u3051\u3067\u306f\u306a\u3044\u3053\u3068\u3067\u3059\u3002<span>Optional<\/span>\u306f\u3001\u3042\u304f\u307e\u3067\u5024\u306e\u3042\u308a\u30fb\u306a\u3057\u3092\u7ba1\u7406\u3059\u308b\u3068\u3044\u3046\u8003\u3048\u65b9\u306a\u306e\u3067\u3001<span>null<\/span>\u3068\u3044\u3046\u5024\u306f\u76f4\u63a5\u53d6\u308a\u51fa\u305b\u306a\u3044\u306e\u3067\u3059\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">public T get()\r\n\u623b\u308a\u5024:\r\n\t\u3053\u306eOptional\u306b\u3088\u3063\u3066\u8a18\u8ff0\u3055\u308c\u308b\u975enull\u5024\r\n\u4f8b\u5916:\r\n\tNoSuchElementException - \u5024\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408<\/pre>\n<pre class=\"prettyprint lang:default decode:true\">Optional&lt;OptionalSample&gt; opt = getSample();\r\nOptionalSample sample = opt.get();\r\nSystem.out.println(sample.hello());<\/pre>\n<p>Optional\u304b\u3089<span>get<\/span>\u3057\u305f\u969b\u306b\u4f8b\u5916\u304c\u767a\u751f\u3059\u308b\u306e\u306f\u5acc\u306a\u306e\u3067\u3001\u4e8b\u524d\u306b\u5024\u304c\u3042\u308b\u304b\u3092\u30c1\u30a7\u30c3\u30af\u3057\u307e\u3057\u3087\u3046\u3002\u5024\u306e\u3042\u308a\u306a\u3057\u306e\u78ba\u8a8d\u306b\u306f<span>Optional.isPresent<\/span>\u3092\u4f7f\u3044\u307e\u3059\u3002<span>isPresent<\/span>\u306f\u3001\u5024\u304c\u3042\u308b\u306a\u3089<span>true<\/span>\u3092\u623b\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">public boolean isPresent()\r\n\u623b\u308a\u5024:\r\n\t\u5024\u304c\u5b58\u5728\u3059\u308b\u5834\u5408\u306ftrue\u3001\u305d\u3046\u3067\u306a\u3044\u5834\u5408\u306ffalse<\/pre>\n<pre class=\"prettyprint lang:default decode:true\">Optional&lt;OptionalSample&gt; opt = getSample();\r\n\r\nif (opt.isPresent()) {\r\n\tOptionalSample sample = opt.get();\r\n\tSystem.out.println(sample.hello());\r\n}<\/pre>\n<p>\u3053\u308c\u3067<span>Optional<\/span>\u5316\u306f\u4e00\u5fdc\u3067\u304d\u307e\u3057\u305f\u304c<span>\u2026<\/span>\u3046\u30fc\u3093\u3001\u3053\u308c\u3060\u3051\u3060\u3068\u3001\u6b63\u76f4\u8a00\u3063\u3066<span>Optional<\/span>\u306b\u306f\u3044\u3044\u3068\u3053\u308d\u304c\u3042\u307e\u308a\u306a\u3055\u305d\u3046\u306b\u898b\u3048\u307e\u3059\u3002<\/p>\n<p>\u30e1\u30bd\u30c3\u30c9\u304b\u3089\u5024\u3092\u76f4\u63a5\u53d6\u5f97\u3067\u304d\u306a\u3044\u306e\u3067\u3001\u8ffd\u52a0\u306e\u51e6\u7406\u306f\u78ba\u304b\u306b\u66f8\u304f\u3088\u3046\u306b\u306f\u306a\u308a\u307e\u3057\u305f\u3002\u3067\u3082\u3001\u5024\u304c\u306a\u3044\u306a\u3089\u305d\u306e\u307e\u307e<span>get<\/span>\u3059\u308b\u3068\u4f8b\u5916\u304c\u767a\u751f\u3057\u307e\u3059\u3057\u3001\u7d50\u5c40<span>if<\/span>\u6587\u3067\u306e\u30c1\u30a7\u30c3\u30af\u304c\u3042\u308b\u306e\u3067\u3059\u304b\u3089\u3001<span>if<\/span>\u6587\u3067<span>null<\/span>\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b\u306e\u3068\u5909\u308f\u3089\u306a\u3044\u3067\u3059\u3088\u306d<span>?<\/span><\/p>\n<p>\u305d\u3046\u601d\u3063\u3066\u3057\u307e\u3046\u306e\u306f\u3001\u307e\u3060\u3053\u3053\u3067\u306f<span>Optional<\/span>\u306e\u672c\u5f53\u306e\u6a5f\u80fd\u3092\u4f7f\u3063\u3066\u3044\u306a\u3044\u304b\u3089\u3067\u3059\u3002<span>Optional<\/span>\u306e\u4ed6\u306e\u30e1\u30bd\u30c3\u30c9\u3068\u7d44\u307f\u5408\u308f\u305b\u3066\u3053\u305d\u3001<span>Optional<\/span>\u306e\u4fbf\u5229\u306a\u3068\u3053\u308d\u304c\u898b\u3048\u3066\u304d\u307e\u3059\u3002<\/p>\n<h2><span id=\"JavaOptional-2\">\uff13\uff0e\u3010Java\u3011Optional\u3092\u4e0a\u624b\u306b\u4f7f\u3046<\/span><\/h2>\n<p>Optional\u3092\u4e0a\u624b\u306b\u4f7f\u3046\u306b\u306f\u3001<span>Java 8<\/span>\u3067\u8ffd\u52a0\u3055\u308c\u305f\u95a2\u6570\u578b\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u3084\u30e9\u30e0\u30c0\u5f0f\u306e\u77e5\u8b58\u304c\u5c11\u3057\u3060\u3051\u5fc5\u8981\u3067\u3059\u3002\u3067\u3082\u3001\u6016\u304c\u308b\u5fc5\u8981\u306f\u3042\u308a\u307e\u305b\u3093<span>!! <\/span>\u7c21\u5358\u306a\u3053\u3068\u3092\u3059\u308b\u30af\u30e9\u30b9\u3092\u4f5c\u308b\u3060\u3051\u3067\u3059\u3002<\/p>\n<p>Optional\u3092\u4f7f\u3046\u30e1\u30ea\u30c3\u30c8\u306e\u4e00\u3064\u306f\u3001<span>null<\/span>\u30c1\u30a7\u30c3\u30af\u306e<span>if<\/span>\u6587\u3084\u7d9a\u304f<span>else<\/span>\u3092\u3001\u30d7\u30ed\u30b0\u30e9\u30de\u304c\u76f4\u63a5\u66f8\u304b\u306a\u3044\u3067\u6e08\u3080\u3053\u3068\u3067\u3059\u3002\u51e6\u7406\u306e\u66f8\u304d\u5fd8\u308c\u3092\u9632\u6b62\u3059\u308b\u4e0a\u3067\u306f\u3001\u300c\u66f8\u304b\u306a\u304f\u3066\u3082\u3044\u3044\u300d\u3068\u3044\u3046\u3053\u3068\u305d\u306e\u3082\u306e\u304c\u3001\u5927\u5909\u610f\u5473\u306e\u3042\u308b\u3053\u3068\u306a\u306e\u3067\u3059\u3002<\/p>\n<h3><span id=\"-ifPresentifPresentOrElse\">\uff13-\uff11\uff0e\u5024\u304c\u3042\u308b\u6642\u3060\u3051\u51e6\u7406\u3092\u3059\u308b<span>ifPresent\/ifPresentOrElse<\/span><\/span><\/h3>\n<p>\u5024\u304c\u3042\u308b\u6642\u3060\u3051\u51e6\u7406\u3092\u3059\u308b\u3053\u3068\u306f\u826f\u304f\u3042\u308a\u307e\u3059\u3002\u666e\u901a\u306f<span>if<\/span>\u6587\u3092\u4f7f\u3044\u307e\u3059\u304c\u3001<span>Optional<\/span>\u306b\u306f\u305d\u306e\u305f\u3081\u306e\u30e1\u30bd\u30c3\u30c9<span>ifPresent<\/span>\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">public void ifPresent(Consumer&lt;? super T&gt; action)\r\n\u30d1\u30e9\u30e1\u30fc\u30bf:\r\n\taction - \u5024\u304c\u5b58\u5728\u3059\u308b\u5834\u5408\u3001\u5b9f\u884c\u3055\u308c\u308b\u30a2\u30af\u30b7\u30e7\u30f3<\/pre>\n<pre class=\"prettyprint lang:default decode:true\">Optional&lt;OptionalSample&gt; opt = getSample();\r\nopt.ifPresent(v -&gt; System.out.println(v.hello()));<\/pre>\n<p>if\u6587\u3068<span>get<\/span>\u304c\u306a\u304f\u306a\u3063\u305f\u306e\u3067\u3001\u30b9\u30c3\u30ad\u30ea\u3057\u307e\u3057\u305f\u3002\u5024\u304c\u3042\u308b\u5834\u5408\u306e\u51e6\u7406\u3060\u3051\u3092<span>Consumer<\/span>\u3067\u66f8\u3051\u3070\u3044\u3044\u306e\u3067\u3059\u306d\u3002\u5024\u304c\u306a\u3044\u5834\u5408\u306f\u4f55\u3082\u3055\u308c\u307e\u305b\u3093\u3002<\/p>\n<p>ifPresentOrElse\u306f\u3001\u5c11\u3057\u30d1\u30ef\u30fc\u30a2\u30c3\u30d7\u3057\u305f<span>ifPresent<\/span>\u3067\u3059\u3002\u5024\u304c\u306a\u3044\u5834\u5408\u306e<span>else<\/span>\u306e\u51e6\u7406\u3092\u3001\u4e8c\u3064\u76ee\u306e\u5f15\u6570\u306e<span>Runnable<\/span>\u3067\u66f8\u3051\u308b\u306e\u3067\u3059\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">public void ifPresentOrElse(Consumer&lt;? super T&gt; action, Runnable emptyAction)\r\n\u30d1\u30e9\u30e1\u30fc\u30bf:\r\n\taction - \u5024\u304c\u5b58\u5728\u3059\u308b\u5834\u5408\u3001\u5b9f\u884c\u3055\u308c\u308b\u30a2\u30af\u30b7\u30e7\u30f3\r\n\temptyAction - \u5024\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u3001\u5b9f\u884c\u3055\u308c\u308b\u7a7a\u306e\u30d9\u30fc\u30b9\u306e\u30a2\u30af\u30b7\u30e7\u30f3<\/pre>\n<pre class=\"prettyprint lang:default decode:true\">Optional&lt;OptionalSample&gt; opt = getSample();\r\nopt.ifPresentOrElse(v -&gt; System.out.println(v.hello()), () -&gt; System.out.println(\"null\u3067\u30fc\u3059\"));<\/pre>\n<h3><span id=\"-orElseorElseGet\">\uff13-\uff12\uff0e\u5024\u304c\u306a\u3044\u6642\u306b\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u3092\u6307\u5b9a\u3067\u304d\u308b<span>orElse<\/span>\u3068<span>orElseGet<\/span><\/span><\/h3>\n<p>\u5024\u304c\u306a\u3044\u6642\u306e\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u3092\u6307\u5b9a\u3057\u305f\u3044\u306a\u3089\u3001<span>Optional.orElse<\/span>\u3092\u4f7f\u3044\u307e\u3057\u3087\u3046\u3002<span>orElse<\/span>\u306f<span>if<\/span>\uff5e<span>else<\/span>\u306e<span>else<\/span>\u90e8\u5206\u3092\u88dc\u3063\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n<p>\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u3092<span>null<\/span>\u306b\u3057\u305f\u3044\u306a\u3089\u3001<span>orElse<\/span>\u306e\u5f15\u6570\u306b<span>null<\/span>\u3092\u6307\u5b9a\u3059\u308c\u3070<span>OK<\/span>\u3067\u3059\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">public T orElse(T other)\r\n\u30d1\u30e9\u30e1\u30fc\u30bf:\r\n\tother - \u5024\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u3001\u8fd4\u3055\u308c\u308b\u5024\u3002 null\u3082\u53ef\u3002\r\n\u623b\u308a\u5024:\r\n\t\u5024(\u5b58\u5728\u3059\u308b\u5834\u5408)\u3001\u305d\u308c\u4ee5\u5916\u306e\u5834\u5408\u306fother<\/pre>\n<pre class=\"prettyprint lang:default decode:true\">Optional&lt;OptionalSample&gt; opt = getSample();\r\nOptionalSample sample = opt.orElse(new OptionalSample()); \/\/ null\u306e\u5834\u5408\u306e\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u3092\u6307\u5b9a\u3057\u3066\u5024\u3092\u53d6\u5f97\u3059\u308b\r\nSystem.out.println(sample.hello());<\/pre>\n<p>orElse\u306e\u3044\u3044\u3068\u3053\u308d\u306f\u3001\u3053\u3053\u3067\u3082<span>if<\/span>\u6587\u3092\u66f8\u304b\u305a\u306b\u6e08\u3093\u3067\u3044\u308b\u3053\u3068\u3067\u3059\u3002\u3067\u3082\u3001\u3082\u3063\u3068\u5927\u4e8b\u306a\u306e\u306f<span>Optional<\/span>\u306e\u30e1\u30bd\u30c3\u30c9\u4e00\u3064\u3092\u547c\u3076\u3060\u3051\u3067\u3001\u5f8c\u7d9a\u306e\u51e6\u7406\u306f<span>null<\/span>\u304b\u3069\u3046\u304b\u3092\u5168\u304f\u610f\u8b58\u3057\u306a\u304f\u3066\u6e08\u3080\u3053\u3068\u3067\u3059\u3002<\/p>\n<p>\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u3092\u4f5c\u308b\u30ed\u30b8\u30c3\u30af\u304c\u5c11\u3005\u8907\u96d1\u306a\u5834\u5408\u306f\u3001<span>Optional.orElseGet<\/span>\u3092\u4f7f\u3044\u307e\u3057\u3087\u3046\u3002\u3053\u3061\u3089\u3067\u306f\u8907\u6570\u884c\u304b\u3089\u306a\u308b\u51e6\u7406\u3092\u3001<span>Supplier<\/span>\u306e\u30e9\u30e0\u30c0\u5f0f\u3067\u6e21\u305b\u307e\u3059\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">public T orElseGet(Supplier&lt;? extends T&gt; supplier)\r\n\u30d1\u30e9\u30e1\u30fc\u30bf:\r\n\tsupplier - \u623b\u3055\u308c\u308b\u5024\u3092\u751f\u6210\u3059\u308b\u4f9b\u7d66\u95a2\u6570\r\n\u623b\u308a\u5024:\r\n\t\u5b58\u5728\u3059\u308b\u5834\u5408\u306f\u5024\u3001\u305d\u3046\u3067\u306a\u3044\u5834\u5408\u306f\u4f9b\u7d66\u95a2\u6570\u306b\u3088\u3063\u3066\u751f\u6210\u3055\u308c\u305f\u7d50\u679c<\/pre>\n<pre class=\"prettyprint lang:default decode:true\">Optional&lt;OptionalSample&gt; opt = getSample();\r\n\/\/ null\u306e\u5834\u5408\u306e\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u3092\u6307\u5b9a\u3057\u3066\u5024\u3092\u53d6\u5f97\u3059\u308b\r\nOptionalSample sample = opt.orElseGet(() -&gt; {\r\n\t\/\/ \u8907\u6570\u884c\u306e\u4f55\u304b\u306e\u51e6\u7406\u3092\u66f8\u3051\u308b!!\r\n\treturn new OptionalSample();\r\n});\r\nSystem.out.println(sample.hello());<\/pre>\n<p>\u306a\u304a\u3001\u3053\u306e\u30b5\u30f3\u30d7\u30eb\u3067\u306f\u81ea\u5206\u3067\u4f5c\u3063\u305f\u30af\u30e9\u30b9\u3092\u4f7f\u3063\u3066\u3044\u307e\u3059\u304c\u3001<span>String<\/span>\u3084<span>Integer<\/span>\u3067\u3082\u540c\u3058\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3088\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">Optional&lt;String&gt; opt = Optional.empty(); \/\/ String\u3092\u6301\u3064Optional\u3092\u53d6\u5f97\u3001String\u306fnull\u304b\u3082\u3057\u308c\u306a\u3044\r\nString s = opt.orElse(\"\u3053\u3093\u306b\u3061\u306f\u3001\u4e16\u754c\"); \/\/ null\u306e\u5834\u5408\u306e\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u3092\u6307\u5b9a\u3057\u3066get\u3001if\u6587\u306f\u4e0d\u8981\r\nSystem.out.println(s); \/\/ \u5f8c\u7d9a\u51e6\u7406\u3067\u306fnull\u304b\u3069\u3046\u304b\u306f\u610f\u8b58\u3057\u306a\u304f\u3066\u3082\u3044\u3044<\/pre>\n<h3><span id=\"-map\">\uff13-\uff13\uff0e\u5024\u304c\u3042\u308b\u6642\u3060\u3051\u5024\u3092\u5909\u63db\u3059\u308b<span>map<\/span><\/span><\/h3>\n<p>\u5024\u304c\u3042\u308b\u6642\u3060\u3051\u3001\u5024\u3092\u4f55\u304b\u306b\u5909\u63db\u3057\u305f\u3044\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002\u305d\u306e\u5834\u5408\u306f<span>Optional.map<\/span>\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">public &lt;U&gt; Optional&lt;U&gt; map(Function&lt;? super T,? extends U&gt; mapper)\r\n\u578b\u30d1\u30e9\u30e1\u30fc\u30bf:\r\n\tU - \u30de\u30c3\u30d4\u30f3\u30b0\u95a2\u6570\u304b\u3089\u8fd4\u3055\u308c\u308b\u5024\u306e\u578b\r\n\u30d1\u30e9\u30e1\u30fc\u30bf:\r\n\tmapper - \u5b58\u5728\u3059\u308b\u5834\u5408\u3001\u5024\u306b\u9069\u7528\u3059\u308b\u30de\u30c3\u30d4\u30f3\u30b0\u95a2\u6570\r\n\u623b\u308a\u5024:\r\n\t\u5024\u304c\u5b58\u5728\u3059\u308b\u5834\u5408\u306f\u30de\u30c3\u30d4\u30f3\u30b0\u95a2\u6570\u3092\u3053\u306eOptional\u306e\u5024\u306b\u9069\u7528\u3057\u305f\u7d50\u679c\u3092\u8a18\u8ff0\u3059\u308bOptional\u3001\u305d\u308c\u4ee5\u5916\u306e\u5834\u5408\u306f\u7a7a\u306eOptional<\/pre>\n<pre class=\"prettyprint lang:default decode:true\">\/\/ \u5024\u304c\u3042\u308b\u6642\u3060\u3051\u3001\u5024\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u884c\u3057\u305f\u7d50\u679c\u306b\u6587\u5b57\u5217\u3092\u8ffd\u52a0\u3059\u308b\r\nOptional&lt;OptionalSample&gt; opt = getSample();\r\nOptional&lt;String&gt; opt2 = opt.map(v -&gt; v.hello() + \"\u3067\u30fc\u3059\"); \/\/ get\u306e\u623b\u308a\u5024\u306fOptional&lt;String&gt;\u306b\u306a\u308b<\/pre>\n<p>map\u3057\u305f\u7d50\u679c\u306f\u3001\u307e\u305f\u3082\u3084<span>Optional<\/span>\u306b\u306a\u308a\u307e\u3059\u3002\u306a\u305c\u304b\u3068\u8a00\u3046\u3068\u3001\u5909\u63db\u5f8c\u306e\u5024\u304c\u3042\u308b\u304b\u308f\u304b\u3089\u306a\u3044\u304b\u3089\u3067\u3059\u3002\u5024\u304c\u306a\u3044\u306a\u3089\u3001\u5024\u3092\u6301\u305f\u306a\u3044<span>Optional<\/span>\u304c\u623b\u308a\u307e\u3059\u3002<\/p>\n<p>map\u3057\u305f<span>Optional<\/span>\u3092\u4f7f\u3063\u3066\u4f55\u304b\u3092\u3057\u305f\u3044\u306a\u3089\u3001\u524d\u8ff0\u3057\u305f<span>ifPresent<\/span>\u3084<span>orElse<\/span>\u3092\u5408\u308f\u305b\u3066\u4f7f\u3044\u307e\u3057\u3087\u3046\u3002\u30e1\u30bd\u30c3\u30c9\u30c1\u30a7\u30a4\u30f3\u3092\u4f7f\u3046\u3068\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u3088\u308a\u30b9\u30c3\u30ad\u30ea\u3057\u307e\u3059\u3002\u3053\u3046\u3059\u308b\u3068\u3001\u3084\u308a\u305f\u3044\u3053\u3068\u304c<span>if<\/span>\u6587\u3092\u89e3\u8aad\u3059\u308b\u3088\u308a\u3082\u8aad\u307f\u53d6\u308a\u3084\u3059\u3044\u3068\u601d\u3044\u307e\u305b\u3093\u304b<span>?<\/span><\/p>\n<pre class=\"prettyprint lang:default decode:true\">\/\/ \u5024\u304c\u3042\u308b\u6642\u3060\u3051\u3001\u672b\u5c3e\u306b\u6587\u5b57\u5217\u3092\u8ffd\u52a0\u3057\u3066print\u3059\u308b\r\ngetSample().map(v -&gt; v.hello() + \"\u3067\u30fc\u3059\").ifPresent(v -&gt; System.out.println(v));<\/pre>\n<pre class=\"prettyprint lang:default decode:true\">\/\/ \u5024\u304c\u3042\u308b\u6642\u306f\u672b\u5c3e\u306b\u6587\u5b57\u5217\u3092\u8ffd\u52a0\u3001\u5024\u304c\u306a\u3044\u306a\u3089\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u3092print\u3059\u308b\r\nString s = getSample().map(v -&gt; v.hello() + \"\u3067\u30fc\u3059\").orElse(\"\u3053\u3093\u306b\u3061\u306f\u3001\u4e16\u754c\");\r\nSystem.out.println(s);<\/pre>\n<h3><span id=\"-filter\">\uff13-\uff14\uff0e\u6761\u4ef6\u306b\u30de\u30c3\u30c1\u3057\u305f\u5024\u3060\u3051\u306b\u3059\u308b<span>filter<\/span><\/span><\/h3>\n<p>Optional\u304c\u5024\u3092\u6301\u3063\u3066\u304a\u308a\u3001\u307e\u305f\u5024\u304c\u7279\u5b9a\u306e\u6761\u4ef6\u3092\u6e80\u305f\u3059\u6642\u306e\u307f\u53d6\u308a\u51fa\u3057\u305f\u3044\u306a\u3089\u3001<span>Optional.filter<\/span>\u3092\u4f7f\u3044\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>filter\u3057\u305f\u7d50\u679c\u306f\u3001<span>map<\/span>\u3068\u540c\u69d8\u306b<span>Optional<\/span>\u3067\u3059\u3002<span>filter<\/span>\u306e\u6761\u4ef6\u306b\u30de\u30c3\u30c1\u3059\u308b\u306a\u3089\u5024\u304c\u3042\u308b<span>Optional<\/span>\u3067\u3001\u30de\u30c3\u30c1\u3057\u306a\u3044\u306a\u3089\u5024\u3092\u6301\u305f\u306a\u3044<span>Optional<\/span>\u3067\u3059\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">public Optional&lt;T&gt; filter(Predicate&lt;? super T&gt; predicate)\r\n\u30d1\u30e9\u30e1\u30fc\u30bf:\r\n\tpredicate - \u5b58\u5728\u3059\u308b\u5834\u5408\u3001\u5024\u306b\u9069\u7528\u3059\u308b\u8ff0\u8a9e\r\n\u623b\u308a\u5024:\r\n\t\u3053\u306eOptional\u306e\u5024\u3092\u8a18\u8ff0\u3059\u308bOptional(\u5024\u304c\u5b58\u5728\u3057\u3001\u305d\u306e\u5024\u304c\u4e0e\u3048\u3089\u308c\u305f\u8ff0\u8a9e\u3068\u4e00\u81f4\u3059\u308b\u5834\u5408)\u3001\u305d\u3046\u3067\u306a\u3044\u5834\u5408\u306f\u7a7a\u306eOptional<\/pre>\n<pre class=\"prettyprint lang:default decode:true\">\/\/ String\u306eOptional\u3067\u3001\u5024\u306eString\u306bABC\u3092\u542b\u3080\u5834\u5408\u3060\u3051print\u3059\u308b\r\nOptional&lt;String&gt; opt = Optional.of(\"ABCDEFG\");\r\nopt.filter(v -&gt; v.contains(\"ABC\")).ifPresent(v -&gt; System.out.println(v));<\/pre>\n<h3><span id=\"-OptionalflatMap\">\uff13-\uff15\uff0e\u5024\u5909\u63db\u306e\u7d50\u679c\u304c<span>Optional<\/span>\u306e\u5834\u5408\u306b\u4fbf\u5229\u306a<span>flatMap<\/span><\/span><\/h3>\n<p>map\u3067<span>Optional<\/span>\u3092\u623b\u3057\u305f\u3044\u5834\u5408\u306b\u4f7f\u3048\u308b\u306e\u304c<span>Optional.flatMap<\/span>\u3067\u3059\u3002<span>map<\/span>\u3067<span>Optional<\/span>\u306b\u5909\u63db\u3059\u308b\u3068\u3001<span>map<\/span>\u306e\u623b\u308a\u5024\u304c<span>Optional&lt;Optional&lt;?&gt;&gt;<\/span>\u3068\u3044\u3046\u3088\u3046\u306b<span>Optional<\/span>\u306e\u5165\u308c\u5b50\u306b\u306a\u3063\u3066\u3057\u307e\u3044\u3001\u5927\u5909\u4f7f\u3044\u3065\u3089\u3044\u3067\u3059\u3002<\/p>\n<p>flatMap\u3092\u4f7f\u3046\u3068\u3001<span>Optional<\/span>\u304c\u5165\u308c\u5b50\u306b\u306a\u3089\u306a\u3044\u3088\u3046\u306b<span>map<\/span>\u3067\u304d\u307e\u3059\u3002\u3064\u307e\u308a\u3001<span>Optional<\/span>\u306e\u5024\u3092\u53d6\u308a\u51fa\u3057\u3066\u3001\u65b0\u3057\u3044<span>Optional<\/span>\u3092\u4f5c\u3063\u3066\u304f\u308c\u308b\u306e\u3067\u3059\u3002\u3057\u304b\u3082\u3001\u5024\u306e\u6709\u7121\u306f\u305d\u306e\u307e\u307e\u306b\u3057\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">public &lt;U&gt; Optional&lt;U&gt; flatMap(Function&lt;? super T,? extends Optional&lt;? extends U&gt;&gt; mapper)\r\n\u578b\u30d1\u30e9\u30e1\u30fc\u30bf:\r\n\tU - \u30de\u30c3\u30d4\u30f3\u30b0\u95a2\u6570\u306b\u3088\u3063\u3066\u8fd4\u3055\u308c\u305fOptional\u306e\u5024\u306e\u578b\r\n\u30d1\u30e9\u30e1\u30fc\u30bf:\r\n\tmapper - \u5b58\u5728\u3059\u308b\u5834\u5408\u3001\u5024\u306b\u9069\u7528\u3059\u308b\u30de\u30c3\u30d4\u30f3\u30b0\u95a2\u6570\r\n\u623b\u308a\u5024:\r\n\t\u5024\u304c\u5b58\u5728\u3059\u308b\u5834\u5408\u306fOptional\u751f\u6210\u30de\u30c3\u30d4\u30f3\u30b0\u95a2\u6570\u3092\u3053\u306eOptional\u306e\u5024\u306b\u9069\u7528\u3057\u305f\u7d50\u679c\u3001\u305d\u308c\u4ee5\u5916\u306e\u5834\u5408\u306f\u7a7a\u306eOptional<\/pre>\n<pre class=\"prettyprint lang:default decode:true\">\/\/ String\u306eOptional\u3067\u3001\u5024\u306eString\u304cABC\u306e\u5834\u5408\u3060\u3051\u65b0\u3057\u3044Optional\u3092\u4f5c\u308a\u3001\u305d\u3046\u3067\u306a\u3051\u308c\u3070\u7a7a\u306eOptional\u306b\u3059\u308b\r\nOptional&lt;String&gt; opt = Optional.of(\"ABC\");\r\nOptional&lt;String&gt; opt2 = opt.flatMap(v -&gt; \"ABC\".equals(v) ? Optional.of(v + \"CDF\") : Optional.empty());<\/pre>\n<h3><span id=\"-Streamstream\">\uff13-\uff16\uff0e\u5024\u3067<span>Stream<\/span>\u3092\u4f5c\u308b<span>stream<\/span><\/span><\/h3>\n<p>Optional\u304c\u6301\u3063\u3066\u3044\u308b\u5024\u304b\u3089\u76f4\u63a5<span>Stream<\/span>\u3092\u4f5c\u308c\u307e\u3059\u3002<span>Optional<\/span>\u304c\u6301\u3063\u3066\u3044\u308b\u306e\u304c<span>null<\/span>\u306e\u5834\u5408\u306f\u3001\u7a7a\u306e<span>Stream<\/span>\u3067\u3059\u3002<\/p>\n<p>stream\u306f\u3001<span>Optional<\/span>\u306e\u5024\u304c<span>Stream<\/span>\u3092\u4f5c\u308c\u308b\u3088\u3046\u306a\u30af\u30e9\u30b9<span>(List<\/span>\u306a\u3069<span>)<\/span>\u3060\u3068\u3001<span>Stream.flatMap<\/span>\u3068\u7d44\u307f\u5408\u308f\u305b\u3066\u4fbf\u5229\u306b\u4f7f\u3048\u305f\u308a\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">public Stream&lt;T&gt; stream()\r\n\u623b\u308a\u5024:\r\n\t\u5024\u306eStream\u3067\u3059\u3002<\/pre>\n<pre class=\"prettyprint lang:default decode:true\">Optional&lt;List&lt;String&gt;&gt; opt = Optional.of(Arrays.asList(\"AA\", \"BB\"));\r\nopt.stream().flatMap(v -&gt; v.stream()).forEach(v -&gt; System.out.println(v)); \/\/ \"AA\"\u3001\"BB\"\u304c\u51fa\u529b\u3055\u308c\u308b\r\n\r\nOptional&lt;List&lt;String&gt;&gt; opt2 = Optional.empty();\r\nopt2.stream().flatMap(v -&gt; v.stream()).forEach(v -&gt; System.out.println(v)); \/\/ \u4f55\u3082\u51fa\u529b\u3055\u308c\u306a\u3044\u3057\u3001\u30a8\u30e9\u30fc\u306b\u3082\u306a\u3089\u306a\u3044<\/pre>\n<p>\u3053\u306e\u3088\u3046\u306b\u3001\u5024\u3092<span>Optional<\/span>\u304b\u3089\u53d6\u308a\u51fa\u3059\u51e6\u7406\u3092\u3044\u3061\u3044\u3061\u66f8\u304b\u306a\u304f\u3066\u3082\u3001\u5b89\u5168\u306b\u51e6\u7406\u3067\u304d\u308b<span>Stream<\/span>\u304c\u4f5c\u308c\u308b\u306e\u306f\u4fbf\u5229\u3067\u3059\u306d\u3002<\/p>\n<h2><span id=\"i\">\uff14\uff0e\u307e\u3068\u3081<\/span><\/h2>\n<p>\u3053\u306e\u8a18\u4e8b\u3067\u306f\u3001<span>Java 8<\/span>\u304b\u3089\u8ffd\u52a0\u3055\u308c\u305f<span>Optional<\/span>\u3092\u304a\u4f1d\u3048\u3057\u307e\u3057\u305f\u3002<span>Optional<\/span>\u306f\u3001<span>null<\/span>\u304b\u3082\u3057\u308c\u306a\u3044\u30e2\u30ce\u3092\u6271\u3046\u305f\u3081\u306e\u30af\u30e9\u30b9\u3067\u3001\u30e1\u30bd\u30c3\u30c9\u306e\u623b\u308a\u5024\u3068\u3057\u3066\u4f7f\u3046\u3053\u3068\u304c\u4e3b\u306b\u60f3\u5b9a\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u30e1\u30bd\u30c3\u30c9\u304c<span>Optional<\/span>\u3092\u623b\u3059\u3068\u3001\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3076\u4eba\u304c\u300c\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u306f<span>null<\/span>\u3092\u623b\u3059\u304b\u3082\u3057\u308c\u306a\u3044\u306e\u304b\u300d\u3068\u6c17\u4ed8\u304d\u3084\u3059\u304f\u306a\u308a\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u4e88\u671f\u305b\u306c<span>NullPointerException<\/span>\u306e\u767a\u751f\u3092\u5c11\u306a\u304f\u3067\u304d\u308b\u306e\u3067\u3059\u3002<\/p>\n<p>\u305d\u3057\u3066<span>Optional<\/span>\u3092\u4f7f\u3048\u3070\u3001\u666e\u901a\u306e\u51e6\u7406\u3067\u306f<span>if<\/span>\u6587\u3092\u66f8\u304b\u306a\u3051\u308c\u3070\u3067\u304d\u306a\u3044\u826f\u304f\u3042\u308b\u51e6\u7406\u3092\u3001<span>Optional<\/span>\u306e\u30e1\u30bd\u30c3\u30c9\u547c\u3073\u51fa\u3057\u3067\u66f8\u3051\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u884c\u6570\u306f\u6e1b\u308a\u307e\u3059\u3057\u3001\u306a\u306b\u3088\u308a\u3084\u308d\u3046\u3068\u3057\u3066\u3044\u308b\u3053\u3068\u304c\u660e\u78ba\u306b\u8868\u73fe\u3067\u304d\u308b\u306e\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java\u306ejava.util.Optional\u306f\u300cnull\u304b\u3082\u3057\u308c\u306a\u3044\u5024\u300d\u3092\u4e0a\u624b\u306b\u53d6\u308a\u6271\u3046\u305f\u3081\u306e\u30af\u30e9\u30b9\u3067\u3059\u3002 Java 8\u3067\u8ffd\u52a0\u3055\u308c\u305f\u3001\u307e\u3060\u6bd4\u8f03\u7684\u65b0\u3057\u3044\u30af\u30e9\u30b9\u3067\u3059\u3002 \u300c\u3048\u3001\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u3063\u3066\u623b\u308a\u5024\u304cnull\u306b\u306a\u308b\u3053\u3068\u304c\u3042\u308b [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":804,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[14],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bold.ne.jp\/engineer-club\/wp-json\/wp\/v2\/posts\/1300"}],"collection":[{"href":"https:\/\/www.bold.ne.jp\/engineer-club\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bold.ne.jp\/engineer-club\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bold.ne.jp\/engineer-club\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bold.ne.jp\/engineer-club\/wp-json\/wp\/v2\/comments?post=1300"}],"version-history":[{"count":9,"href":"https:\/\/www.bold.ne.jp\/engineer-club\/wp-json\/wp\/v2\/posts\/1300\/revisions"}],"predecessor-version":[{"id":5740,"href":"https:\/\/www.bold.ne.jp\/engineer-club\/wp-json\/wp\/v2\/posts\/1300\/revisions\/5740"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bold.ne.jp\/engineer-club\/wp-json\/wp\/v2\/media\/804"}],"wp:attachment":[{"href":"https:\/\/www.bold.ne.jp\/engineer-club\/wp-json\/wp\/v2\/media?parent=1300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bold.ne.jp\/engineer-club\/wp-json\/wp\/v2\/categories?post=1300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}