일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- MariaDB
- systemd-resolved
- k8s
- PVC
- RDB
- Spring boot
- 메가바이트스쿨
- MegabyteSchool
- L2 통신
- reclaim
- CoreDNS
- linux dns
- ssh tunneling
- DNS
- dns forward
- PV
- 내일배움카드
- L2 통작
- 국비지원교육
- ARP
- 127.0.0.53
- 개발자취업부트캠프
- linux domain
- 패스트캠퍼스
- Layer 2
- Today
- Total
hoonii2
[Java] 07-2. Lambda Method Reference 본문
참고 자료
https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html
Method References (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. See Java Language Changes for a summary of updated
docs.oracle.com
0. 개요
Method Reference 는 Method 를 이름으로 호출할 수 있어 명확하고 간결하게 사용할 수 있다.
0-1. 람다를 표현식으로 사용했을 경우
public class Person {
// ...
LocalDate birthday;
public int getAge() {
// ...
}
public LocalDate getBirthday() {
return birthday;
}
public static int compareByAge(Person a, Person b) {
return a.birthday.compareTo(b.birthday);
}
// ...
}
Comparator 를 직접 Class 로 만들어서 사용 가능
Person[] rosterAsArray = roster.toArray(new Person[roster.size()]);
class PersonAgeComparator implements Comparator<Person> {
public int compare(Person a, Person b) {
return a.getBirthday().compareTo(b.getBirthday());
}
}
Arrays.sort(rosterAsArray, new PersonAgeComparator());
Comparator 는 함수형 인터페이스이기 때문에 람다 사용 가능
Arrays.sort(rosterAsArray,
(Person a, Person b) -> {
return a.getBirthday().compareTo(b.getBirthday());
}
);
하지만 람다 대신 'Person' 내에 이미 Compareto 하는 Method 가 있기 때문에 해당 메소드를 호출할 수 있다.
Arrays.sort(rosterAsArray,
(a, b) -> Person.compareByAge(a, b)
);
이를 메소드 호출로 사용 가능하다.
훨신 간결하고 가독성도 좋아진다.
Arrays.sort(rosterAsArray, Person::compareByAge);
1. Static Method 참조
import java.util.function.BiFunction;
public class MethodReferencesExamples {
public static <T> T mergeThings(T a, T b, BiFunction<T, T, T> merger) {
return merger.apply(a, b);
}
public static String appendStrings(String a, String b) {
return a + b;
}
public static void main(String[] args) {
MethodReferencesExamples myApp = new MethodReferencesExamples();
// Calling the method mergeThings with a lambda expression
System.out.println(MethodReferencesExamples.
mergeThings("Hello ", "World!", (a, b) -> a + b));
// Reference to a static method
System.out.println(MethodReferencesExamples.
mergeThings("Hello ", "World!", MethodReferencesExamples::appendStrings));
}
}
2. 특정 인스턴스 메소드 참조
import java.util.function.BiFunction;
public class MethodReferencesExamples {
public static <T> T mergeThings(T a, T b, BiFunction<T, T, T> merger) {
return merger.apply(a, b);
}
public String appendStrings2(String a, String b) {
return a + b;
}
public static void main(String[] args) {
MethodReferencesExamples myApp = new MethodReferencesExamples();
// Reference to an instance method of a particular object
System.out.println(MethodReferencesExamples.
mergeThings("Hello ", "World!", myApp::appendStrings2));
}
}
3. 특정 유형의 인스턴스 메소드 참조
import java.util.function.BiFunction;
public class MethodReferencesExamples {
public static <T> T mergeThings(T a, T b, BiFunction<T, T, T> merger) {
return merger.apply(a, b);
}
public static void main(String[] args) {
MethodReferencesExamples myApp = new MethodReferencesExamples();
// Reference to an instance method of an arbitrary object of a
// particular type
System.out.println(MethodReferencesExamples.
mergeThings("Hello ", "World!", String::concat));
}
}
4, 생성자 참조
아래 두 코드는 동일한 동작을 한다.
String::new
() -> new String()
'개념 공부 > (개발) 01. Java' 카테고리의 다른 글
[Java] 08. Stream (0) | 2023.01.06 |
---|---|
[Java] 07-3. 기본 제공 함수형 인터페이스 (0) | 2022.12.23 |
[Java] 07. Lambda Expression (0) | 2022.12.09 |
[Java] 06. Local Class / Anonymous Class (0) | 2022.12.02 |
[Java] 05. Interface (0) | 2022.11.25 |