1. 메서드 참조(method reference)
개념: 람다 표현식의 축약된 표현방법
//1. static 메서드 사용: 문법: 클래스명::static메서드명
//2. 인스턴스 메서드 사용 : 문법: 참조변수명::메서드명
단, 참조변수가 파라미터인 경우에는 클래스명::메서드명 형식사용.
// 3. new 이용한 생성자 호출 문법: 클래스명::new
// 4. sysout 문법: System.out::println
package exam23_표준API함수형인터페이스;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
class Calc {
public static int methodA(int x, int y) { return x + y; }
public int methodB(int x, int y) { return x * y; }
public Calc() { }
public Calc(int x, String x2) {}
}
public class Functional07_메서드호출 {
public static void main(String[] args) {
// 메서드 호출(method reference) : 람다 표현식의 축약된 표현식
// 1. static 메서드 사용
// 문법: 클래스명::static 메서드명
BinaryOperator<Integer> s = (a, b) -> { return Calc.methodA(a, b); };
// 축약 ↓
BinaryOperator<Integer> s2 = (a, b) -> Calc.methodA(a, b);
// 메서드호출 ↓
BinaryOperator<Integer> s3 = Calc::methodA;
int result = s3.apply(10, 20);
System.out.println(result); // 30
// "100" ---> 100
Function<String, Integer> f = x -> { return Integer.parseInt(x); };
Function<String, Integer> f2 = x -> Integer.parseInt(x);
Function<String, Integer> f3 = Integer::parseInt;
int result2 = f3.apply("100");
System.out.println(result2); // 30
// 2. 인스턴스 메서드 사용
Calc c = new Calc();
BinaryOperator<Integer> k = (a,b) -> { return c.methodB(a, b); };
BinaryOperator<Integer> k2 = (a,b) -> c.methodB(a, b);
BinaryOperator<Integer> k3 = c::methodB;
int result3 = k3.apply(10, 20);
System.out.println(result3); // 200
// "HeLLO" --> "HELLO"
UnaryOperator<String> y = (String x) -> { return x.toUpperCase(); };
UnaryOperator<String> y2 = x -> { return x.toUpperCase(); };
UnaryOperator<String> y3 = x -> x.toUpperCase();
UnaryOperator<String> y4 = String::toUpperCase;
String result4 = y4.apply("HeLLO"); // HELLO
System.out.println(result4);
// 3. new 이용한 생성자 호출
// 호출하면 객체생성후 반환
Supplier<Calc> xx = () -> { return new Calc(); };
Supplier<Calc> xx2 = () -> new Calc();
Supplier<Calc> xx3 = Calc::new;
Calc yy = xx3.get();
System.out.println(yy.methodB(10, 10)); // 100
// int 와 String 전달해서 객체생성후 반환
BiFunction<Integer, String, Calc> kkk = (a,b) -> { return new Calc(a,b); };
BiFunction<Integer, String, Calc> kkk2 = (a,b) -> new Calc(a,b);
BiFunction<Integer, String, Calc> kkk3 = Calc::new;
// 4. sysout
Consumer<String> cc = x -> System.out.println(x);
Consumer<String> cc2 = System.out::println;
cc2.accept("Hello"); // Hello
}
}
'[study]이론정리 > JAVA(eclipse)' 카테고리의 다른 글
java 14 일차 _ 03. 스트림(중간처리) (0) | 2024.03.27 |
---|---|
java 14 일차 _ 02. 스트림 (0) | 2024.03.27 |
java 13일차 _ 01. 제네릭스( generics ) / 컬렉션 API / 표준API함수형인터페이스 (0) | 2024.03.26 |
java 13 일차 _ 12일차 정리 (0) | 2024.03.26 |
java 12일차_ 02. 제네릭스( generics ) (0) | 2024.03.25 |