λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
Study/Java

[TIL] 슀트림

by hong- 2022. 5. 21.

πŸ’ͺ🏻 슀트림 Stream

 - λ°μ΄ν„°μ˜ 흐름

 - λ°°μ—΄κ³Ό μ»¬λ ‰μ…˜μ„ ν•¨μˆ˜ν˜•μœΌλ‘œ 처리

 - λ°°μ—΄ λ˜λŠ” μ»¬λ ‰μ…˜ μΈμŠ€ν„΄μŠ€μ— ν•¨μˆ˜ μ—¬λŸ¬ 개λ₯Ό μ‘°ν•©ν•΄μ„œ μ›ν•˜λŠ” κ²°κ³Όλ₯Ό ν•„ν„°λ§ν•˜κ³  κ°€κ³΅λœ κ²°κ³Όλ₯Ό 얻을 수 있음

 - μ½”λ“œμ˜ 양을 쀄이고 κ°„κ²°ν•˜κ²Œ ν‘œν˜„ν•  수 있음 = μ €μž₯곡간 ν•„μš” μ—†μŒ

 - λ°°μ—΄, μ»¬λ ‰μ…˜μ˜ μ €μž₯ μš”μ†Œλ₯Ό ν•˜λ‚˜μ”© μ°Έμ‘°ν•΄μ„œ λžŒλ‹€μ‹μœΌλ‘œ μ²˜λ¦¬ν•  수 μžˆλ„λ‘ ν•΄μ£ΌλŠ” 반볡자

 - λ‹€μ–‘ν•œ 데이터 μ†ŒμŠ€λ₯Ό λ‹€λ£¨λŠ” ν’λΆ€ν•œ λ©”μ„œλ“œ 제곡


πŸ“ 슀트림 νŠΉμ§•

1️⃣  μ„ μ–Έν˜•

  - μ„ μ–Έν˜• ν”„λ‘œκ·Έλž˜λ° ? μ–΄λ–»κ²Œ λ³΄λ‹€λŠ” 무엇을 μˆ˜ν–‰ν•˜λŠ”μ§€ 관심을 λ‘λŠ” ν”„λ‘œκ·Έλž˜λ°

  - μ„ μ–Έν˜• λ°©μ‹μœΌλ‘œ μž‘μ„±ν•˜λ©΄ λ‚΄λΆ€ λ™μž‘ 원리λ₯Ό λͺ°λΌλ„ μ½”λ“œκ°€ 무슨 일 ν•˜λŠ”μ§€ 이해 κ°€λŠ₯

2️⃣  λžŒλ‹€μ‹μœΌλ‘œ μš”μ†Œ 처리 μ½”λ“œλ₯Ό 제곡

- 슀트림이 μ œκ³΅ν•˜λŠ” λŒ€λΆ€λΆ„μ˜ μš”μ†Œ 처리 λ©”μ†Œλ“œλŠ” ν•¨μˆ˜μ  μΈν„°νŽ˜μ΄μŠ€ λ§€κ°œνƒ€μž…μ„ 가짐

- λ§€κ°œκ°’μœΌλ‘œ λžŒλ‹€μ‹ λ˜λŠ” λ©”μ†Œλ“œ μ°Έμ‘°λ₯Ό λŒ€μž…ν•  수 있음

stream.forEach( x -> {
    String id = x.getId();
    int pw = x.getPw();
    System.out.println("idλŠ” " + id + "pwλŠ” " + pw);
});

3️⃣  λ‚΄λΆ€ 반볡자λ₯Ό μ‚¬μš©ν•˜μ—¬ 병렬 μ²˜λ¦¬κ°€ 쉬움

  πŸ’‘ μ™ΈλΆ€ 반볡자 ? κ°œλ°œμžκ°€ μ½”λ“œλ‘œ 직접 μ»¬λ ‰μ…˜μ˜ μš”μ†Œλ₯Ό λ°˜λ³΅ν•΄μ„œ κ°€μ Έμ˜€λŠ” νŒ¨ν„΄

    → forλ¬Έ, while λ¬Έ λ“±

 

  πŸ’‘ λ‚΄λΆ€ 반볡자 ? μ»¬λ ‰μ…˜ λ‚΄λΆ€μ—μ„œ μš”μ†Œλ“€μ„ λ°˜λ³΅μ‹œν‚€κ³  κ°œλ°œμžλŠ” μ²˜λ¦¬ν•΄μ•Ό ν•  μ½”λ“œλ§Œ μ œκ³΅ν•˜λŠ” νŒ¨ν„΄

    → κ°œλ°œμžλŠ” μš”μ†Œ 처리 μ½”λ“œμ—λ§Œ 집쀑 (μ»¬λ ‰μ…˜μ΄ μš”μ†Œ 반볡 λ‹΄λ‹Ή)

    → μš”μ†Œλ“€μ„ λΆ„λ°°μ‹œμΌœ 병렬 처리 μž‘μ—… κ°€λŠ₯

 

  πŸ’‘ λ³‘λ ¬ 처리 ? ν•œ κ°€μ§€ μž‘μ—…μ„ μ„œλΈŒ μž‘μ—…μœΌλ‘œ λ‚˜λˆ„μ–΄ λΆ„λ¦¬λœ μŠ€λ ˆλ“œμ—μ„œ λ³‘λ ¬μ μœΌλ‘œ 처리

4️⃣  쀑간 μ—°μ‚°κ³Ό μ΅œμ’… μ—°μ‚° κ°€λŠ₯

  - 쀑간 μ—°μ‚° : λ§€ν•‘, 그루핑, 필터링, μ •λ ¬

  - μ΅œμ’… μ—°μ‚° : 반볡, μΉ΄μš΄νŒ…, 평균, 총합, μ΅œλŒ€/μ΅œμ†Œκ°’

double sum = list.stream()
                .mapToInt(Age:: getAge)
                .sum()
                .getAsDouble();
    System.out.println("합계: " + sum);

πŸ“ νŒŒμ΄ν”„λΌμΈ ꡬ성 (.)

  - νŒŒμ΄ν”„λΌμΈ ? μ—¬λŸ¬ 개의 슀트림이 μ—°κ²°λ˜μ–΄ μžˆλŠ” ꡬ쑰

  - νŒŒμ΄ν”„λΌμΈμ—μ„œ μ΅œμ’… 연산을 μ œμ™Έν•˜κ³ λŠ” λͺ¨λ‘ 쀑간 μ—°μ‚° 슀트림 !

  - μ΅œμ’…μ—°μ‚°μ΄ μ‹€ν–‰λ˜μ–΄μ•Ό 쀑간 μŠ€νŠΈλ¦Όμ—μ„œ μ—°μ‚° μ‹œμž‘


πŸ‘πŸ» 슀트림 생성 및 μ—°μ‚° κ³Όμ •

πŸ“ 슀트림 생성 

Stream<String> Streamλͺ… = list.stream();  (1) 리슀트 슀트림
Stream<String> Streamλͺ… = Arrys.stream(~); (2) λ°°μ—΄ 슀트림
Stream<String> Streamλͺ… = Stream.of(~);    (3) κ°€λ³€ λ°°μ—΄ 슀트림

  - stream( )을 μ‚¬μš©ν•˜λ©΄ ν•΄λ‹Ή μ»¬λ ‰μ…˜ 객체λ₯Ό Stream으둜 λ°˜ν™˜

  - μ»¬λ ‰μ…˜ μΈν„°νŽ˜μ΄μŠ€μ—λŠ” stream()이 이미 μ •μ˜λ˜μ–΄μžˆμŒ

1️⃣ μ»¬λ ‰μ…˜ 슀트림

package Stream;
import java.util.*;
import java.util.stream.Stream;

public class liststream {
    public static void main(String[] args) {
        List<String> ls = Arrays.asList("One", "Two", "Three");
        Stream<String> lsStream = ls.stream();
        lsStream.forEach(System.out::println);
    }
}

 - μ»¬λ ‰μ…˜ νƒ€μž…(Collection, List, Set)의 경우 stream λ©”μ†Œλ“œλ₯Ό μ΄μš©ν•΄μ„œ λ§Œλ“€ 수 있음

2️⃣ λ°°μ—΄ 슀트림

package Stream;
import java.util.*;
import java.util.stream.Stream;

public class ArrayStream {
    public static void main(String[] args) {
        String[] arr = new String[]{"ν•˜λ‚˜","λ‘˜","μ…‹"};

        Stream<String> arrStream = Arrays.stream(arr);
        arrStream.forEach(x -> System.out.print(x + " "));

        System.out.println();

        Stream<String> arrSteam2 = Arrays.stream(arr,1,2);
        arrSteam2.forEach(y -> System.out.print(y + " "));

        System.out.println();
        
        Stream<Double> st = Stream.of(4.3,2.4,2.9);
        st.forEach(System.out::println);
    }
}
//κ²°κ³Ό
ν•˜λ‚˜ λ‘˜ μ…‹ 
λ‘˜ 
4.3
2.4
2.9

  πŸ’‘ Array.stream

     - Array.stream(arr)을 μ‚¬μš©ν•  수 도 있고 λΆ€λΆ„ 문자 좜λ ₯을 μ›ν•˜λ©΄ Arrays.strema(arr,1,2)처럼 μ‚¬μš©ν•˜λ©΄ 됨

 πŸ’‘ forEach( )

     - ν•΄λ‹Ή 슀트림의 μš”μ†Œλ₯Ό ν•˜λ‚˜μ”© μ†Œλͺ¨ν•΄κ°€λ©° 순차적으둜 μš”μ†Œμ— μ ‘κ·Όν•˜λŠ” λ©”μ†Œλ“œ

  πŸ’‘ stream.of

     - Stream 클래슀의 of λ©”μ†Œλ“œλŠ” κ°€λ³€ λ§€κ°œλ³€μˆ˜λ₯Ό 전달받아 μŠ€νŠΈλ¦Όμ„ 생성할 수 있음


β–ͺ️ μ»¬λ ‰μ…˜μœΌλ‘œλΆ€ν„° 슀트림 생성

List<Integer> μ°Έμ‘°λ³€μˆ˜ = Arrays.asList(1,2,3,4,5);
Stream<Integer> st = ls.stream();

β–ͺ️ λ°°μ—΄λ‘œλΆ€ν„° 슀트림 생성

Stream<String> stream = Arrays.stream(T[])
Stream<String> stream = Arrays.stream(int[])
Stream<String> stream = Arrays.stream(long[])
Stream<String> stream = Arrays.stream(double[])

Stream<String> stream = Stream.of(T[])
Stream<String> stream = IntStream.of(int[])
Stream<String> stream = LongStream.of(long[])
Stream<String> stream = DoubleStream.of(double[])

β–ͺ️ νŠΉμ • λ²”μœ„μ˜ μ •μˆ˜λ₯Ό μš”μ†Œλ‘œ κ°–λŠ” 슀트림 생성

IntStream intstream = IntStream.range(1,5)
IntStream intstream = IntStream.rangeClosed(1,5)

LongStream longstream = LongStream.range(1,5)
LongStream longstream = LongStream.rangeClosed(1,5)

 


πŸ‘‹πŸ» 쀑간 μ—°μ‚°

 - 쀑간연산은 μ—°μ‚° κ²°κ³Όλ₯Ό 슀트림으둜 λ°˜ν™˜ν•˜κΈ° λ•Œλ¬Έμ— μ—°μ†ν•΄μ„œ μ—¬λŸ¬ 번 μˆ˜ν–‰ν•  수 있음

πŸ“ 필터링

   πŸ”Ž distinct( )

    : 슀트림의 μš”μ†Œλ“€μ— μ€‘λ³΅λœ 데이터 제거

   πŸ”Ž filter( )

    : 쑰건에 λ§žλŠ” λ°μ΄ν„°λ§Œ μ •μ œν•˜μ—¬ μž‘μ€ μ»¬λ ‰μ…˜μ„ λ§Œλ“¦

List<String> sick = Arrays.asList("두톡","κ·Όμœ‘ν†΅","κ³ μ—΄","볡톡","κ·Όμœ‘ν†΅");
sick.stream()
            .distinct()
            .forEach(n -> System.out.println(n));

sick.stream()
            .filter( x -> x.startsWith("두"))
            .forEach(x-> System.out.println(x));

    πŸ’‘ startsWith(체크할 λ¬Έμžμ—΄)

     - 체크할 λ¬Έμžμ—΄λ‘œ μ‹œμž‘ν•˜λŠ”μ§€ 확인

     - a.startsWith("JAVA")일 λ•Œ, λ¬Έμžμ—΄μ΄ JAVA둜 μ‹œμž‘ν•œλ‹€λ©΄ True !

    πŸ’‘ endsWith(체크할 λ¬Έμžμ—΄)

     - 체크할 λ¬Έμžμ—΄λ‘œ λλ‚˜λŠ”μ§€ 확인

     - b.endsWith("Programming")일 λ•Œ 끝 λ¬Έμžμ—΄κ³Ό μΌμΉ˜ν•œλ‹€λ©΄ True !


πŸ“ λ§€ν•‘

   πŸ”Ž map( )

    : μƒˆλ‘œμš΄ μŠ€νŠΈλ¦Όμ„ ν˜•μ„±ν•˜λŠ” κ²ƒμœΌλ‘œ 기쑴의 슀트림 μš”μ†Œλ“€μ„ λŒ€μ²΄ν•˜λŠ” μš”μ†Œ

     - 일반적인 Stream 객체λ₯Ό μ›μ‹œ Stream으둜 λ°”κΎΈκ±°λ‚˜ κ·Έ λ°˜λŒ€ μž‘μ—…μ΄ ν•„μš”ν•œ κ²½μš°μ— μ‚¬μš©

     - 슀트림의 μŠ€νŠΈλ¦Όμ„ λ°˜ν™˜ !!!! return : Stream <Stream>

    → map 이외에도 mapToInt(), mapToLong, mapToDouble 

List<String> coffee = Arrays.asList("americano", "caffe latte" ,"cappuccino");
coffee.stream()
             .map(x -> x.toLowerCase())
             .forEach(x->System.out.println(x));

    πŸ’‘ flatMap( )

     - μš”μ†Œλ₯Ό λŒ€μ²΄ν•˜λŠ” 볡수 개의 μš”μ†Œλ“€λ‘œ κ΅¬μ„±λœ μƒˆλ‘œμš΄ 슀트림 리턴

     - 초기 μƒμ„±λœ 슀트림 ν˜•νƒœκ°€ 배열인 경우, λͺ¨λ“  μ›μ†Œλ₯Ό 단일 슀트림으둜 λ°˜ν™˜ ν•  수 있음

     - 슀트림 λ°˜ν™˜ !!!! return : Stream


πŸ“ μ •λ ¬

   πŸ”Ž sorted( )

    : 슀트림의 μš”μ†Œλ“€μ„ μ •λ ¬

     - μ˜€λ¦„μ°¨μˆœ : sorted( ) 

     - λ‚΄λ¦Όμ°¨μˆœ : sorted(Comparator.reverseOrder( ))

List<String> person = Arrays.asList("Hand","Foot","Body","Face");

person.stream()
            .sorted(Comparator.reverseOrder())
            .forEach(x->System.out.println(x));

 


πŸ“ μ—°μ‚° κ²°κ³Ό 확인

   πŸ”Ž peek( )

    : 쀑간 μ—°μ‚° λ©”μ„œλ“œλ‘œ, μš”μ†Œλ₯Ό ν•˜λ‚˜μ”© λŒλ©΄μ„œ 좜λ ₯

    - ν•˜λ‚˜μ˜ μŠ€νŠΈλ¦Όμ— μ—¬λŸ¬ 번 μ‚¬μš©ν•  수 있음

    - 주둜 μ—°μ‚° 쀑간에 κ²°κ³Όλ₯Ό ν™•μΈν•˜μ—¬ λ””λ²„κΉ…ν•˜κ³ μž ν•  λ•Œ μ‚¬μš©

      * 디버깅 ? 였λ₯˜λ‚˜ 버그λ₯Ό μ°Ύμ•„λ‚΄κ³  μˆ˜μ •ν•˜λŠ” μž‘μ—…

   πŸ”Ž forEach( )

    : μ΅œμ’… μ—°μ‚° λ©”μ„œλ“œλ‘œ, μš”μ†Œλ₯Ό ν•˜λ‚˜μ”© λŒλ©΄μ„œ 좜λ ₯

    - ν•œ 번만 ν˜ΈμΆœκ°€λŠ₯ (μž¬ν˜ΈμΆœν•˜λ €λ©΄ μƒˆλ‘œμš΄ 슀트림 생성해야 함)

 


πŸ‘‹πŸ»  μ΅œμ’… μ—°μ‚°

 - μ—°μ‚° κ²°κ³Όλ₯Ό ν•œ 번만 μ‹€ν–‰ κ°€λŠ₯

πŸ“ μ—°μ‚° κ²°κ³Ό 확인

   πŸ”Ž forEach( )

    : νŒŒμ΄ν”„λΌμΈ λ§ˆμ§€λ§‰μ—μ„œ μš”μ†Œλ₯Ό ν•˜λ‚˜μ”© μ—°μ‚°

      - 리턴 값이 μ—†λŠ” 이메일 λ°œμ†‘, μŠ€μΌ€μ€„λ§ μž‘μ—…μ—μ„œλ„ μ‚¬μš©λ¨


πŸ“ λ§€μΉ­ 

   πŸ”Ž match( )

    : 슀트림의 μš”μ†Œλ“€μ΄ νŠΉμ •ν•œ 쑰건을 μΆ©μ‘±ν•˜λŠ”μ§€ κ²€μ‚¬ν•˜κ³  싢은 경우 μ‚¬μš©

     - ν•¨μˆ˜ν˜• μΈν„°νŽ˜μ΄μŠ€λ₯Ό λ°›μ•„ ν•΄λ‹Ή 쑰건을 λ§Œμ‘±ν•˜λŠ”μ§€ κ²€μ‚¬ν•˜κ³  true/false둜 λ°˜ν™˜

      1️⃣ allMatch( ) 

       : λͺ¨λ“  μš”μ†Œλ“€μ΄ λ§€κ°œκ°’μœΌλ‘œ μ£Όμ–΄μ§„ 쑰건에 λ§Œμ‘±ν•˜λŠ”μ§€ 쑰사

      2️⃣ anyMatch( ) 

       : μ΅œμ†Œν•œ ν•œ 개의 μš”μ†Œκ°€ λ§€κ°œκ°’μœΌλ‘œ μ£Όμ–΄μ§„ 쑰건을 λ§Œμ‘±ν•˜λŠ”μ§€ 쑰사

      3️⃣ noneMatch( ) 

       : λͺ¨λ“  μš”μ†Œλ“€μ΄ λ§€κ°œκ°’μœΌλ‘œ μ£Όμ–΄μ§„ 쑰건을 λ§Œμ‘±ν•˜μ§€ μ•ŠλŠ”μ§€ 쑰사

public class matchTest {
    public static void main(String[] args) {
        int[] num = { 1, 3, 5};
        boolean result = Arrays.stream(num).allMatch(x->x%2==1);
        System.out.println("ν™€μˆ˜ μΈκ°€μš” ? " + result);

        result = Arrays.stream(num).anyMatch(x->x%3==0);
        System.out.println("3의 λ°°μˆ˜κ°€ μžˆλ‚˜μ—¬? "+result);

        result = Arrays.stream(num).noneMatch(x-> x%3==0);
        System.out.println("3의 λ°°μˆ˜κ°€ μ—†λ‚˜μš”? " + result);
    }
        }

πŸ“ κΈ°λ³Έ 집계

   πŸ”Ž sum( ), count( ), average( ), max( ), min( )

    : μš”μ†Œλ“€μ„ 합계, μΉ΄μš΄νŒ…, 평균, μ΅œλŒ€, μ΅œμ†Œ κ°’ λ“±μœΌλ‘œ μ—°μ‚°ν•˜μ—¬ ν•˜λ‚˜μ˜ κ°’μœΌλ‘œ μ‚°μΆœ

int[] arr = { 1, 2, 3, 4, 5};

long count = Arrays.stream(arr).count();

long sum = Arrays.stream(arr).sum();

double avg = Arrays.stream(arr).average().getAsDouble();

int max = Arrays.stream(arr).max().getAsInt();

 

   πŸ”Ž reduce

    : λˆ„μ ν•˜μ—¬ ν•˜λ‚˜λ‘œ μ‘μΆ•ν•˜λŠ” λ°©μ‹μœΌλ‘œ λ‹€μ–‘ν•œ 집계 결과물을 λ§Œλ“€ 수 있음

.reduce(0, (a,b) -> a + b )
(1) Accumulator : 각 μš”μ†Œλ₯Ό κ³„μ‚°ν•œ 쀑간 κ²°κ³Όλ₯Ό μƒμ„±ν•˜κΈ° μœ„ν•΄ μ‚¬μš©
(2) Identity : 계산을 μˆ˜ν–‰ν•˜κΈ° μœŸλ‚˜ μ΄ˆκΈ°κ°’
(3) Combiner : 병렬 μŠ€νŠΈλ¦Όμ—μ„œ λ‚˜λˆ„μ–΄ κ³„μ‚°λœ κ²°κ³Όλ₯Ό ν•˜λ‚˜λ‘œ ν•©μΉ˜κΈ° μœ„ν•œ 둜직

   πŸ”Ž Collect( )

    : 슀트림의 μš”μ†Œλ“€μ„ λ‹€λ₯Έ μ’…λ₯˜μ˜ 결과둜 μƒˆλ‘­κ²Œ μˆ˜μ§‘ν•˜κ³  싢을 경우 μ‚¬μš©

     - Collector 맀개 값은 μ–΄λ–€ μš”μ†Œλ₯Ό μ–΄λ–€ μ»¬λ ‰μ…˜μ— μˆ˜μ§‘ν•  것인지λ₯Ό λ‚˜νƒ€λƒ„ 

.collect(Collectors.toList())
.collect(Collectors.toSet())
.collect(Collectors.toMap())
.collect(Collectors.toCollection(~))

🀟🏻  Optional <T>

public final class Optional<T> {

	private final T vale;
    
 }

 : Null이 올 수 μžˆλŠ” 값을 κ°μ‹ΈλŠ” wrapper 클래슀 

- NPE(NullPointerException) : null κ°’μœΌλ‘œ 인해 μ—λŸ¬κ°€ λ°œμƒν•˜λŠ” ν˜„μƒ

 - Optional은 NPEκ°€ λ°œμƒν•˜μ§€ μ•Šλ„λ‘ μ—°μ‚°κ²°κ³Όλ₯Ό κ°€μ§€κ³  μžˆλ‹€κ°€ λ°˜ν™˜

 

  πŸ‘‰πŸ» λ©”μ„œλ“œκ°€ λ°˜ν™˜ν•  κ²°κ³Ό 값이 μ—†μŒμ„ λͺ…ν™•νžˆ ν‘œν˜„ν•  ν•„μš”κ°€ 있고

       null을 λ°˜ν™˜ν•˜λ©΄ μ—λŸ¬κ°€ λ°œμƒν•  κ°€λŠ₯성이 높은 μƒν™©μ—μ„œ Optional μ‚¬μš©ν•˜λŠ” 것이 λͺ©μ 

 

1️⃣ orElse() / orElseGet() orElseThrow() μ‚¬μš©

  : 값이 null 일 λ•Œ 이듀을 ν™œμš©ν•˜μ—¬ λ””ν΄νŠΈ κ°’ μ§€μ • κ°€λŠ₯

.orElse(getId())
.orElseGet(this::getId)

  - orElse() : νŒŒλΌλ―Έν„°λ‘œ 값을 λ°›μŒ & 값이 미리 μ‘΄μž¬ν•˜λŠ” 경우 μ‚¬μš©

  - orElseGet() : νŒŒλΌλ―Έν„°λ‘œ ν•¨μˆ˜ν˜• μΈν„°νŽ˜μ΄μŠ€λ₯Ό λ°›μŒ & 값이 미리 μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” 경우 μ‚¬μš©

2️⃣ of()와 ofNullable()

Optional<String> one = Optional.ofNullable("123");
Optional<String> two = Optinal.of("123");

 - of()λŠ” xκ°€ null이 μ•„λ‹˜μ΄ ν™•μ‹€ν•  λ•Œλ§Œ μ‚¬μš©

 - ofNullable()은 xκ°€ null일 μˆ˜λ„ μžˆμ„λ•Œλ§Œ μ‚¬μš©

3️⃣ empty()

Optional<String> three = Optional.empty();

  - μ°Έμ‘° λ³€μˆ˜λ₯Ό κΈ°λ³Έκ°’μœΌλ‘œ μ΄ˆκΈ°ν™”ν•  λ•Œ μ‚¬μš©

  - Optional은 Wrapper클래슀 이기 λ•Œλ¬Έμ— 빈 값이 올 수 있음

 

4️⃣ get()

  - Optional 객체에 μ €μž₯된 값을 κ°€μ Έμ˜€λŠ” λ©”μ†Œλ“œ

  - 값이 null 일 λ•Œ orElse() 등을 ν™œμš©ν•˜μ—¬ λ””ν΄νŠΈ κ°’ μ§€μ • κ°€λŠ₯

 

'Study > Java' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

[TIL] μŠ€λ ˆλ“œ  (0) 2022.05.22
[TIL] 파일 μž…μΆœλ ₯  (0) 2022.05.21
[TIL] λžŒλ‹€  (0) 2022.05.20
[TIL] μ—λ„ˆν…Œμ΄μ…˜  (0) 2022.05.18
[TIL] enum  (0) 2022.05.18