메소드 참조(Method references)

메소드를 참조해서 

매개변수의 정보 및 리턴타입을 알아내어 람다식에서 불필요한 매개변수를 제거하는 것이 목적이다.


종종 람다식은 기존 메소드를 단순하게 호출만 하는 경우가 있다.

 (left, right) -> Math.max(left,right);           
    --->  
 Math :: max  -----메소드 참조


메소드 참조도 람다식과 마찬가지로 인터페이스의 익명 구현 객체로 생성됨.

타겟 타입에서 추상메소드의 매개변수 및 리턴 타입에 따라 메소드 참조도 달라진다.

예)  IntBinayOperator 인터페이스는 두개의 int  매개값을 받아 int 값을 리턴 하므로
 동일한 매개값과 리턴타입을 갖는 Math 클래스의 max() 메소드를 참조할 수 있다.

IntBinaryOperator operator = Math :: max;  -----메소드 참조



정적 메소드와 인스턴스 메소드 참조

정적 메소드 참조

클래스 :: 메소드

인스턴스 메소드 참조

참조변수 :: 메소드


Exam

public class Calculator {
public static int staticMethod(int x, int y) {
return x + y;
}

public int instanceMethod(int x, int y) {
return x + y;
}

}
import java.util.function.IntBinaryOperator;

public class MethodReferencesExample {
public static void main(String[] args) {
IntBinaryOperator operator;

//정적 메소드 참도
operator = (x, y) -> Calculator.staticMethod(x, y);
System.out.println("결과1 : " + operator.applyAsInt(1, 2));
//결과1 : 3

operator = Calculator::staticMethod;
System.out.println("결과2 : " + operator.applyAsInt(3, 4));
//결과2 : 7

//인스턴스 메소드 참조
Calculator obj = new Calculator();
operator = (x,y) -> obj.instanceMethod(x, y);
System.out.println("결과3 : " + operator.applyAsInt(5,6));
//결과3 : 11

operator = obj::instanceMethod;
System.out.println("결과3 : " + operator.applyAsInt(7,8));
//결과4 : 15
}
}



매개변수의 메소드 참조

(a,b) -> {a.instanceMethod(b);}   --->   클래스 :: instanceMethod



ToIntBiFunction<String, String> function;


function = (a,b) -> a.compareToIgnoreCase(b);

print(function.applyAsInt( "Java8", "JAVA8"));


---->


function = String :; compareToIgnoreCase;

print(function.applyAsInt( "Java8", "JAVA8"));


exam

import java.util.function.ToIntBiFunction;

public class ArgumentMethodReferencesExample {
public static void main(String[] args) {
ToIntBiFunction<String, String> function;

function = (a, b) -> { return a.compareToIgnoreCase(b); };
print(function.applyAsInt("java8", "JAVA8"));
//동일한 문자열입니다.

function = String::compareToIgnoreCase;
print(function.applyAsInt("java8", "JAVA8"));
//동일한 문자열입니다.
}

private static void print(int order) {
if (order < 0) {System.out.println("사전 순으로 먼저 옵니다."); }
else if (order == 0) { System.out.println("동일한 문자열입니다."); }
else {System.out.println("사전순으로 나중에 옵니다."); }
}
}



생정자 참조

(a, b)  -> {return new 클래스(a,b);}   --- > 클래스 :: new

Exam

public class Member {
private String name;
private String id;

public Member() {
System.out.println("Member() 실행");
}

public Member(String id) {
System.out.println("Member(String id) 실행");
this.id = id;
}

public Member(String name, String id) {
System.out.println("Member(String name, String id) 실행");
this.name = name;
this.id = id;
}
}
import java.util.function.BiFunction;
import java.util.function.Function;

public class ConstructorReferencesExample {
public static void main(String[] args) {
Function<String, Member> function1 = Member::new;
Member member1 = function1.apply("angel");
// Member(String id) 실행

BiFunction<String, String, Member> function2 = Member::new;
Member member2 = function2.apply("김나박", "angel");
//Member(String name, String id) 실행
}
}


+ Recent posts