Spring

[JAVA] Lamda ν‘œν˜„μ‹ 예제

natrue 2021. 8. 27. 15:09
728x90

 Lamda ν‘œν˜„μ‹

  • λžŒλ‹€μ‹μ€ JAVA 8에 λ„μž…λœ ν•¨μˆ˜(λ©”μ„œλ“œ)λ₯Ό κ°„λ‹¨ν•œ μ‹μœΌλ‘œ ν‘œν˜„ν•˜λŠ” λ°©λ²•μœΌλ‘œ 읡λͺ… ν•¨μˆ˜(anonymous function)λ₯Ό μƒμ„±ν•˜κΈ° μœ„ν•œ 식이닀.
  • 객체 μ§€ν–₯ μ–Έμ–΄λ³΄λ‹€λŠ” ν•¨μˆ˜ μ§€ν–₯ 언어에 가깝닀. 

 

<ν•¨μˆ˜μ™€ λ©”μ„œλ“œμ˜ 차이>

ν•¨μˆ˜ λ©”μ„œλ“œ
ν΄λž˜μŠ€μ— 독립적 ν΄λž˜μŠ€μ— 쒅속적
일반적인 μš©μ–΄ 객체지ν–₯ κ°œλ… μš©μ–΄

 

 Lamda식 κΈ°λ³Έ μž‘μ„± 방법 

(인수) -> {처리}
()-> {처리}
(인수) -> 처리
(인수) -> {return 0;}
int max(int a, int b) {
	retrun a > b ? a : b; 
}

λ©”μ„œλ“œμ˜ 이름과 λ°˜ν™˜ νƒ€μž…μ„ 제거 ν›„ ' -> ' μΆ”κ°€

(a, b) -> a > b ? a : b

 

 Listμ—μ„œ Lamdaμ‚¬μš© 

Java 7 μ΄ν•˜ : forλ¬Έ μ‚¬μš©

List<Map<String, Object>> list = ...service.select..(map); 
for (Map<String, Object> string : list) {
  System.out.println( "string :: " + string);
}

Java 8 이상 : Lambda μ‚¬μš©

List<Map<String, Object>> list = ...service.select..(map); 
list.forEach(string -> System.out.println( "string :: " + string));

λ™μΌν•œ κ²°κ³Ό

string :: {SEQNUM=1, APP_NAM=νŠΈλ£¨μ½”λ”©1, IST_YMD=20200101} 
string :: {SEQNUM=2, APP_NAM=νŠΈλ£¨μ½”λ”©2, IST_YMD=20200102} 
string :: {SEQNUM=3, APP_NAM=νŠΈλ£¨μ½”λ”©3, IST_YMD=20200103} 
string :: {SEQNUM=4, APP_NAM=νŠΈλ£¨μ½”λ”©4, IST_YMD=20200104}

 - Lambda μ‚¬μš©ν•˜λ©΄ μ½”λ“œκ°€ κ°„κ²°ν•΄μ§€κ³ , μ˜λ„κ°€ λͺ…ν™•ν•˜κ²Œ λ“œλŸ¬λ‚˜ 가독성이 λ†’μ•„μ§„λ‹€.

 

 λ°°μ—΄μ—μ„œ Lamdaμ‚¬μš© 

String[] stringArray = { "truena", "truema", "trueda" }; 
Arrays.asList(stringArray).forEach(string -> System.out.println(string));

 

 mapμ—μ„œ Lamdaμ‚¬μš© 

Java 7 μ΄ν•˜ : forλ¬Έ μ‚¬μš©

HashMap<String, Object> doc = new HashMap<String, Object>(); 
 doc.put("name", "true"); 
 doc.put("age", "27")); 

for(Entry<String, Object> entry : doc.entrySet()) { 
 System.out.println(" mapKey ::" + entry.getKey());
 System.out.println(" mapValue ::" + entry.getValue()); 
}

Java 8 이상 : Lambda μ‚¬μš©

HashMap<String, Object> doc = new HashMap<String, Object>(); 
 doc.put("name", "true"); 
 doc.put("age", "27")); 

doc.forEach((key, value) -> { 
 System.out.println(" mapKey ::" + key); 
 System.out.println(" mapValue ::" + value); 
});

λ™μΌν•œ κ²°κ³Ό

mapKey :: name 
mapValue :: true 
mapKey :: age 
mapValue :: 27

 

 Lamda μž₯/단점 

 μž₯점  λ‹¨μ 
 μ½”λ“œκ°€ κ°„κ²°ν•΄μ§„λ‹€.  Lamdaλ₯Ό μ‚¬μš©ν•˜λ©΄μ„œ λ§Œλ“  무λͺ…ν•¨μˆ˜λŠ” μž¬μ‚¬μš© λΆˆκ°€
 μ˜λ„κ°€ λͺ…ν™•νžˆ λ“œλŸ¬λ‚˜ 가독성 ν–₯상  λ””버깅이 κΉŒλ‹€λ‘­λ‹€.
 μ½”λ”© μ‹œκ°„μ˜ 단좕  λΉ„μŠ·ν•œ ν•¨μˆ˜λ₯Ό 계속 쀑볡 생성할 κ°€λŠ₯성이 λ†’μ•„ 
 μ½”λ“œκ°€ μ§€μ €λΆ„ν•΄μ§ˆ 수 μžˆλ‹€.
 λ³‘λ ¬ ν”„λ‘œκ·Έλž˜λ°μ— 용이  μž¬κ·€λ‘œ λ§Œλ“€κ²½μš°λŠ” λΆ€μ ν•©ν•œλ©΄μ΄ μžˆλ‹€.

 

 ** ν•¨μˆ˜ν˜• μΈν„°νŽ˜μ΄μŠ€ Lamda ν‘œν˜„μ‹ 문법 예제  

/* λ‘κ°œμ˜ 숫자 λ”ν•˜κΈ° */

@FunctionalInterface // ν•¨μˆ˜ν˜• μΈν„°νŽ˜μ΄μŠ€λŠ” 단 ν•˜λ‚˜μ˜ 좔상 λ©”μ„œλ“œλ§Œ κ°€μ Έμ•Όν•œλ‹€.
interface Compare{
    public int compareTo(int a, int b);
}

public class Ramda2 {
    public static void exec(Compare com) {
      int k = 10;
      int m = 20;
    
      // ν•¨μˆ˜ν˜• μΈν„°νŽ˜μ΄μŠ€
      int value = com.compareTo(k, m);
      System.out.println(value);
    }
    
    public static void main(String[] args) { 
      //λžŒλ‹€μ‹ 문법 (λ§€κ°œλ³€μˆ˜ λͺ©λ‘)->{μ‹€ν–‰λ¬Έ}
      exec((i,j)->{
       return i+j;
      });
    }
}

 































μ°Έκ³  : 

 

[Java] λžŒλ‹€μ‹(Lambda Expressions) -> μ‚¬μš©λ²• & 예제

 λžŒλ‹€μ‹μ΄λž€? λžŒλ‹€μ‹μ΄λž€ "μ‹λ³„μžμ—†μ΄ μ‹€ν–‰κ°€λŠ₯ν•œ ν•¨μˆ˜" ν•¨μˆ˜μΈλ° ν•¨μˆ˜λ₯Ό λ”°λ‘œ λ§Œλ“€μ§€ μ•Šκ³  μ½”λ“œν•œμ€„μ— ν•¨μˆ˜λ₯Ό μ¨μ„œ 그것을 ν˜ΈμΆœν•˜λŠ” 방식이라고 μƒκ°ν•˜μ‹œλ©΄ λ˜κ² μŠ΅λ‹ˆλ‹€. ν•¨μˆ˜μ  ν”„λ‘œκ·Έλž˜λ°μ„

coding-factory.tistory.com

 

ν”„λ‘œκ·Έλž¨ 개발 지식 곡유, devkuma

λ°λΈŒμΏ λ§ˆλŠ” ν”„λ‘œκ·Έλž˜λ° κ°œλ°œμ— λŒ€ν•œ 지식을 κ³΅μœ ν•©λ‹ˆλ‹€.

www.devkuma.com

 

[Java] λžŒλ‹€μ‹(Lambda Expression)κ³Ό ν•¨μˆ˜ν˜• μΈν„°νŽ˜μ΄μŠ€(Functional Interface) (2/5)

1. λžŒλ‹€μ‹(Lambda Expression) μ΄λž€? Stream 연산듀은 λ§€κ°œλ³€μˆ˜λ‘œ ν•¨μˆ˜ν˜• μΈν„°νŽ˜μ΄μŠ€(Functional Interface)λ₯Ό 받도둝 λ˜μ–΄μžˆλ‹€. 그리고 λžŒλ‹€μ‹μ€ λ°˜ν™˜κ°’μœΌλ‘œ ν•¨μˆ˜ν˜• μΈν„°νŽ˜μ΄μŠ€λ₯Ό λ°˜ν™˜ν•˜κ³  μžˆλ‹€. κ·Έλ ‡κΈ° λ•Œλ¬Έ

mangkyu.tistory.com