Notice
Recent Posts
Link
Today
Total
10-06 00:17
관리 메뉴

dingdong coding

[ JAVA ] Lambda λ³Έλ¬Έ

🐰JAVA/Basic

[ JAVA ] Lambda

🐢 개발개발 🐾 2022. 6. 14. 17:31

Spring Boot둜 κ°œλ°œν•˜λ˜ 쀑 μžμ£Όμ‚¬μš©ν•˜κ²Œ λ˜λŠ” λžŒλ‹€μ— λŒ€ν•΄ ν•œ 번 ν•™μŠ΅ν•˜κ³  λ„˜μ–΄κ°€κ³ μž μ •λ¦¬ν•˜κ²Œ λ˜μ—ˆλ‹€.

Lambda? 

Anonymous function  즉, 이름이 μ—†λŠ” 읡λͺ…ν•¨μˆ˜λ₯Ό λœ»ν•©λ‹ˆλ‹€.

보톡 λ©”μ„œλ“œκ°€ μ•„λ‹Œ ν•¨μˆ˜λ‘œ 쒅속적이지 μ•Šλ‹€λŠ” νŠΉμ§•μ„ 가지고 μžˆμŠ΅λ‹ˆλ‹€.

 

1. λžŒλ‹€μ‹ 이전 

for (int i = 0; i < 10; i++) {
    System.out.println(i);
}

2. λžŒλ‹€μ‹ μ‚¬μš© 

IntStream.range(0, 10).forEach((int value) -> System.out.println(value));

 

이렇듯 ν•¨μˆ˜ν˜• ν”„λ‘œκ·Έλž˜λ°μΈ 읡λͺ…ν•¨μˆ˜λŠ” 일급객체(first-class-citizen)λΌλŠ” νŠΉμ§•μ„ 가지고 μžˆμŠ΅λ‹ˆλ‹€.

first-class-citizen

  • λ³€μˆ˜λ‚˜ 데이터 κ΅¬μ‘°μ•ˆμ— ν•¨μˆ˜λ₯Ό 담을 수 μžˆμ–΄μ„œ ν•¨μˆ˜μ˜ νŒŒλΌλ―Έν„°λ‘œ 전달할 수 있고, ν•¨μˆ˜μ˜ λ°˜ν™˜κ°’μœΌλ‘œ μ‚¬μš©ν•  수 μžˆλ‹€.
  • 할당에 μ‚¬μš©λœ 이름과 관계없이 κ³ μœ ν•œ ꡬ별이 κ°€λŠ₯ν•˜λ‹€.
  • ν•¨μˆ˜λ₯Ό λ¦¬ν„°λŸ΄λ‘œ λ°”λ‘œ μ •μ˜ν•  수 μžˆλ‹€.

λžŒλ‹€μ˜ μž₯단점 

μž₯점

  • μ½”λ“œμ˜ κ°„κ²°μ„± - λžŒλ‹€λ₯Ό μ‚¬μš©ν•˜λ©΄ λΆˆν•„μš”ν•œ 반볡문의 μ‚­μ œκ°€ κ°€λŠ₯ν•˜λ©° λ³΅μž‘ν•œ 식을 λ‹¨μˆœν•˜κ²Œ ν‘œν˜„ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
  • 지연연산 μˆ˜ν–‰ - λžŒλ‹€λŠ” 지연연상을 μˆ˜ν–‰ ν•¨μœΌλ‘œμ¨ λΆˆν•„μš”ν•œ 연산을 μ΅œμ†Œν™” ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
  • λ³‘λ ¬μ²˜λ¦¬ κ°€λŠ₯ - λ©€ν‹°μ“°λ ˆλ””λ₯Ό ν™œμš©ν•˜μ—¬ λ³‘λ ¬μ²˜λ¦¬λ₯Ό μ‚¬μš© ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

단점

  • λžŒλ‹€μ‹μ˜ 호좜이 κΉŒλ‹€λ‘­μŠ΅λ‹ˆλ‹€
  • λžŒλ‹€ stream μ‚¬μš© μ‹œ λ‹¨μˆœ forλ¬Έ ν˜Ήμ€ whileλ¬Έ μ‚¬μš© μ‹œ μ„±λŠ₯이 λ–¨μ–΄μ§‘λ‹ˆλ‹€.
  • λΆˆν•„μš”ν•˜κ²Œ λ„ˆλ¬΄ μ‚¬μš©ν•˜κ²Œ 되면 였히렀 가독성을 λ–¨μ–΄ 뜨릴 수 μžˆμŠ΅λ‹ˆλ‹€.

λžŒλ‹€μ‹ ν‘œν˜„λ²• 

λ§€κ°œλ³€μˆ˜ + μ‹€ν–‰λ¬Έ 으둜 κ΅¬μ„±λ˜λ©° μ ‘κ·Όμž, λ°˜ν™˜ν˜•μ€ μƒλž΅ν•©λ‹ˆλ‹€.

λžŒλ‹€μ˜ κΈ°λ³Έ 식: ( λ§€κ°œλ³€μˆ˜, ... ) -> { μ‹€ν–‰λ¬Έ }

( )에 interface ν•¨μˆ˜μ˜ λ§€κ°œλ³€μˆ˜λ₯Ό μž…λ ₯ν•˜κ³  { } μ•ˆμ— μ‹€ν–‰ν•  μ½”λ“œλ₯Ό μž‘μ„±ν•˜λŠ” ν˜•νƒœλ„ μž‘μ„±ν•œλ‹€.

 

1. Calculator의 calc()λ₯Ό λžŒλ‹€μ˜ κΈ°λ³Έμ‹μœΌλ‘œ κ΅¬ν˜„

class Driver {
  public static void main(String[] args) {
    int n = 2;
    // (λ§€κ°œλ³€μˆ˜) -> {κ΅¬ν˜„λ‘œμ§}
    Calculator cal = (int n) -> {return n + 1;};

    System.out.println(cal.calc(n)); // 3
  }
}

2. λ§€κ°œλ³€μˆ˜μ˜ νƒ€μž…μ„ μžλ™μœΌλ‘œ μΈμ‹ν•˜κΈ° λ•Œλ¬Έμ— λ³€μˆ˜ νƒ€μž…μ„ μ‚­μ œν•  수 μžˆλ‹€.

class Driver {
  public static void main(String[] args) {
    int n = 2;
    // (λ§€κ°œλ³€μˆ˜) -> {κ΅¬ν˜„λ‘œμ§}
    Calculator cal = (n) -> {return n + 1;};

    System.out.println(cal.calc(n)); // 3
  }
}

3. λ§€κ°œλ³€μˆ˜κ°€ ν•˜λ‚˜ 일 λ•Œ ()λ₯Ό μƒλž΅ν•  수 μžˆλ‹€. 두 κ°œμ΄μƒ ν˜Ήμ€ 없을 λ•ŒλŠ” ()κ°€ ν•„μš”ν•¨

class Driver {
  public static void main(String[] args) {
    int n = 2;
    // (λ§€κ°œλ³€μˆ˜) -> {κ΅¬ν˜„λ‘œμ§}
    Calculator cal = n -> {return n + 1;};
    // λ§€κ°œλ³€μˆ˜κ°€ μ—†λŠ” 경우 무쑰건 ()κ°€ ν•„μš”ν•¨. () -> {return "hi";}

    System.out.println(cal.calc(n)); // 3
  }
}

4. 둜직이 ν•œ μ€„μ•ˆμ— λλ‚˜λŠ” 경우 {}κ³Ό return을 μ œκ±°ν•  수 μžˆλ‹€.

class Driver {
  public static void main(String[] args) {
    int n = 2;
    Calculator cal = n -> n + 1; // κ°„λ‹¨ν•˜κ²Œ λ³€κ²½

    System.out.println(cal.calc(n)); // 3
  }
}

 

ν•™μŠ΅μ— 많이 도움이 된 λΈ”λ‘œκ·Έ 링크λ₯Ό  μ°Έμ‘°ν–ˆμŠ΅λ‹ˆλ‹€. πŸ™‚

예제둜 λ°°μ›Œλ³΄λŠ” Stream & Lambda

λžŒλ‹€μ‹(Lamda)의 κ°œλ… 및 μ‚¬μš©λ²•

'🐰JAVA > Basic' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

[JAVA ] List Interface  (0) 2022.06.20
[JAVA ] Collection  (0) 2022.06.20
[ JAVA ] Optional  (0) 2022.06.15
[ JAVA ] HashMap  (0) 2022.06.14
[ JAVA, DB μœ„μ£Ό ] 개발 κ΄€λ ¨ μš©μ–΄ 정리 ( 개인 ν•™μŠ΅μš© )  (0) 2022.06.14
Comments