MapleStory Finger Point

๐ŸŸค JAVA/๐ŸŸค JAVA ๊ฐœ๋…์ •๋ฆฌ

[JAVA 8+] Optional ๊ฐ์ฒด

HYEJU01 2024. 12. 7. 12:13

https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html

 

Optional (Java Platform SE 8 )

A container object which may or may not contain a non-null value. If a value is present, isPresent() will return true and get() will return the value. Additional methods that depend on the presence or absence of a contained value are provided, such as orEl

docs.oracle.com

 

 

 

Optional <T> ๊ฐ์ฒด๋ž€? 

Java 8 ์—์„œ ๋„์ž…๋œ Wrapper ํด๋ž˜์Šค์ด๋‹ค. 

null ๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๋Š”๋ฐ  ๋„์›€์„ ์ค€๋‹ค.  (NPE ๋ฅผ ๋ฐฉ์ง€ํ•œ๋‹ค. `NullPointException`)

 

 

  • ๊ฐ’์ด ์—†์„ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์— Optional ์‚ฌ์šฉํ•˜์—ฌ ํ‘œํ˜„๊ฐ€๋Šฅ
  • `๊ฒฐ๊ณผ ์—†์Œ` , `๋นˆ๊ฒฐ๊ณผ` ๋ฅผ ๋ช…ํ™•ํžˆ ํ‘œํ˜„ํ•˜๋Š” ์šฉ๋„
    (์ด์ „์—” null ์„ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ null ์„ ์‚ฌ์šฉํ•˜๋ฉด NPE ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด์„œ ํ”„๋กœ๊ทธ๋žจ์ด ์ฃฝ์–ด๋ฒ„๋ฆผ)
  • JPA ์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค 

 

 

Optional ์ฃผ์˜์‚ฌํ•ญ

  • Optional ๋ณ€์ˆ˜์—๋Š” null ํ• ๋‹น ํ•˜์ง€ ์•Š๊ธฐ
  • Optional ์„ ํด๋ž˜์Šค ํ•„๋“œ ํƒ€์ž…์œผ๋กœ ์‚ฌ์šฉ ํ•˜์ง€ ์•Š๊ธฐ -> ๊ฐ์ฒด ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉ ์ด ์˜ณ์Œ
  • ๋ฉ”์„œ๋“œ ์ธ์ž๋กœ Optional ์‚ฌ์šฉ ํ•˜์ง€ ์•Š๊ธฐ -> ๊ฐ์ฒด ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉ์ด ์˜ณ์Œ
  • ๋นˆ ์ปฌ๋ ‰์…˜, ๋ฐฐ์—ด์— OPtional ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ธฐ -> ๋นˆ ์ปฌ๋ ‰์…˜์„ ๋ฆฌํ„ดํ•˜๋Š”๊ฒŒ ์˜ณ์Œ
  • ์›์‹œํƒ€์ž… ๊ฐ’์€ Optional ํด๋ž˜์Šค๊ฐ€ ์•„๋‹Œ OptionalInt , OptionalLong ... ํด๋ž˜์Šค ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ์„ฑ๋Šฅ๋ช…์—์„œ ์ข‹๋‹ค.->๋ฐ•์‹ฑ ์–ธ๋ฐฉ์‹์ด ์ผ์–ด๋‚˜๋ฉด์„œ ์„ฑ๋Šฅ ์ €ํ•˜ 

 

 

 

 

 

Optional ์‚ฌ์šฉ

 

Optional<String> optional = Optional.empty();
Optional<String> opt = Optional.of("hello");
String temp = null;
Optional<String> opt = Optional.ofNullable(temp);

 

 

 

Optional ๊ฐ์ฒด๊ฐ€ ์ œ๊ณตํ•ด์ฃผ๋Š” ๋ฉ”์„œ๋“œ

 

  • empty () `public static <T> Optional<T> empty()`
Optional<String> emptyOptional = Optional.empty();

 

๋นˆ `Optional` ๊ฐ์ฒด ๋ช…์‹œ์  ์ƒ์„ฑ

 

  • of ()  `public static <T> Optional<T> of (T value)`
Optional <String> optionalValue = Optional.of("hello");

 

null ๊ฐ’์ด ์•„๋‹ˆ๋ผ๊ณ  ํ™•์‹ ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

null ์„ ๋ฐ›์œผ๋ฉด NPE ๋ฐœ์ƒ

 

  • ofNullable ()
Optional<String> optionalValue = Optional.ofNullable(null);
Optional<String> optionalValue2 = Optional.ofNullable("World");

๊ฐ’์ด null ์ผ ์ˆ˜๋„ ์žˆ์„ ๋•Œ ์‚ฌ์šฉ

๊ฐ’์ด null ์ด๋ฉด `๋นˆ Optional ๊ฐ์ฒด`๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

 

  • get () `public T get()`
Optional<String> optional = Optional.of("Hello");
System.out.println(optional.get()); // "Hello"

// Optional<String> emptyOptional = Optional.empty();
// System.out.println(emptyOptional.get()); // ์˜ˆ์™ธ ๋ฐœ์ƒ

 

๊ฐ’์ด ์กด์žฌํ•˜๋ฉด ๊ฐ’์„ ๋ฐ˜ํ™˜

์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด `NoSuchElementException` ๋ฐœ์ƒ

!! ๊ฐ’์ด ํ•ญ์ƒ ์กด์žฌํ•œ๋‹ค๊ณ  ํ™•์‹คํ• ๋•Œ๋งŒ ์‚ฌ์šฉํ•œ๋‹ค. !!

 

    • isPresent () `public boolean isPresent()`

Optional<String> optional = Optional.of("Test");
System.out.println(optional.isPresent()); // true

Optional<String> emptyOptional = Optional.empty();
System.out.println(emptyOptional.isPresent()); // false

 

๊ฐ’์ด ์กด์žฌํ•˜๋ฉด true , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด false

 

 

 

  • ifPresent () `public void ifPresent(Consumer<? super T> consumer`
Optional<String> optional = Optional.of("Hello");
optional.ifPresent(value -> System.out.println("Value: " + value)); // "Value: Hello"

 

๊ฐ’์ด ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ ์ œ๊ณต๋œ Consumer ์‹คํ–‰

 

  • filter()
    `public Optional<T> filter(Predicate<? super T> predicate)

๊ฐ’์ด ์กด์žฌํ•˜๊ณ  ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฉด Optional ๋ฐ˜ํ™˜

์•„๋‹ˆ๋ผ๋ฉด ๋นˆ Optonal ๋ฐ˜ํ™˜

 

  • map() `public <U> Optional<U> map(Function<? super T,? extends U> mapper)`

๊ฐ’์ด ์กด์žฌํ•˜๋ฉด ๋งคํ•‘ํ•จ์ˆ˜ (Function) ์ ์šฉ ๊ฒฐ๊ณผ `์ผ๋ฐ˜ ๊ฐ’ or ๊ฐ์ฒด`๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ 

์ตœ์ข…์ ์œผ๋กœ๋Š” Optional ๋กœ ๊ฐ์‹ธ์ ธ์„œ ๋ฐ˜ํ™˜

 

  • flatMap 

๋งคํ•‘ํ•จ์ˆ˜๊ฐ€  Optional ์„ ๋ฐ˜ํ™˜ํ• ๋•Œ ์‚ฌ์šฉ (๋งคํ•‘ํ•จ์ˆ˜ ๊ฒฐ๊ณผ๋ถ€ํ„ฐ Optional ์ž„)

Optional<<>> ์€ ์ค‘์ฒฉ๋˜์ง€ ์•Š๊ณ  Optional<R> ๋กœ ๋งŒ๋“ค์–ด์ง

 

 

  • orElse() `public T orElse(T other)`

๊ฐ’์ด ์กด์žฌํ•˜๋ฉด ๊ฐ’์„ ๋ฐ˜ํ™˜ , ์•„๋‹ˆ๋ฉด ๊ธฐ๋ณธ๊ฐ’ ๋ฐ˜ํ™˜

 

 

  • orElseGet() `public T orElseGet(Supplier<? extends T> other)`

๊ฐ’์ด ์กด์žฌํ•˜๋ฉด ๊ฐ’์„ ๋ฐ˜ํ™˜ , ์•„๋‹ˆ๋ฉด `Supplier` ์‚ฌ์šฉํ•ด ๋™์  ๊ฐ’ ์ƒ์„ฑ

 

  • orElseThrow()
    `public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X`

๊ฐ’์ด ์กด์žฌํ•˜์ง€์•Š์œผ๋ฉด ์‚ฌ์šฉ์ž์ •์˜ ์˜ˆ์™ธ ๋ฐœ์ƒ

 

 

  • equals()
    `public boolean equals(Object obj)`

 

  • hashCode()
    `public int hashCode()`

 

  • toString()
    `public String toString()`