14장
1. 람다
/*
==========================================================================
1. 람다식의 예시
==========================================================================
*/
// 1.1. 메서드
int max(int a, int b) {
return a > b ? a : b;
}
// 1.1. 람다식
(int a, int b) -> { return a > b ? a : b; } // 1
(int a, int b) -> a > b ? a : b; // 2
(a, b) -> a > b ? a : b; // 3
// 1.2. 메서드
void printVar(String name, int i) {
System.out.println(name + "=" + i);
}
// 1.2. 람다식
(String name, int i) -> { System.out.println(name + "=" + i); } // 1
(name, i) -> { System.out.println(name + "=" + i); } // 2
(name, i) -> System.out.println(name + "=" + i) // 3
// 1.3. 메서드
int square(int x) {
return x * x;
}
// 1.3. 람다식
(int x) -> x * x // 1
(x) -> x * x // 2
x -> x * x // 3
// 1.4. 메서드
int roll() {
return (int)(Math.random()*6);
}
// 1.4. 람다식
() -> {
return (int)(Math.random()*6);
}
() -> return (int)(Math.random()*6)
//1.5. 메서드
int sumArr(int[] arr) {
int sum = 0;
for (int i : arr) {
sum += i;
}
return sum;
}
// 1.5. 람다식
(int[] arr) -> {
int sum = 0;
for(innt i : arr) {
sum += i;
}
return sum;
}
/*
==========================================================================
2. 메서드 참조
- 클래스이름::메서드이름
- 참조변수::메서드 이름
==========================================================================
*/
// 2.1. 람다식
Function<String, Integer> f = (String s) -> Integer.parseInt(s);
// 2.1. 메서드 참조(컴파일러는 생략된 부분을 제네릭으로 알아냄)
Function<String, Integer> f = Integer::parseInt;
// 2.2. 람다식
Function<Integer, MyClass> f = (i) -> new MyClass(i);
// 2.2. 메서드 참조(생성자)
Function<Integer, MyClass> f = MyClass:new;2. 스트림
2.1. 스트림의 소개
2.2. 특징
2.3. 스트림의 예시
2.4. 스트림의 연산
중간 연산
설명
최종 연산
설명
Last updated