Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- string
- sorting
- greedy
- geometry
- SQL
- 코딩테스트
- 코테
- Number Theory
- dynamic programming
- database
- Method
- Counting
- array
- Math
- java
- 구현
- implement
- bit manipulation
- two pointers
- Class
- 자바
- Data Structure
- Matrix
- Binary Search
- Tree
- hash table
- Binary Tree
- Stack
- simulation
- 파이썬
Archives
- Today
- Total
코린이의 소소한 공부노트
람다식과 메서드 참조 본문
[메서드 참조]
- 하나의 메서드만 호출하는 람다식을 더 간단하게 표현하는 방법
1. 메서드별 참조 방법
// 1) static메서드 참조
(x) -> ClassName.method(x) // 이 람다식을
ClassName::method // 이렇게 참조 가능
// 2) 인스턴스메서드 참조
(obj, x) -> obj.method(x) // 이 람다식을
ClassName::method // 이렇게 참조 가능
// 3) 특정 객체 인스턴스메서드 참조 - 거의 사용하지 않는다.
(x) -> obj.method(x) // 이 람다식을
obj::method // 이렇게 참조 가능
// 결국 우리가 사용할 메서드 참조 방법은
// 클래스이름::메서드이름 형태
// 메서드 참조를 보고 이해가 되지 않는다면
// 다시 람다식으로 바꿔보면 이해하는 데 도움이 된다.
2. 메서드 참조 예시 코드
// 1) static메서드 참조
Integer method(String s) { // parseInt() 호출 기능만 있다.
return Integer.parseInt(s);
}
int result = obj.method("123"); // 그래서 method()를 호출하나
int result = Integer.parseInt("123"); // 직접 parseInt()를 호출하나 차이가 없다.
// 그래서 method()를 람다식으로 간단하게 만들거나
Function<String, Integer> f = (String s) -> Integer.parseInt(s);
// 메서드 참조를 이용해 더 간단하게 만들 수 있다.
Function<String, Integer> f = Integer::parseInt; // 메서드 참조
// 2) 인스턴스 메서드 참조
BiFunction<String,String,Boolean> f = (s1, s2) -> s1.equals(s2); // 람다식
BiFunction<String,String,Boolean> f = String::equals; // 메서드 참조
// 3) 특정 객체의 인스턴스 메서드 참조
MyClass obj = new MyClass();
Function<String, Boolean> f = (x) -> obj.equals(x); // 람다식
Function<String, Boolean> f2 = obj::equals; // 메서드 참조
3. new 연산자(생성자, 배열)와 메서드 참조
// 1) 기본 생성자
Supplier<MyClass> s = () -> new MyClass(); // 람다식
Supplier<MyClass> s = MyClass::new; // 메서드 참조
// 2) 매개변수가 있는 생성자
Function<Integer, MyClass> s = (i) -> new MyClass(i); // 람다식
Function<Integer, MyClass> s = MyClass::new; // 메서드 참조
// 매개변수가 2개라면 BiFunction<T,U,R> 이용. T,U가 입력, R이 출력
// 3) 배열 생성
Function<Integer, int[]> f = x -> new int[x]; // 람다식
Function<Integer, int[]> f = int[]::new; // 메서드 참조
4. new 연산자 메서드 참조 예시 코드
// 1) 기본 생성자
class MyClass {}
// main()
// Supplier는 매개변수X, 반환값O
// Supplier<MyClass> s = () -> new MyClass(); // 람다식
Supplier<MyClass> s = MyClass::new; // 메서드 참조
System.out.println(s.get()); // s로 만든 객체의 해시코드가 출력된다.
System.out.println(s.get()); // 위와 다른 해시코드가 출력된다.
// 2) 매개변수가 1개인 생성자
class MyClass2 {
int iv;
MyClass2(int iv){ this.iv = iv;}
}
// main()
// Function은 매개변수O, 반환값O
// Function<Integer, MyClass2> f = (i) -> new MyClass2(i); // 람다식
Function<Integer, MyClass2> f = MyClass2::new; // 메서드 참조
System.out.println(f.apply(3)); // f로 만든 객체의 해시코드가 출력된다.
System.out.println(f.apply(4).iv); // 4
// 3) 배열 생성
// 배열은 길이를 입력으로 줘야 생성이 된다.
// Function<Integer, int[]> a = (i) -> new int[i]; // 람다식
Function<Integer, int[]> a = int[]::new; // 메서드 참조
System.out.println(a.apply(5).length); // 5
'Java' 카테고리의 다른 글
스트림 생성하기 (0) | 2023.02.01 |
---|---|
스트림의 정의와 특징 (0) | 2023.01.30 |
function 패키지와 메서드 (0) | 2023.01.12 |
함수형 인터페이스의 정의와 활용 (0) | 2022.12.29 |
람다식의 정의와 작성 방법 (0) | 2022.12.28 |