728x90
반응형
SMALL
람다 식 정의
- 메소드를 하나의 식으로 간결하고 명확하게 표현
- 익명 메소드 형식
함수형 인터페이스
- 추상메소드 한개!만 가짐
- 함수를 다른객체들에게 가능 연산 모두 지원하는 일급 객체 취급
- 할당, 인자 전달받기, 결과return 가능
익명 클래스
- 한번만 사용 가능
- 클래스 선언+객체생성
@FunctionalInterface //
interface UnaryOp{
public int add(int k);
}
public class Lambda {
public static int getResult(UnaryOp op, int k){
return op.add(k);
}
public static void main(String[] args){
UnaryOp result = new UnaryOp(){
public int add(int i){
return i+i;
}
};
System.out.print(getResult(result, 1));
}
}
람다 식을 이용한 코드
- 기본 식
@FunctionalInterface
interface BinaryOp3{
public int aply(int i);
}
public class Lambda {
public static int resultLambda(BinaryOp3 bin, int i){
return bin.aply(i);
}
public static void main(String[] args){
System.out.println(resultLambda((int i) -> {return i+i;},3));
}
}
- 파라미터가 하나 일 때, 다음과 같이 타입과 () 생략가능
public static void main(String[] args){
System.out.println(resultLambda(i -> {return i+i;},3));
}
파라미터가 2개일 때
@FunctionalInterface
interface UnaryOp2{
public int applying(int i, int j);
}
public class Lambda2 {
public static int getUnaryResult2(UnaryOp2 bindery, int i, int j){
return bindery.applying(i,j);
}
public static void main(String[] args){
System.out.println(getUnaryResult2((int i, int j) -> {return i*j;}, 3, 3));
}
}
728x90
반응형
LIST
'스마트인재개발원 > 자바' 카테고리의 다른 글
[자바] eclipse 단축키 모음 (0) | 2022.09.06 |
---|---|
[JAVA]오류4 - error: unmappable character (0xEC) for encoding x-windows-949 (0) | 2022.08.02 |
[JAVA] 오류3 - Exception in thread "main" java.nio.fie.InvalidPathException / file not found (0) | 2022.08.02 |
[JAVA] swap 버블정렬(bubble sort)/generic을 사용한 버블정렬 (0) | 2022.07.30 |
[JAVA] 제네릭(Generic) 개요 / 타입 & 메소드선언 / 타입 파라미터 명명 규칙 (0) | 2022.07.30 |