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

  • ํ•™์ƒ์„ฑ์ ๋ฆฌ์ŠคํŠธ : (ํ•™์ƒ์ด๋ฆ„:์ ์ˆ˜) ์ถœ๋ ฅ / ์ ์ˆ˜ ํ•ฉ๊ณ„ ์ถœ๋ ฅ
  • https://jungeun980906.tistory.com/391
 

[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());