JavaSE8ã§è¿½å ãããjava.util.Optional
ã«ã¯null
ã¨ã®æ¦ãã«çµæ¢ç¬¦ãæã£ã¦ãããããã¨æã£ã¦ãããã§ãããæã£ã¦ãã ãã ã¨ä½ãèµ·ãããªãã®ã§ã使ãæ¹ã¨ãæã£ããã¨ã¨ããä¸éãæ¸ãã¦ããã¾ãã
- Optionalã®ãã¡ã¯ããªã¡ã½ãã
- Optionalã®ã¤ã³ã¹ã¿ã³ã¹ã¡ã½ãã
- å¤ãåå¾ãããã®
- å¤ã使ç¨ãããã®
- Optionalã®ã¾ã¾æ±ããã®
- ã¾ã¨ã
ãªããä¸éãã¨è¨ãã¤ã¤OptionalInt
ã¨ãã¯ã¹ã«ã¼ãã¦ãã¾ããæ©ä¼ã¨æ¸ããã¨ãããã°ãã®ãã¡æ¸ãããããã¾ããã
Optional
ã«ã¤ãã¦ã¯è«¸äºæ
ï¼é
çã¨ãç解ä¸è¶³ã¨ãåéã¨ãï¼ã«ããJavaã¨ã³ã¸ãã¢é¤æèªæ¬ã§ã¯è»½ãç´¹ä»ã«ã¨ã©ã¾ã£ã¦ããã¾ããã®ã§ãè£è¶³ã¨ãã¦ãèªã¿ããã ããã¨å¹¸ãã§ãããã¨ããã®è¾ºãåèã«ã©ããã
Optionalã®ãã¡ã¯ããªã¡ã½ãã
Optional
ã®ã³ã³ã¹ãã©ã¯ã¿ã¯å
¬éããã¦ããªããããstaticãã¡ã¯ããªã¡ã½ããã使ããã¨ã«ãªãã¾ããof
, empty
, ofNullable
ã®3ã¡ã½ããã ããªã®ã§è¦ãã¾ããããå
¨é¨ofNullable
ã§è³ãã¯ããã®ã§ãããæ示çã«ä½¿ãåããæ¹ãããã§ãã
ãOptional
ãä½ãæã¯å¸¸ã«ofNullable
ã使ãããªãã¦å¤ãªçãã¤ããªãããã«ãã¦ãã ãããnull
ã«ãªããªããã¨ãããã£ã¦ãããªãã°ãof
ã使ç¨ãã¾ããããæ³å®å¤ã®null
ã«ofNullable
ã使ç¨ãã¦ãã¾ãã¨ãç¾è¡ã§ã¯å¦çæã«ã¬ãã½ã§è½ã¡ã¦å©ãã£ã¦ãããã®ãæ£å¸¸ã¨ãã¦å¦çããã¦ãã¾ãå¯è½æ§ãé«ããªãã¾ããï¼Optional
ãè¦æ±ããã¨è¨ããã¨ã¯ãå¤ãå
¥ã£ã¦ããªãå¯è½æ§ãèæ
®ãããã¨ãå¦çæã«å¼·å¶ãããããã§ããããï¼ãã§ã¤ã«ãã¡ã¹ãã®æå³ã§ããå¯è½ãªå ´åã¯ç©æ¥µçã«of
ã使ã£ãæ¹ãè¯ãã§ãããã
Optional.empty()
ã¯è¨ãã¾ã§ããªããOptional.ofNullable(null)
ã¨æ¸ããããæ示çãªã®ã§ä½¿ããªãçç±ã¯ãªãã§ããã
Optionalã®ã¤ã³ã¹ã¿ã³ã¹ã¡ã½ãã
Optional
ã«å®ç¾©ããã¦ããã¡ã½ããã¯15åããã¡3åã¯åè¿°ã®ãã¡ã¯ããªã¡ã½ããã§ãããã«3åã¯Object
ããã®ãã¤ãã®toString
, hashCode
, equals
ã§ãããã®ãã確èªããã®ã¯ãæ®ãã®9åã«ãªãã¾ãã
9åãã¾ã¨ãã¦ç¸æã«ãã¦ãããã®ã§ãããããã¸ã«ã«ãã³ãã¼7±2ããèããã¨ãã¾ã¨ãã¦ç¸æã«ããã®ã¯å°ã骨ãæãã¾ããåãã¾ããããããããªåããããåºæ¥ãã¨æãã¾ããã7±2ãèæ ®ãããªãã°ã5åãè¶ ããåãæ¹ã¯ã§ãã¾ãããã¨ãããã¨ã§ã以ä¸ã®ããã«åãã¦ã¿ã¾ããã
- å¤ãåå¾ãããã® - 4ã¡ã½ãã
- å¤ã使ç¨ãããã® - 2ã¡ã½ãã
- Optionalã®ã¾ã¾æ±ããã® - 3ã¡ã½ãã
å¤ãåå¾ãããã®
get
, orElse
, orElseGet
, orElseThrow
ã®4ã¤ã§ããOptional<T>
ã«å¯¾ãã¦ãT
ãè¿ãåãã¡ã§ãããã®åãã¡ã¯åºæ¬çã«ã¯åç´ã«ãæã£ã¦ããå¤ãè¿ããã ãã§ããå¤ãæããªãå ´åã®æåãéãã®ã§ãããã ãæ¼ããã¦ããã¾ãããã
ã¡ã½ãã | å¤ããªãæ |
---|---|
get() | NoSuchElementExceptionãã¹ãã¼ |
orElseThrow(Supplier<Throwable>) | å¼æ°ã®Supplierã®æ»ãå¤ãã¹ãã¼ |
orElse(T) | å¼æ°ã®ã¤ã³ã¹ã¿ã³ã¹ |
orElseGet(Supplier<T>) | å¼æ°ã®Supplierã®æ»ãå¤ |
ãããã¦ã¿ãã¨ãget
ã¯orElseThrow
ã®å¼æ°ã®è¦ããªã簡便ãªå®è£
ã£ã½ãè¦ãã¾ããç§ãget
ãå®è£
ãããªãããæ¸ããããªããªã¼ã¨æãã¾ããã
public T get() { return orElseThrow(NoSuchElementException::new); }
ãããã«orElse
ã®å®è£
ããããããã¨ã¯æãã¾ãããâ¦â¦ããããããããªãâ¦â¦ã
public T orElse(T other) { return orElseGet(() -> other); }
å®éã¯ãã£ã¨ç´ ç´ãªå®è£ ã«ãªã£ã¦ã¾ãããã¶ãããããªçç±ããããã§ãããã
æ°ãåãç´ãã¦ãåå¥ã«ä½¿ãæ¹ãè¦ã¦ããã¾ãããã
get, orElseThrow
get
ã使ç¨ããæ©ä¼ã¯å°ãªãã¯ãã§ããOptional
ããããã使ç¨ããã¨è¨ããã¨ã¯ãå¤ãå
¥ã£ã¦ããªãå ´åã¯ãããªã«ãã¦ãããã¨ããã¡ãã»ã¼ã¸ã§ããget
ã使ç¨ãã¦ãããªãä¾å¤ã«ãªãã®ãªããNullPointerException
ãçºçããã®ã¨åãã§ããããã¦å¤ãã®ç¾å ´ãããã§ããããã«ãã¬ãã½ã¯åç´ãªãã¹ãã¨æããã¦ãããªãã°ããªãããé¿ããã¹ãã§ããããï¼ãã®è¾ºãã«ã¤ãã¦ã¯å¥ã¨ã³ããªã§æ¸ãããã¨ããã§ããï¼ã¬ãã½ãNoSuchElementException
ã«å¤ãã£ãã¨ããã§ä½ãå¤ããã¾ãããä»ã¾ã§null
ãã§ãã¯ã®ä¸ã§æ示çãªä¾å¤ãæãã¦ããã®ã¨åæ§ã«orElseThrow
ã使ç¨ã㦠ãã£ã¨ç¦ã
ããä¾å¤ ãæãã¾ãããã
ãããããå¤ãåå¾ãããã¨ããã¨å®è¡æã«ä¾å¤ã¨ãªãget
ãorElseThrow
ã®åºçªã¯ãªãæ¹ãå¥å
¨ã§ããããããåºç¾ãããªãã°ãã使ç¨ããã¹ãã§ãªãå ´é¢ã§Optional
ã使ç¨ããã¦ããã®ã§ã¯ãªãããã¨ããè¦åã¨æãããã¨ãã§ãã¾ãããããã¯ãã®å¥åº·ã®ããã«ããããã®ã¡ã½ããã®åºç¾åæ°ãã«ã¦ã³ããã¦ããã¦ãè¯ãããããã¾ããã
å¼æ°Optional<T>
ã«å¯¾ãã¦get
ãorElseThrow
ã使ç¨ãããã®ãªãã°ãç´ ç´ã«T
ãå¼æ°åã«ãã¾ããããå®è¡æã«ä¾å¤ã«ãªããããã³ã³ãã¤ã«æã«æ¤åºã§ããæ¹ãè¯ãã§ãããã¡ããnull
ã使ç¨ãããã¨æ¬æ«è»¢åãªã®ã§ã@NotNull
ã¨ãã使ãããã¨ããã§ãã
ãªã«ãããã®ã¡ã½ããã®æ»ãå¤ãOptional
ãªãã°ä½¿ããã¨ãããã§ãããããããåå¾ã§ããªãå ´åã«ä¾å¤ãã¹ãã¼ããã¨ããç¨éããããã¯ãã«ããã¦éå®çã§ãªããªãã°ããã®ã¡ã½ããã¯Optional
ãè¿ãããè¿ãå¤ããªãå ´åã«ä¾å¤ãã¹ãã¼ããã»ããè¯ãã§ããããå
¨ã¦ã®å¼ã³åºãç®æã§å¤ãæ¼ããããã«orElseThrow
ã使ç¨ããããªãã¦ãã¨ã¯ãã¦ã¯ããã¾ããã
orElse, orElseGet
ãããã¯å¤ãåå¾ã§ããªãå ´åã®ããã©ã«ãå¤ãæå®ããåå¾æ¹æ³ã¨è¦ããã¨ãã§ãã¾ããããã©ã«ãå¤ãæå®ããAPIã¯ä»ã«ãProperties#getProperty
ãªã©ãããã®ã§ã使ãæ¹ã¯æ³åãã¤ãããããã¾ããã
ä»ã¾ã§å¼æ°ã®ããã¥ã¡ã³ãã³ã¡ã³ãã«ãæå®ã§ããªãæã¯null
ã«ãã¦ãï¼ãã¨ãæ¸ãã¦ããã¨ããã¯ãOptional
ã«ãããã¨ã§ãæå®ã§ããªãæã¯Optional.empty()
ã«ãã¦ãï¼ãã¨ä¼ããã¾ãã誤解ã®ãªãAPIãããã¥ã¡ã³ãã³ã¡ã³ããèªãå¿
è¦ããªããªãããããããããªããã§ãããããOptional.empty()
ã¨ãä½ããããªãã®ã§ããªãã·ã§ã³å¼æ°ã¯ãªã¼ãã¼ãã¼ãã«ãã¦æ¬²ããã¨ããã§ãããç¶æ³æ¬¡ç¬¬ã§ã¯ããã¨æãã¾ãã
ãªãorElseGet
ã®å¼æ°ã§ããSupplier
ã¯ç©ºã®Optional
ã«å¯¾ãã¦orElseGet
ãå¼ã°ããªãéãå®è¡ããã¾ããããã®ããã¤ã³ã¹ã¿ã³ã¹çæãéãå ´åã®ä½¿ç¨ãæ³å®ããã¾ãããããã©ã«ãå¤ãªãã°å
±æã§ããã¤ã³ã¹ã¿ã³ã¹ã«ãªããã¨ãå¤ãã®ã§åºçªã¯å°ãªãããããã¾ããã
ã¨ããã§ãç§ã®è¶£å³ã«åããªãå®è£
ã®ä¸ã¤ã§ãã å¤æ°ã¸ã®å代å
¥ ã§ããããã®å
¸åçãªä½¿ç¨ä¾ã§ãããnull
ã§ãªãå ´åã ãå¤ãä¸æ¸ãããããorElse
ãªã©ã«ããå°ãè¦ãããã®ã«ãªãã¾ããããã ãã§ãOptional
ã使ããããªãã¾ãã
void before() { Object value = DEFAULT_VALUE; Object result = method1(); if (result != null) { value = result; } // value ã使ç¨ããå¦çâ¦â¦ } void after() { Object value = method2().orElse(DEFAULT_VALUE); // value ã使ç¨ããå¦çâ¦â¦ }
ãã¡ããå¼ã³åºãã¡ã½ãããOptional
ãè¿ãããã«ãªã£ã¦ããã§ããããã§null
ãè¿ãããã¡ã½ãããOptional.ofNullable(method1())
ã¨ããå½¢ã§å¼ã¶ã¨å¬ããã¯æ¿æ¸ãã¾ããé§ç®ãªå®è£
ã誤éåãããã«ä½¿ç¨ããã®ã¯é¿ããã¹ãã§ããä»®ã«ããããªã£ãã¨ãã¦ãããã®é¨åã ãå¥ã¡ã½ããã«ãããªã©ä¸å±¤æãã§ã¢ããã¿ã«ããã¨ãããæ¹ãè¯ãã§ãã
orElseThrow
ã®å ´åã¯ãå¼ã³åºãããã¡ã½ããã§ä¾å¤ãã¹ãã¼ããæ¹ãè¯ããã¨åè¿°ãã¾ããããorElse
ã®å ´åãåæ§ã®ãã¨ãè¨ããã§ããããããããªããç§ã®çµé¨ã«ãªãã¾ãããé©åãªããã©ã«ãå¤ãããå ´åã¯å
ã
ãã®å¤ãè¿å´ãã¦ãããã¨ãå¤ããnull
ãè¿ãå ´åã¯ç°å¸¸ã示ããã¨ãå¤ãã¨è¨ãã¾ãããã®ãããªå ´åãããããOptional
ã«ããçç±ã¯ããã¾ãããä»ã¾ã§éãé©åãªnull
以å¤ã®å¤ãè¿ãã¦ããã¦ãã ããã
å¤åå¾ã®ããæ¹ã¨ä½¿ãã©ããã¾ã¨ã
ã¡ã½ãã | å¼æ°ã«å¯¾ã㦠| è¿ãå¤ã«å¯¾ã㦠|
---|---|---|
get() orElseThrow(Supplier<Throwable>) |
Ã: å¼æ°åãTã«ãã | â³: 常ç¨ãããªãå¤ãè¿ãåã«ä¾å¤ãæããããã |
orElse(T) orElseGet(Supplier<T>) |
â³: å¼æ°ãæ¸ããããªã¼ãã¼ãã¼ããåªå ããã | â: ããã©ã«ãå¤ã§å¦çãããifPresent ã§ç¡è¦ãããã®é¸æã«ãªã |
å¤ã使ç¨ãããã®
ifPresent
, isPresent
ã®2ã¡ã½ãããååã¯ä¼¼ã¦ãã®ã§ãã使ãç®çã¯éãã¾ããã°ã«ã¼ãã³ã°ã¨ãã¦ã¯åã£ã¦ãªãæ°ããã¾ãããä»ã«ãã¾ãåããããæãã¤ããªãã£ãã ããªã®ã§æ°ã«ããªãã§ãã ããã
ifPresent
Optional
ã使ç¨ããå ´åã®æ¬å½ã§ãã ifPresent
ã§ãããã®ã¡ã½ããã«ã¯ãå¤ãåå¨ããå ´åã ãè¡ãå¦çãConsumer
åã®å¼æ°ã§ä¸ãã¾ãããã¤ã³ãã¯ä»¥ä¸ã®2ç¹ã
- å¤ã¯å¼ã³åºãå´ã«è¿ããããå¼æ°ã®å¦çã«æ¸¡ãããã
- å¤ãåå¨ããªãæã¯ä½ãå®è¡ãããªãã
void before() { Object value = method1(); if (value != null) { // value ã使ç¨ããå¦çâ¦â¦ } } void after() { method2().ifPresent(value -> { // value ã使ç¨ããå¦çâ¦â¦ }); }
ãããã¦çããããããããããªã£ã¦ããªãããã«è¦ãã¾ããããªãã¨ãªã});
ã«å¤ãJavaèãæãã¾ãããããã¯1è¡ã®å½¢ã§æ¸ããã¨ãã§ãã¾ãããã¡ã½ããåç
§ã使ã£ã¦ããã®Lambdaã ã¨ãæãã®ã§ããã£ã¨ãªãã¨ããªãã¾ãã
æããããå©ç¹ã¯ãå¿ããå¯è½æ§ã®ãã£ãnull
ãã§ãã¯ã«å¯¾ããOptional
ãå¦çããã¨ãã¯åå¾ã§ããªãå ´åã®æ¯ãèããæèãããããç¹ã§ãã
ã¾ããifPresent
ã使ç¨ããã¨å¿
ç¶çã«å¤ã®ã¹ã³ã¼ããå°ãããªãã¾ãã"æé»çfinal"ã«ããLambdaå
ããå¤å´ã®å¤æ°ã¸ã®ä»£å
¥ã¯ã§ããªãã®ã§ããããããå¤æ°ã®ã¹ã³ã¼ãã¯å°ãããã¾ãããï¼ãã¨è¨ããªãã¦è¯ããªãã®ã¯å¬ããã
対ãã¦æ³¨æç¹ã¯ã空ã®Optional
ã®å ´åã«è»½ãã¹ã«ã¼ããã¦ãã¾ããã¨ã§ããæ¬å½ã«ãªãã®å¦çãããªãã¦è¯ããã®æ¤è¨ã¯å¿
é ã§ããä¾ãã°ãOptional
ã¯ifPresent
ã§å¦çããããªãã¦è¬ã®ã«ã¼ã«ãä½ã£ã¦ãã¾ãã¨ãããªãå¦çãå®è¡ãããªãã£ããããããªããå¤ãå
¥ã£ã¦ããªããªãã¦æããªãã£ããããªã©ã¨ãã£ãããããããªãçºè¨ãèããã¨ã«ãªãã§ãããã
isPresent
ã使ã£ããè² ããã¨ã¾ã§è¨ãããisPresent
ãè¦ã¦ã¿ã¾ããããã
ãã¡ãããnull
ãã§ãã¯ã®ä»£ããã®ä½¿ç¨ãã¯è«å¤ã§ããä¾ãã°ã次ã®ãããªå®è£
ã¯ãæåæ°ã¨åæã«åããã¥ããã¨ã¡ã³ããã³ã¹ã³ã¹ããå¢å ãããæªéã®ææ¥ã§ãã
void before() { Object value = method1(); if (value != null) { // value ã使ç¨ããå¦çâ¦â¦ } } void after() { Optional<Object> value = method2(); if (value.isPresent()) { // value ã使ç¨ããå¦çâ¦â¦ } }
ã¾ããOptional
ã使ç¨ããæå³ãèããã°ã使ç¨ããç¬éã§ããå¤ã®æç¡ãæ°ã«ããªãã®ãæ£éã§ã¯ããã¾ãããã®ãããisPresent
ã使ç¨ãã¦æ¬¡ã®ããã«get
ããããªãOptional
ã«åå¨ä¾¡å¤ã¯ããã¾ããã
Optional<Object> opt = method(); if (opt.isPresent()) { Object value = opt.get(); // value ã使ç¨ããå¦çâ¦â¦ }
ããããããã®ãªãã°ifPresent
ã使ç¨ãã¦ãã ããã
isPresentã®ä½¿ãæãèãã¦è¦ã
ããããªããisPresent
ããªããã°ãå¤ãåå¾ããã«å¤ã®æç¡ãå¤æãããã¨ãã§ãã¾ãããã¤ã¾ã次ã®ãããªãã¨ãããããã«ãä¸åº¦"å¤ãªã"ã表ãã¤ã³ã¹ã¿ã³ã¹ã«orElse
ã§å¤æããªãããããªããã¨ã«ãªãã¾ãã
// å¤ã®ãããã®ã ãå¾ã®å¦çãããã Stream.of(Optional.empty(), Optional.of("hoge"), Optional.ofNullable(null), Optional.of("fuga"), Optional.ofNullable("piyo")) .filter(Optional::isPresent) .map(Optional::get) .forEach(System.out::println);
ãã¡ããä¸è¨ç¨åº¦ãªãã°ããã§ãããã§ããã
Stream.of(Optional.empty(), Optional.of("hoge"), Optional.ofNullable(null), Optional.of("fuga"), Optional.ofNullable("piyo")) .forEach(opt -> opt.ifPresent(System.out::println));
Optional
ãåä½æ±ãå ´åã«isPresent
ã®åºçªã¯ç¢ºãã«ç¡ããããªã®ã§ãããStream
ã使ãå ´åã«ã¯åºã¦ãããã«æã£ãã®ã§æ¸ãã¦ã¿ã¾ãããä»ã®ç¨éã¯æãã¤ãã¾ããã
è±ç·
ãã®ãããã©ãã§ãããã®ã§ãããisPresent
ãmap
ã¨orElse
ã§å®è£
ããã¨ãããªæãã«ãªãã¾ããï¼ãããã£ãã¨ãã¦ãmap
ãisPresent
使ã£ã¦ãã®ã§StackOverflowError
ã«ãªããããisPresent
使ã£ã¦ãã¨ããããããããï¼
public boolean isPresent() { return map(v -> true).orElse(false); }
orElse
ã®å¤ãè¿ãã¦ãéãããã®æèã ã¨isPresent
ããå¤ãåå¾ãããã®ãã«åé¡ãã¦è¯ããªãããã§ãã
Optionalã®ã¾ã¾æ±ããã®
ããã¾ã§ã§Object
ç±æ¥ã®3ã¡ã½ãããé¤ãOptional
ã®12ã¡ã½ããã®ãã¡9ã¡ã½ããã確èªãã¾ããã®ã§ã3/4ãææ¡ãããã¨ã«ãªãã¾ããããã§ãæ¦ããã¨ã¯ã§ããã§ãããã
ãããä»ã®ç¶æ
ã§ã¯ãOptional
ã®åå¨ããªãå¤ãè¯ãä¸çã¨Optional
ã®åå¨ããä¸çãåºå
¥ãããæ¹æ³ãç¥ã£ãã ãã§ããããã§ã¯ä½ããããã³ã«ä¸çãå¾å¾©ããªãã¦ã¯ããã¾ãããããã§Optional
ãOptional
ã®ã¾ã¾åãæ±ãã¡ã½ããã®åºçªã¨ãªãã¾ãã
ãªããããã¾ã§ã¯ããã§ãããã¾ããã§ããããããããã¯ããç¨åº¦Lambdaã®ç¥èãå¿ è¦ã«ãªãã¾ããä¸æ¹ã§ãStreamAPIã«æ¢ã«é¦´æãã§ãããªãã°ã ãããã¯äºæ³ãã¤ãã§ãããã
map
Javaã®ä¸çã§ãããããã¨è¨ãã¨HashMap
ãªã©ãæãæµ®ãã¹ããã¦ãã¾ãã®ã§ãããmap
ã¡ã½ããã¯ãããã³ã°ãã¤ã¾ã対å¿ä»ãã§ããMap
ã¤ã³ã¿ãã§ã¼ã¹èªä½ãããã®ã¯ããªã®ã§ããã©ãã©ããã¦ããã¼ããªã¥ã¼ã®ã³ã³ããã¿ãããªã¤ã¡ã¼ã¸ãå¼·ãããããmap
ã¡ã½ãããªã©ã¨ãã¾ããªã³ã¯ããªãå ´åããããããªã®ã§ã念ã®ããæå³ã®å確èªããã¦ããã¾ããã
ãã¦ãmap
ã¯å¼æ°ã®Function
ã«å¾ã£ã¦å¤ãå¤æ´ããæ°ããOptional
ã¤ã³ã¹ã¿ã³ã¹ãä½æãã¾ããä¾ãã°æååãå
¨é¨å¤§æåã«ããããªããã®ããã«ã§ãã¾ãã
Optional<String> str = Optional.of("hoge") .map(String::toUpperCase); assert str.get().equals("HOGE");
ãããã³ã°åå¾ã®åã«é¢é£ã¯ç¡ãã®ã§ãå ¨ãéãåã«ãããã¨ãã§ãã¾ãã
Optional<Integer> length = Optional.of("hoge") .map(String::length); assert length.get().equals(4);
Optional
ã使ç¨ãã¦ããã®ã§ã空ã®å ´åã¯å½ç¶ä½ãè¡ããã¾ããããnull
ã¨ãªããããã³ã°ãè¡ãããå ´åã¯ç©ºã®Optional
ã«ãªãã¾ããï¼å
é¨ã§ã¯ofNullable
ã使ããã¦ãã¾ããï¼
flatMap
flatMap
ã¯ãOptional
ãè¿ãFunction
ã渡ãã¦ãOptional
ããã¹ããããªãmap
ãã§ããä½ãè¨ã£ã¦ããã®ãããããªãã¨æãã¾ãããç§ãä½ãè¨ã£ã¦ããã®ããããã¾ããã®ã§ãã³ã¼ãã§ç解ãã¦ãã ããã
Optional<Object> result = Optional.of("hoge") .flatMap(value -> Optional.of(value + "fuga")) .flatMap(Optional::of) .flatMap(value -> Optional.of(value + "piyo")); assertThat(result.toString(), is("Optional[hogefugapiyo]")); // åã«mapã« Function<String, Optional<String>> ã渡ãããç´ æµãªãã¨ã«ãªãã Optional<Object> result2 = Optional.of("hoge") .map(value -> Optional.of(value + "fuga")) .map(Optional::of) .map(value -> Optional.of(value + "piyo")); assertThat(result2.toString(), is("Optional[Optional[Optional[Optional[hogefuga]]piyo]]"));
flatMap
ã¯ãOptional
ãè¿ãã¡ã½ãããããç¨åº¦ä½ããã¦ããããæ¬çªã§ããã¬ãã«ãä½ãã¨å½¹ã«ç«ããªãæ¦å¨ã¨ãéæ³ã¨ãã¯RPGã§ã¯ããããã¾ãããããã¨åæ§ã«ãããã¯ãã使ç¨ã©ã¤ãã©ãªã®Optional
ã¬ãã«ï¼ãªã«ããï¼ãããç¨åº¦ä¸ãã£ã¦ããã¨åºçªãå¢ãã¦ãã¾ãã
filter
å¤ãåãåã£ã¦boolean
ãè¿ãPredicate
ãå¼æ°ã«ã¨ããæ¡ä»¶ã«åè´ããªããã®ã空ã®Optional
ã«ããåã§ããæåãããããããã§ããããã¾ã説æãããã¨ã¯ããã¾ããã
使ç¨ä¾ãæ»åºããã¨ãç¾å ´ã§ã¯String
ã®null
ã¨ç©ºæåå""
ãåãæ±ãã¨ããå ´åã«ã空ã®Optional
ã«æããã¨è¨ã£ã使ãæ¹ãæ³å®ããã¾ãã
Predicate<String> isEmpty = String::isEmpty; Optional<String> opt = Optional.ofNullable(str) .filter(isEmpty.negate());
ã¡ã½ããåç
§ã®Predicate
ããã¾ãå転ãããæ¹æ³ã欲ãããã®ã â¦â¦
ããã¾ã§ã®æµãã«ä¹ã£ã¦ä¸å¿æ¸ãã¦ããã¾ãã¨ããæ¡ä»¶ã«åè´ããªãå ´åã¯null
ã«ãããã³ã°ãããã¨æããã°map
ã§ä»£æ¿å¯è½ã§ããä¾ãã°filter
ãããå®è£
ãããã¨ãã§ãããã
public Optional<T> filter(Predicate<? super T> predicate) { return map(v -> predicate.test(v) ? v : null); }
ãªããfilter
ãmap
ã¯isPresent
ã®ã¨ãã«ããã£ã¨ç»å ´ããã¦ãããã¾ããå¼ã£ããã£ã¦ãã¾ã£ã¦ããããããªããã
ã¾ã¨ã
- çæã¯
ofNullable
ã§ã ãããè³ãããã©ã3ã¤ã¨ã使ãåãããã - ãªãã§ãããã§ã
Optional
ã«ããªããget
,orElseThrow
ã¯ãªãã¹ãé¿ããããorElse
,orElseGet
ã¯å¼æ°ã«ä½¿ããããªãã
- 大æµã¯
orElse
ãifPresent
ã®ã©ã¡ãã§å¦çããããé¸ã¶ã isPresent
ã¯è¦ããªãåã§ã¯ãªããmap
ãfilter
ã使ããªãã¨å¤æå°çã«ãªãã
Optional
ã¯æåéãã«ãä»»æãããé¸æããæå³ããç©ã§ããã使ç¨è
ã¯ã©ãããããæèãã¦é¸æããå¿
è¦ãããã¾ããã¾ããé¸æãã¦æ¬²ããã¨è¨ãæå³ãã³ã¼ãã«è¾¼ãããããªãã¾ãããOptional
ãè¦æ±ãã/è¦æ±ããããã¨ãã©ã®ãããªæå³ãæã¤ããæèããªãã使ã£ã¦ããã°ãå¤ãªæ¹ã«ã¯è¡ããªããããç¡ãããªã¨æãã¾ãã