Skip to content

Commit 74100bc

Browse files
committed
Today I Learned Method Ref
1 parent d0ffd18 commit 74100bc

11 files changed

Lines changed: 318 additions & 0 deletions

‎methodref/MethodRef3.java‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package methodref;
2+
3+
import java.util.function.Function;
4+
5+
public class MethodRef3 {
6+
7+
public static void main(String[] args) {
8+
9+
// 임의 객체의 인스턴스 메서드 참조(특정 타입의)
10+
Person person1 = new Person("Kim");
11+
Person person2 = new Person("Park");
12+
Person person3 = new Person("Lee");
13+
14+
// 람다
15+
Function<Person, String> fun1 = (Person person) -> person.introduce();
16+
System.out.println("person1 = " + fun1.apply(person1));
17+
System.out.println("person2 = " + fun1.apply(person2));
18+
System.out.println("person3 = " + fun1.apply(person3));
19+
System.out.println();
20+
21+
// 메서드 참조, 타입이 첫 번째 매개변수가 됨
22+
// 그리고 첫 번쨰 매개변수의 메서드를 호출, 나머지는 순서대로 매개변수에 전달
23+
Function<Person, String> fun2 = Person::introduce; // 타입::인스턴스 메서드 (타입 이름) -> 이름.메서드
24+
System.out.println("person1 = " + fun2.apply(person1));
25+
System.out.println("person2 = " + fun2.apply(person2));
26+
System.out.println("person3 = " + fun2.apply(person3));
27+
28+
29+
}
30+
}

‎methodref/MethodRef4.java‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package methodref;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.function.Function;
6+
7+
public class MethodRef4 {
8+
public static void main(String[] args) {
9+
List<Person> personList = List.of(new Person("Kim"),
10+
new Person("Park"),
11+
new Person("Lee")
12+
);
13+
List<String> result1 = mapPersonToString(personList, (Person p) -> p.introduce());
14+
List<String> result2 = mapPersonToString(personList, Person::introduce);
15+
System.out.println("result1 = " + result1);
16+
System.out.println("result2 = " + result2);
17+
18+
List<String> stringResult1 = mapStringToString(result1, (String s)-> s.toUpperCase());
19+
List<String> stringResult2 = mapStringToString(result1, String::toUpperCase);
20+
System.out.println("stringResult1 = " + stringResult1);
21+
22+
}
23+
24+
public static List<String> mapPersonToString(List<Person> personList, Function<Person, String> fun){
25+
List<String> result = new ArrayList<>();
26+
for (Person p : personList) {
27+
String apply = fun.apply(p);
28+
result.add(apply);
29+
}
30+
return result;
31+
}
32+
33+
public static List<String> mapStringToString(List<String> stringList, Function<String, String> fun){
34+
List<String> result = new ArrayList<>();
35+
for (String p : stringList) {
36+
String apply = fun.apply(p);
37+
result.add(apply);
38+
}
39+
return result;
40+
}
41+
}

‎methodref/MethodRef5.java‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package methodref;
2+
3+
import lambda.lambda5.mystream.MyStreamV3;
4+
5+
import java.util.List;
6+
7+
public class MethodRef5 {
8+
public static void main(String[] args) {
9+
List<Person> personList = List.of(
10+
new Person("Kim"),
11+
new Person("Park"),
12+
new Person("Lee")
13+
);
14+
15+
List<String> result1 = MyStreamV3.of(personList)
16+
.map(person -> person.introduce())
17+
.map(str -> str.toUpperCase())
18+
.toList();
19+
System.out.println("result1 = " + result1);
20+
21+
List<String> result2 = MyStreamV3.of(personList)
22+
.map(Person::introduce)
23+
.map(String::toUpperCase)
24+
.toList();
25+
System.out.println("result2 = " + result2);
26+
}
27+
}

‎methodref/MethodeRefEx1.java‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package methodref;
2+
3+
import java.util.function.Supplier;
4+
5+
public class MethodeRefEx1 {
6+
public static void main(String[] args) {
7+
8+
// 1. 정적 메서드 참조
9+
Supplier<String> staticMethod1 = () -> Person.greeting();
10+
Supplier<String> staticMethod2 = Person::greeting; // 클래스::정적 메서드
11+
12+
System.out.println("staticMethod1 = " + staticMethod1.get());
13+
System.out.println("staticMethod2 = " + staticMethod2.get());
14+
15+
// 2. 특정 객체의 인스턴스 참조
16+
Person person = new Person("Choi");
17+
Supplier<String> instanceMethod1 = () -> person.introduce();
18+
Supplier<String> instanceMethod2 = person::introduce; // 객체::인스턴스메서드
19+
20+
System.out.println("instanceMethod1 = " + instanceMethod1.get());
21+
System.out.println("instanceMethod2 = " + instanceMethod2.get());
22+
23+
// 3. 생성자 참조
24+
Supplier<Person> newPerson1 = () -> new Person();
25+
Supplier<Person> newPerson2 = Person::new; // 클래스::new
26+
27+
System.out.println("newPerson1 = " + newPerson1.get());
28+
System.out.println("newPerson2 = " + newPerson2.get());
29+
30+
31+
}
32+
}

‎methodref/MethodeRefEx2.java‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package methodref;
2+
3+
import java.util.function.Function;
4+
import java.util.function.Supplier;
5+
6+
public class MethodeRefEx2 {
7+
public static void main(String[] args) {
8+
9+
// 1. 정적 메서드 참조
10+
Function<String, String> staticMethod1 = (name) -> Person.greetingWithName(name);
11+
Function<String, String> staticMethod2 = Person::greetingWithName;
12+
13+
System.out.println("staticMethod1 = " + staticMethod1.apply("Kim"));
14+
System.out.println("staticMethod2 = " + staticMethod2.apply("Kim"));
15+
16+
// 2. 특정 객체의 인스턴스 참조
17+
Person person = new Person("Choi");
18+
Function<Integer, String> instanceMethod1 = (n) -> person.introduceWithNumber(n);
19+
Function<Integer, String> instanceMethod2 = person::introduceWithNumber; // 객체::인스턴스메서드
20+
21+
System.out.println("instanceMethod1 = " + instanceMethod1.apply(1));
22+
System.out.println("instanceMethod2 = " + instanceMethod2.apply(2));
23+
24+
// 3. 생성자 참조
25+
Function<String, Person> newPerson1 = (name) -> new Person(name);
26+
Function<String, Person> newPerson2 = Person::new; // 클래스::new
27+
28+
System.out.println("newPerson1 = " + newPerson1.apply("Choi"));
29+
System.out.println("newPerson2 = " + newPerson2.apply("Choi"));
30+
31+
32+
}
33+
}

‎methodref/MethodeRefEx6.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package methodref;
2+
3+
import java.util.function.BiFunction;
4+
5+
// 매개 변수 추가
6+
public class MethodeRefEx6 {
7+
public static void main(String[] args) {
8+
// 4. 임의 객체의 인스턴스 메서드 참조(특정 타입의)
9+
Person person = new Person("Kim");
10+
11+
// 람다
12+
BiFunction<Person, Integer, String> fun1 =
13+
(Person p, Integer n) -> p.introduceWithNumber(n);
14+
15+
System.out.println("person.introduceWithNumber = " + fun1.apply(person, 5));
16+
17+
// 메서드 참조, 타입이 첫 번쨰 매개변수가 됨, 그리고 첫 번째 매개변수의 메서드를 호출
18+
// 나머지는 순서대로 매개변수에 전달
19+
BiFunction<Person, Integer, String> fun2 = Person::introduceWithNumber; // 타입::메서드 명
20+
System.out.println("person.introduceWithNumber = " + fun2.apply(person, 10));
21+
22+
23+
24+
}
25+
}

‎methodref/Person.java‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package methodref;
2+
3+
public class Person {
4+
String name;
5+
6+
public Person() {
7+
this("unkown");
8+
}
9+
10+
public Person(String name) {
11+
this.name = name;
12+
}
13+
14+
// 정적 매서드
15+
public static String greeting() {
16+
return "hello";
17+
}
18+
19+
// 정적 매서드 + 매개변수
20+
public static String greetingWithName(String name) {
21+
return "hello " + name;
22+
}
23+
24+
public String getName() {
25+
return name;
26+
}
27+
28+
// 인스턴스 메서드
29+
public String introduce(){
30+
return "I am " + name;
31+
}
32+
33+
// 인스턴스 메서드, 매개변수
34+
public String introduceWithNumber(int number){
35+
return "I am " + name + ", my number is " + number;
36+
}
37+
38+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package methodref.start;
2+
3+
import java.util.function.BinaryOperator;
4+
5+
public class MethodRefStartV1 {
6+
public static void main(String[] args) {
7+
BinaryOperator<Integer> add1 = (x,y) -> x+y;
8+
BinaryOperator<Integer> add2 = (x,y) -> x+y;
9+
10+
Integer result1 = add1.apply(1,2);
11+
System.out.println("result1 = " + result1);
12+
13+
Integer result2 = add2.apply(3,4);
14+
System.out.println("result2 = " + result2);
15+
}
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package methodref.start;
2+
3+
import java.util.function.BinaryOperator;
4+
5+
public class MethodRefStartV2 {
6+
public static void main(String[] args) {
7+
BinaryOperator<Integer> add1 = (x,y) -> add(x,y);
8+
BinaryOperator<Integer> add2 = (x,y) -> add(x,y);
9+
10+
Integer result1 = add1.apply(1,2);
11+
System.out.println("result1 = " + result1);
12+
13+
Integer result2 = add2.apply(3,4);
14+
System.out.println("result2 = " + result2);
15+
}
16+
17+
public static int add(int x, int y){
18+
return x + y;
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package methodref.start;
2+
3+
import java.util.function.BinaryOperator;
4+
5+
public class MethodRefStartV3 {
6+
public static void main(String[] args) {
7+
BinaryOperator<Integer> add1 = MethodRefStartV3::add;
8+
BinaryOperator<Integer> add2 = MethodRefStartV3::add;
9+
10+
Integer result1 = add1.apply(1,2);
11+
System.out.println("result1 = " + result1);
12+
13+
Integer result2 = add2.apply(3,4);
14+
System.out.println("result2 = " + result2);
15+
}
16+
17+
public static int add(int x, int y){
18+
return x + y;
19+
}
20+
}

0 commit comments

Comments
 (0)