rosieblue
article thumbnail
728x90

Optional์„ ๋”ฐ๋กœ ๊ณต๋ถ€ํ•œ ์ ์ด ์—†์–ด์„œ ๋Œ€์ถฉ ์˜ค๋ฅ˜๋งŒ ๋ดค์„ ๋•Œ ์—ฌํƒœ๊ป isPresent์ด๋ž‘ ifPresent๊ฐ€ ๊ฐ™์€ ์• ์ธ์ค„ ์•Œ์•˜๋‹ค ..

๋ฐ”๋ถ€ ๊ทธ๋ž˜์„œ isPresent()์•ˆ์— ์ธ์ž๋กœ ์™œ ๋žŒ๋‹ค์‹์„ ์จ์ฃผ๋Š”์ง€ ์ดํ•ด๊ฐ€์•ˆ๋๋‹ค..ํ•˜์ง€๋งŒ ๋‹ค๋ฅธ ๋ฉ”์†Œ๋“œ์˜€๋‹ค๋Š”๊ฒƒ ใ…‹.. ํฌ์ŠคํŠธ๊ฐ€ ๋„ˆ๋ฌด ์งง์Œ ์ฃผ์˜

์•”ํŠผ ์˜ค๋Š˜์€ Optional์ด ๋ญ”์ง€ ์•„์ฃผ ์งง๊ฒŒ ์‚ดํŽด๋ณด๊ณ  isPresent/ifPresent/ifPresentOrElse ๋ฉ”์†Œ๋“œ์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค. 

 

Optional์ด๋ž€?

์กด์žฌํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ ์•ˆ ํ•  ์ˆ˜๋„ ์žˆ๋Š” ๊ฐ์ฒด,

์ฆ‰, “null์ด ๋  ์ˆ˜๋„ ์žˆ๋Š” ๊ฐ์ฒด”์„ ๊ฐ์‹ธ๊ณ  ์žˆ๋Š” ์ผ์ข…์˜ ๋ž˜ํผ ํด๋ž˜์Šค์ด๋‹ค.

์›์†Œ๊ฐ€ ์—†๊ฑฐ๋‚˜ ์ตœ๋Œ€ ํ•˜๋‚˜ ๋ฐ–์— ์—†๋Š” Collection์ด๋‚˜ Stream์ด๋ผ๊ณ  ์ƒ๊ฐํ•ด๋„ ์ข‹๋‹ค.

null ๋•Œ๋ฌธ์— ์ƒ๊ธฐ๋Š” ๊ท€์ฐฎ์Œ์„ ํ•ธ๋“ค๋งํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“ค์–ด์กŒ๋‹ค๊ณ  ํ•œ๋‹ค.

 

ifPresent ๋ฉ”์†Œ๋“œ

Oracle ๊ณต์‹ ๋ฌธ์„œ ์บก์ณ

๋Œ€์ถฉ ifPresent ์•ˆ์— action์„ ๋„ฃ์–ด์ฃผ๋ฉด presentํ• ๋•Œ ์‹คํ–‰ํ•œ๋‹ค๋Š” ์–˜๊ธฐ๋‹ค. ๋งŒ์•ฝ presentํ•œ๋ฐ action์ด ์ •์˜๋˜์–ด์žˆ์ง€ ์•Š์œผ๋ฉด NullPointerException์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

.ifPresent(person -> System.out.println("์‚ฌ๋žŒ ์กด์žฌํ•ด์šฉ"));

๋Œ€์ถฉ person์ด ์กด์žฌํ•˜๋ฉด ์‚ฌ๋žŒ ์กด์žฌํ•ด์šฉ ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋“œ

 

 

๋‚ด๊ฐ€ ๊ฐ•์˜์—์„œ ์ดํ•ด ์•ˆ๋๋˜ ์ฝ”๋“œ๋Š” ์ด ์•„๋ž˜ ์ฝ”๋“œ์ด๋‹ค. ๊ทธ๋ž˜์„œ ifPresent์— ๋Œ€ํ•ด์„œ ๊ณต๋ถ€ํ•˜๊ฒŒ๋๋‹น

memberRepository.findByName(member.getName())
	.ifPresent(m->{
     	throw new IllegalStateException("์›… ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ํšŒ์›์ž„ใ…‹");
     });

์ด ์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ํ•ด์„ํ•ด๋ณด๋ฉด memberRepository์—์„œ findByName์œผ๋กœ ์–ด๋–ค ์ด๋ฆ„์„ ๊ฐ€์ง„ Optional<Member>๋ฅผ ๋ฝ‘์•„๋‚ด๊ณ , ๋งŒ์•ฝ์— ๋ฉค๋ฒ„๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด ifPresent์— ์˜ํ•ด IllegalStateException์ด throw๋˜๋Š” ๊ฒƒ์ด๋‹ค. (memberRepository๊ฐ€ Optional<Member> ์ด๋ผ๊ณ  ๊ฐ€์ •)

 

ifPresentOrElse ๋ฉ”์†Œ๋“œ

Oracle ๊ณต์‹ ๋ฌธ์„œ ์บก์ฒ˜

์–˜๋Š” ifPresent์™€ ๊ฑฐ์˜ ๋น„์Šทํ•œ๋ฐ ๊ทธ๋ƒฅ Else์ธ ๊ฒฝ์šฐ๊นŒ์ง€ ํ•ธ๋“ค๋งํ•ด์ฃผ๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค.

.ifPresentOrElse(
    person -> System.out.println("์‚ฌ๋žŒ ์žˆ์–ด์œ "),
    () -> System.out.println("์‚ฌ๋žŒ ์—†์–ด์œ ")
  );

person ์กด์žฌ ํ•  ๊ฒฝ์šฐ "์‚ฌ๋žŒ ์žˆ์–ด์œ " ์ถœ๋ ฅ, ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ "์‚ฌ๋žŒ ์—†์–ด์œ " ์ถœ๋ ฅ

 

isPresent ๋ฉ”์†Œ๋“œ

Oracle ๊ณต์‹ ๋ฌธ์„œ ์บก์ฒ˜

๊ทธ๋ƒฅ boolean ๋ฐ˜ํ™˜ํ•˜๋Š” ์• ๋‹ค. ๋”์ด์ƒ ํ• ๋ง ์—†์Œ

 

๋”๋ณด๊ธฐ

์•„๋‹ˆ ์˜ค๋Š˜ ์Šคํ”„๋ง๋ถ€ํŠธ ๋‚จ์€๊ฑฐ ์—…๋กœ๋“œํ•˜๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ ์ค‘๊ฐ„์— ๋ชจ๋ฅด๋Š” ๊ฐœ๋… ์ •๋ฆฌํ•˜๋‹ˆ๊นŒ ๋์ด ์•ˆ๋‚จ ์–ด์นด์ฅฌ....ใ…œ ใ…  ใ… 

 

References

https://www.daleseo.com/java8-optional-after/

 

์ž๋ฐ”8 Optional 2๋ถ€: null์„ ๋Œ€ํ•˜๋Š” ์ƒˆ๋กœ์šด ๋ฐฉ๋ฒ•

Engineering Blog by Dale Seo

www.daleseo.com

https://docs.oracle.com/javase/9/docs/api/java/util/Optional.html#ifPresentOrElse-java.util.function.Consumer-java.lang.Runnable-

 

Optional (Java SE 9 & JDK 9 )

If a value is present, returns the result of applying the given Optional-bearing mapping function to the value, otherwise returns an empty Optional. This method is similar to map(Function), but the mapping function is one whose result is already an Optiona

docs.oracle.com

 

'๐Ÿ‘ฉโ€๐Ÿ’ป Programming & Algorithm > JAVA' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[JAVA] Stream ์ด๋ž€?  (0) 2023.03.14
profile

rosieblue

@Rosieblue

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!