728x90
람다식
람다식 함수형 프로그래밍 방식(Functional Programming : FP)
- 메소드를 하나의 식으로 표현한 것으로, 코드를 효율적이고 간결하게 작성할 수 있도록 문법을 제공
- 함수는 자체로 선언하여 쓰일 수 없고 클래스의 구성 멤버로 선언되고 사용됨
- 람다식은 익명 함수 생성 문법으로 함수명 없이 구현부(실행문)만으로 선언되고 함수가 아닌 실행문을 가진 객체가 됨
- 일반적인 객체가 아닌 인터페이스를 구현한 익명 구현 객체를 생성
함수의 매개변수는 값 또는 객체이지만 람다식으로 작성한 함수(동작)를 넘겨줄 수 있게 됨
함수형 인터페이스
- 클래스 내에 선언되고 클래스의 객체를 생성해서 호출하는 객체지향방식인 클래스 의존 형식이 아님
- 인터페이스를 이용해서 추상 함수를 정의하고 오버라이드 시킨 구문을 하나의 식으로 정의해서 만드는 익명 구현 객체
- 추상 메소드를 하나만 갖는 인터페이스(추상 메소드가 람다식과 일대일 매칭 되어야하기 때문)
- 추상 메소드가 2개 이상이라면 함수형 인터페이스가(람다식)이 아니라 익명 클래스
package test16;
import java.util.Scanner;
interface MyAdd{
int add(int x, int y); //추상 메소드 선언
}
public class TestMyNumber {
//2개의 정수를 입력 받아 더하는 함수 구현
public static int add(int x, int y) {
return x + y;
}
//문자열 길이를 리턴하는 함수 구현
public static int strLen(String str) {
return str.length();
}
//2개의 정수를 입력 받아 큰 값을 구하는 함수 구현
public static int getMax(int x, int y) {
if(x>=y) {
return x;
}else {
return y;
}
}
public static void main(String[] args) {
System.out.println("정수 입력");
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
//add(10,5) 호출하고 함수 내에서 돌려받은 반환값 저장
//static 접근 시 클래스명.메소드명()
System.out.println("add("+x+","+y+") = " + TestMyNumber.add(x,y));
//strLen(String str) 호출하고 함수 내에서 돌려받은 반환값 저장
String str = "함수형 프로그래밍(람다식)";
System.out.println("strLen(String str) = " + strLen(str));
//getMax
System.out.println("getMax("+x+","+y+") = " + getMax(x,y));
System.out.println("================================");
//익명 객체 구현(익명 클래스로 만들기)
//인터페이스는 객체를 생성할 수 없음
MyAdd myadd = new MyAdd() {
@Override
public int add(int x, int y) {
return x+y;
}
}; //세미콜론을 반드시 붙여줘야함
System.out.println(myadd.add(x, y));
}
}
package test16;
import java.util.Scanner;
interface MyAdd{
int add(int x, int y); //추상 메소드 선언
}
public class TestMyNumber {
//2개의 정수를 입력 받아 더하는 함수 구현
public static int add(int x, int y) {
return x + y;
}
//문자열 길이를 리턴하는 함수 구현
public static int strLen(String str) {
return str.length();
}
//2개의 정수를 입력 받아 큰 값을 구하는 함수 구현
public static int getMax(int x, int y) {
if(x>=y) {
return x;
}else {
return y;
}
}
public static void main(String[] args) {
// System.out.println("정수 입력");
Scanner sc = new Scanner(System.in);
// int x = sc.nextInt();
// int y = sc.nextInt();
//add(10,5) 호출하고 함수 내에서 돌려받은 반환값 저장
//static 접근 시 클래스명.메소드명()
// System.out.println("add("+x+","+y+") = " + TestMyNumber.add(x,y));
//strLen(String str) 호출하고 함수 내에서 돌려받은 반환값 저장
String str = "함수형 프로그래밍(람다식)";
System.out.println("strLen(String str) = " + strLen(str));
//getMax
// System.out.println("getMax("+x+","+y+") = " + getMax(x,y));
System.out.println("================================");
//익명 객체 구현(익명 클래스로 만들기)
//인터페이스는 객체를 생성할 수 없음
//인터페이스 변수명 = 인터페이스로 익명으로 구현한 객체를 넣어줌
MyAdd myadd = new MyAdd() {
@Override
public int add(int x, int y) {
return x+y;
}
}; //세미콜론을 반드시 붙여줘야함
// System.out.println(myadd.add(x, y));
System.out.println("================================");
//람다식으로 익명 객체 구현 : 오버라이드 시킨 내용을 수식으로 표현
//오버라이드 시켜 구현한 함수 기능을 람다식으로 수식으로 표현하고 변수에 대입
MyAdd plus = (x, y) -> {return x+y;};
System.out.println(plus.add(10, 5));
}
}
interface MyAdd{
int add(int x, int y); //추상 메소드 선언
}
//익명 객체 구현(익명 클래스로 만들기)
//인터페이스는 객체를 생성할 수 없음
//인터페이스 변수명 = 인터페이스로 익명으로 구현한 객체를 넣어줌
MyAdd myadd = new MyAdd() {
@Override
public int add(int x, int y) {
return x+y;
}
}; //세미콜론을 반드시 붙여줘야함
System.out.println("================================");
//람다식으로 익명 객체 구현 : 오버라이드 시킨 내용을 수식으로 표현
//중괄호 안의 구현 부분이 return문 하나라면 중괄호와 return문을 모두 생략하고 식만 사용할 수 있음
MyAdd plus = (x, y) -> x+y;
interface MyStrLen{
int strLen(String str);
}
//MyStrLen msl = (String str) -> {return str.length();}; //반환자료형, 함수명 제외
//MyStrLen msl = (str) -> {return str.length();}; //매개변수의 자료형 생략
MyStrLen msl = str -> str.length(); //자료형이 1개인 경우 괄호 생략, return문이 하나라면 return문 모두 생략하고 식만 사용
System.out.println(msl.strLen("함수형 프로그래밍(람다식)"));
interface MyMax{
int getMax(int x, int y);
}
MyMax mm = (x, y) -> x >= y? x: y;
System.out.println(mm);
package test16;
//함수형 인터페이스
@FunctionalInterface
interface Calc{
int calc(int x, int y);
}
public class TestMyNumber2 {
public static void main(String[] args) {
//더하기
Calc calc1 = (x, y) -> x+y;
//최댓값
Calc calc2 = (x, y) -> (x>y)? x : y;
//x~y까지의 합계
Calc calc3 = (x, y) -> {
int sum = 0;
for(int i = x; i<=y; i++) {
sum += i;
}
return sum;
};
System.out.println(calc1.calc(1, 10));
System.out.println(calc2.calc(1, 10));
System.out.println(calc3.calc(1, 10));
}
}
package test16;
//함수형 인터페이스
@FunctionalInterface
interface Calc{
int calc(int x, int y);
}
public class TestMyNumber2 {
// 메소드에 매개변수로 익명함수(람다식)를 전달하여 사용할 수 있음
// 익명함수(람다식), 정수형 데이터 값 1, 정수형 데이터 값 2
public static void Calc_func(Calc calc, int a, int b) {
int result = calc.calc(a, b);
System.out.println(result);
}
public static void main(String[] args) {
//더하기
Calc calc1 = (x, y) -> x+y;
//최댓값
Calc calc2 = (x, y) -> (x>y)? x : y;
//x~y까지의 합계
Calc calc3 = (x, y) -> {
int sum = 0;
for(int i = x; i<=y; i++) {
sum += i;
}
return sum;
};
System.out.println(calc1.calc(1, 10));
System.out.println(calc2.calc(1, 10));
System.out.println(calc3.calc(1, 10));
System.out.println("============================");
//Calc_func 함수 호출
Calc_func(calc1, 5, 10);
Calc_func(calc2, 5, 10);
Calc_func(calc3, 5, 10);
}
}
람다식 만들기(함수형 인터페이스 만들기)
- 1개의 추상 메소드를 정의한 인터페이스를 만듦
- 오버라이드 시킬 구문을 하나의 식으로 정의(익명함수형식)
- 인터페이스형 변수에 대입
- 인터페이스형 변수에는 함수형 수식(람다식)이 저장
- 인터페이스 변수에 저장된 함수형 수식을 함수 형태로 호출할 때 매개변수로 데이터값을 대입
@FunctionalInterface 애노테이션을 사용하여 함수형 인터페이스임을 표시
728x90
'KDT > Java' 카테고리의 다른 글
240318 Java - 스트림 3, 스레드 1 (0) | 2024.03.18 |
---|---|
240314 Java - 람다식 2, 스트림 1 (0) | 2024.03.14 |
240311 Java - 내부 클래스2 (0) | 2024.03.11 |
240307 Java 입출력 스트림 3, 내부 클래스 1 (0) | 2024.03.07 |
240306 Java 입출력과 스트림 2 (0) | 2024.03.06 |