티스토리 뷰
● 도입
- 함수가 1급 시민이 되기 위해서는 함수를 object 형태로 나타내야한다.
- 이를 가능하게 해주는 것이 java의 Function Interface이다.
- 핵심 : 함수를 object 형태로 나타내보자
프로그래밍에서 1급 시민의 조건
1. 함수/메서드의 매개변수(parameter)로서 전달할 수 있는가
2. 함수/메서드의 반환값(return)이 될 수 있는가
3. 변수에 담을 수 있는가
● Function Interface

- 위의 interface 중에서 Function이라는 Interface에 대해 알아보자
// Function interface는 아래와 같이 생겼다.
@FunctionalInterface
public interface Function<T, R> {
R apply(T t); // 추상 메소드
// T : apply method의 input type
// R : apply method의 return type
}
- Function interface는 T type의 input을 받아 R type의 값을 return 하는 함수를 interface 형태로 구현한 것이다.
● 실습
- Integer를 받아 (+10)을 해서 return을 해주는 함수를 object의 형태로 나타내보자
[Adder class]
package com.fastcampus.functionalprogramming.chapter3.util;
import java.util.function.Function;
public class Adder implements Function<Integer, Integer>{
@Override
public Integer apply(Integer x) { // function interface를 구현할 함수
return x + 10;
}
}
[Chapter3Section1 class]
package com.fastcampus.functionalprogramming.chapter3;
import java.util.function.Function;
import com.fastcampus.functionalprogramming.chapter3.util.Adder;
public class Chapter3Section1 {
public static void main(String[] args) {
// Function interface를 이용해
// Integer를 받아 (+10)을 해서 return을 해주는 '함수를 object의 형태로 구현'했다.
Function<Integer, Integer> myAdder = new Adder();
int result = myAdder.apply(5);
System.out.println("result : " + result);
}
}

'Backend > Java8' 카테고리의 다른 글
#6 Supplier (0) | 2022.09.18 |
---|---|
#5 @FunctionalInterface / input을 3개 받는 interface 만들기 (1) | 2022.09.17 |
#4 BiFunction Interface(매개변수가 두 개일 때) (0) | 2022.09.16 |
#3 Lambda(이름이 없는 함수, Anonymous function) : 함수형 인터페이스를 구현하는 가장 간단한 방법 (0) | 2022.09.15 |
#1 함수형 프로그래밍, 1급 시민으로서의 함수 (0) | 2022.09.13 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- node.js
- SpringBoot
- 프로세스
- db
- 메모리
- 자료구조
- MongoDB
- 코딩테스트
- Spring Boot
- Advanced Stream
- nosql
- Java8
- spring
- SQL
- java
- 빅데이터
- Stream
- Phaser3
- DART
- git
- 프로그래머스
- OS
- 코테
- 알고리즘
- 빅데이터 분석기사
- MySQL
- Phaser
- 운영체제
- API
- jpa
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함