Chapter 16 - 람다식 확인문제

김태원·2023년 1월 26일
0

Chapter 16 - 람다식 확인문제

정답: 4

@FunctionalInterface를 붙이는 것은 선택사항이며 컴파일 과정에서 추상 메소드가 하나인지 검사하기 때문에 정확한 함수형 인터페이스를 작성할 수 있게 도와주는 역할을 한다.

정답: 4

디폴트 생성자만 호출되는 것은 아니다.

정답: 2

(a, b) -> a * b

와 같이 작성해야 한다.

정답:

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);
}

정답:

방법 1

// 최대값 얻기
int max = maxOrMin((x, y) -> (x >= y) ? x : y);
System.out.println("최대값: " + max);

// 최소값 얻기
int min = maxOrMin((x, y) -> (x <= y) ? x : y);
System.out.println("최소값: " + min);

방법 2

// 최대값 얻기
int max = maxOrMin((x, y) -> Math.max(x, y));
System.out.println("최대값: " + max);

// 최소값 얻기
int min = maxOrMin((x, y) -> Math.min(x, y));
System.out.println("최소값: " + min);

방법 3(Best way)

// 최대값 얻기
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);
    }

    return (double) sum / students.length;
}

정답:

Student::getEnglishScore
Student::getMathScore
profile
개발이 재밌어서 하는 Junior Backend Developer

0개의 댓글