๐ 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. ๋๋ค์ ์ฌ์ฉ์์
- ์ฑ์ ๋ฐฐ์ด a์์ 70์ ์ด์๋ง ํฉ๊ณ ๊ตฌํ๊ธฐ
- https://jungeun980906.tistory.com/386
[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
- ์ฑ์ ๋ฆฌ์คํธ์์ 70์ ์ด์๋ง ํฉ๊ณ ๊ตฌํ๊ธฐ
- https://jungeun980906.tistory.com/387
[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());
'JAVA > java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JAVA] Character ํด๋์ค (๋ฌธ์ ๊ด๋ จ ์์ ) (0) | 2024.02.07 |
---|---|
[JAVA] thread 592 (2) | 2023.09.12 |
[JAVA] ๋ฐ์ดํฐ์ ์ถ๋ ฅ (์คํธ๋ฆผ) (0) | 2023.09.12 |
[JAVA] StringBuffer / StringBuilder ๋ฌธ์์ด ์ถ๊ฐ/๋ณ๊ฒฝ ๊ฐ์ฒด (0) | 2023.09.12 |
[JAVA] RegularExpression ์ ๊ทํํ์ (0) | 2023.09.12 |