람다식: (매개변수, ...) -> { 처리 내용 }
@FunctionalInterface
public interface Calculable {
//추상 메소드
void calculate(int x, int y);
}
public class LambdaExample {
public static void main(String[] args) {
action((x, y) -> {
int result = x + y;
System.out.println("result: " + result);
});
action((x, y) -> {
int result = x - y;
System.out.println("result: " + result);
});
}
public static void action(Calculable calculable) {
//데이터
int x = 10;
int y = 4;
//데이터 처리
calculable.calculate(x, y);
}
}
result: 14
result: 6
(매개변수, ... ) -> {
실행문;
return 값;
} //return문 하나만 있을 경우에는 중괄호와 함께 return 생략 가능
Calcuable 인터페이스에 리턴 값이 있는 calc() 추상메소드 작성
@FunctionalInterface
public interface Calcuable {
double calc(double x, double y);
}
person 클래스에 Workable을 매개변수로 갖는 action1() 메소드와 Speakable을 매개변수로 갖는 action2() 메소드 작성
public class Person {
public void action(Calcuable calcuable) {
double result = calcuable.calc(10, 4);
System.out.println("결과: " + result);
}
}
public class LambdaExample {
public static void main(String[] args) {
Person person = new Person();
//실행문이 두 개 이상일 경우
person.action((x, y) -> {
double result = x + y;
return result;
});
//리턴문이 하나만 있을 경우(연산식)
//person.action((x, y) -> {
// return (x + y);
//});
person.action((x, y) -> (x + y));
//리턴문이 하나만 있을 경우(메소드 호출)
//person.action((x, y) -> {
// return sum(x, y);
//});
person.action((x, y) -> sum(x, y));
}
public static double sum(double x, double y) {
return (x + y);
}
}
결과 : 14.0
결과 : 14.0
결과 : 14.0
클래스 :: 메소드
참조변수 : 메소드
(a, b) -> { a.instanceMethod(b); }
// 람다식에서 제공되는 a 매개변수의 메소드를 호출해서
// b 매개변수를 매개값으로 사용하는 경우
클래스 :: instancMethod
//위 를 메소드 참조로 표현한 것
@FunctionalInterface
public interface Comparable {
int compare(String a, String b);
}
Person 클래스에 Comparable을 매개변수로 갖는 ordering() 메소드 작성
public class Person {
public void ordering(Comparable comparable) {
String a = "홍길동";
String b = "김길동";
int result = comparable.compare(a, b);
if(result < 0) {
System.out.println(a + "은 " + b + "보다 앞에 옵니다.");
} else if(result == 0) {
System.out.println(a + "은 " + b + "과 같습니다.");
} else {
System.out.println(a + "은 " + b + "보다 뒤에 옵니다.");
}
}
}
public class MethodReferenceExample {
public static void main(String[] args) {
Person person = new Person();
person.ordering(String :: compareToIgnoreCase);
}
}
홍길동은 김길동보다 뒤에 옵니다.
(a, b) -> { return new 클래스(a, b); }
//코드를 보면 람다식은 단순히 객체를 생성 후 리턴한다.
클래스 :: new
//생성자 참조로 표현한 것.
public class Example {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
for(int i=0; i<3; i++) {
System.out.println("작업 스레드가 실행됩니다."); }
}
);
thread.start();
}
}
(() -> {System.out.println("Ok 버툰을 클릭했습니다.");})
(() -> {System.out.println("Cancel 버튼을 클릭했습니다.");})
@FunctionalInterface
public interface Function {
public double apply(double x, double y);
}
public static void main(String[] args) {
int max = maxOrMin(Math::max
);
System.out.println("최댓값 : " + max);
int min = maxOrMin(Math::min
);
System.out.println("최솟값 : " + min);
}
}
public static double avg(Function<Student> function) {
int sum = 0;
for (Student student : students) {
sum += function.apply(student);
}
double avg = (double) sum / students.length;
return avg;
}
Student :: getEnglishScore
Student :: getMathScore