JAVA/java

[JAVA] Lambda λžŒλ‹€μ‹, .forEach,

congs 2023. 9. 12. 01:18

 

πŸ“Œ Lambda expression λžŒλ‹€μ‹

  • μˆœμˆ˜ν•œ ν•¨μˆ˜λ₯Ό κ΅¬ν˜„ → 호좜
  • μ™ΈλΆ€ μžλ£Œμ— λΆ€μˆ˜μ μΈ(side effect) 영ν–₯x = λ‹€λ₯Έ μžλ£Œκ°€ λ³€κ²½λ˜λŠ” 일x
  • jsμ—μ„œ 많이 μ‚¬μš©ν•˜λŠ” 방법 (javaμ—μ„œλŠ” 잘 μ‚¬μš©x)

1. ν•¨μˆ˜ν˜• ν”„λ‘œκ·Έλž¨μ΄λ‹€.

: Functional Programming = FP

2.  순수 ν•¨μˆ˜(pure function)이닀.

: 맀개 λ³€μˆ˜λ§Œμ„ μ‚¬μš©ν•˜μ—¬ λ§Œλ“œλŠ” ν•¨μˆ˜

  • ν•¨μˆ˜ λ‚΄λΆ€μ—μ„œ λ³€μˆ˜λ₯Ό μ‚¬μš©x ⇒ ν•¨μˆ˜κ°€ μ‹€ν–‰λ˜λ”λΌλ„ 외뢀에 영ν–₯x
// < κΈ°λ³Έλ©”μ„œλ“œ >
int result = add(3,5);
System.out.println(result);
		
// < λžŒλ‹€μ‹ >
(int x, int y) -> {return x+y}
int x -> {return x+y}
		
	
// λ©”μ„œλ“œ
public static int add(int x, int y) {
	return x+y;
}

 

3. λžŒλ‹€μ‹ μ‚¬μš©μ˜ˆμ œ

 

[JAVA] 성적 λ°°μ—΄ aμ—μ„œ 70점 μ΄μƒλ§Œ 합계 κ΅¬ν•˜κΈ°

성적 λ°°μ—΄ aμ—μ„œ 70점 μ΄μƒλ§Œ 합계 κ΅¬ν•˜κΈ° public static void main(String[] args) { int a[] = {10,20,30,40,50,60,70,80,90}; int sum1 = 0; //java for(int i=0; i=70) { sum1 += a[i]; } } System.out.println("javaκ΅¬ν˜„ sum: "+sum1); //Stream int su

jungeun980906.tistory.com

 

[JAVA] 성적 λ¦¬μŠ€νŠΈμ—μ„œ 70점 μ΄μƒλ§Œ 합계 κ΅¬ν•˜κΈ°

성적 λ¦¬μŠ€νŠΈμ—μ„œ 70점 μ΄μƒλ§Œ 합계 κ΅¬ν•˜κΈ° public static void main(String[] args) { int a[] = {10,20,30,40,50,60,70,80,90}; List aList = new ArrayList(); for(Integer tmp : a) { aList.add(tmp); } int listSum = aList.stream().filter(b->b>=70).ma

jungeun980906.tistory.com

  • λ¬Έμžμ—΄(이름)을 λ‹΄λŠ” 리슀트 생성 + 슀트림으둜 생성 → μ •λ ¬ 좜λ ₯
  • : μ΄λ¦„μ˜ μˆ˜κ°€ 5개 이상 좜λ ₯ + 각 μ΄λ¦„μ˜ κΈ€μž 수 좜λ ₯
  • https://jungeun980906.tistory.com/388

μ •λ ¬ + μ΄λ¦„μ˜ μˆ˜κ°€ 5개 이상 좜λ ₯ + 각 μ΄λ¦„μ˜ κΈ€μž 수 좜λ ₯

μ •λ ¬ + μ΄λ¦„μ˜ μˆ˜κ°€ 5개 이상 좜λ ₯ + 각 μ΄λ¦„μ˜ κΈ€μž 수 좜λ ₯

  • λ°°μ—΄μ—μ„œ 짝수만 좜λ ₯ + 쀑볡 값은 μ‚­μ œ + μƒˆλ‘œμš΄ 배열에 μ €μž₯
  • https://jungeun980906.tistory.com/389
 

[JAVA] λ°°μ—΄μ—μ„œ 짝수만 좜λ ₯ + 쀑볡 값은 μ‚­μ œ + μƒˆλ‘œμš΄ 배열에 μ €μž₯

λ°°μ—΄μ—μ„œ 짝수만 좜λ ₯ + 쀑볡 값은 μ‚­μ œ + μƒˆλ‘œμš΄ 배열에 μ €μž₯ import java.util.Arrays; import java.util.stream.Stream; public class streamEx02 { public static void main(String[] args) { /* λ°°μ—΄μ—μ„œ 짝수만 좜λ ₯(μ •λ ¬) * 쀑볡

jungeun980906.tistory.com

  • 여행고객λͺ…단 : λͺ…λ‹¨μΆœλ ₯ / λΉ„μš©κ³„μ‚° / 쑰건에 λ§žλŠ” λͺ…λ‹¨λ§Œ 좜λ ₯
  • https://jungeun980906.tistory.com/390
 

[JAVA] 여행고객λͺ…단 : λͺ…λ‹¨μΆœλ ₯ / λΉ„μš©κ³„μ‚° / 쑰건에 λ§žλŠ” λͺ…λ‹¨λ§Œ 좜λ ₯

여행고객λͺ…단 : λͺ…λ‹¨μΆœλ ₯ / λΉ„μš©κ³„μ‚° / 쑰건에 λ§žλŠ” λͺ…λ‹¨λ§Œ 좜λ ₯ import java.util.ArrayList; public class Customar_travel { public static void main(String[] args) { ArrayList list = new ArrayList(); list.add(new Customar("μ΄μˆœμ‹ ",40)

jungeun980906.tistory.com

 

[JAVA] ν•™μƒμ„±μ λ¦¬μŠ€νŠΈ : (학생이름:점수) 좜λ ₯ / 점수 합계 좜λ ₯

ν•™μƒμ„±μ λ¦¬μŠ€νŠΈ : (학생이름:점수) 좜λ ₯ / 점수 합계 좜λ ₯ import java.util.ArrayList; import java.util.stream.Stream; public class StreamEx03 { public static void main(String[] args) { // ArrayList list 생성 ν›„ //list.add() 학생λͺ…,

jungeun980906.tistory.com

 


 

πŸ“Œ .forEach : ν–₯μƒλœ forκ³Ό 같은 μ—­ν• 

// < κΈ°λ³Έλ©”μ„œλ“œ >
		ArrayList<Integer> number = new ArrayList<>();
		number.add(10);
		number.add(20);
		number.add(30);
		
		for(Integer i : number) {
			System.out.print(i+" ");
		};
		
		System.out.println();
		System.out.println("--λžŒλ‹€μ‹ μ‚¬μš©--");
		
		// < λžŒλ‹€μ‹ >

		//return 있으면 μ€‘κ΄„ν˜Έ μƒλž΅ λΆˆκ°€λŠ₯
		//return μ—†μœΌλ©΄ μ€‘κ΄„ν˜Έ μƒλž΅ κ°€λŠ₯ 
		//예) str -> System.out.println(str);


		//.forEach : ν–₯μƒλœ forκ³Ό 같은 μ—­ν• 
		
		//1. (n=λ§€κ°œλ³€μˆ˜)λ₯Ό μ‚¬μš©ν•˜λŠ” 방법
		number.forEach((n)->{ //n을 λ„£μœΌλ©΄ μ•„λž˜ 싀행ꡬ문을 μ²˜λ¦¬ν•΄!
			System.out.print(n+" ");
		});
		
		//2. λ©”μ„œλ“œμ— 직접 μ°Έμ‘°ν•˜λŠ” 방법 (λ§€κ°œλ³€μˆ˜λ₯Ό μ‚¬μš©ν•˜μ§€ μ•ŠλŠ” 방법)
		number.forEach(System.out::println);
		
		System.out.println("--number list의 총 개수--");
		System.out.println(number.stream().count());