Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
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
Tags more
Archives
Today
Total
관리 메뉴

Formoat's Open Blog

함수적 인터페이스 - Function 본문

Java/Java Basic

함수적 인터페이스 - Function

snd-snd 2019. 9. 3. 22:04

1. Function

1) 매개값과 반환값이 모두 있는 함수적 인터페이스

2) 매개값과 반환값이 모두 있는 applyXXX() 메소드를 가지고 있다.

2) 이들 메소드는 매개값을 이용해 연산하고 다른타입으로 결과를 반환하는 역할을 한다.

4) Operator와 다른점은 매개값과 반환값의 타입이 다르다는 것이다.

 

Interface Name Abstract Method Remarks
Function<T, R> R apply(T t) 객체 T를 객체 R로 매핑 후 반환
BiFunction<T, U, R> R apply(T t, U u) 객체 T, U를 객체 R로 매핑 후 반환
DoubleFunction<R> R apply(double value) double 값을 객체 R로 매핑 후 반환
IntFunction<R> R apply(int value) int 값을 객체 R로 매핑 후 반환
IntToDoubleFunction double applyAsDouble(int value) int 값을 double 값으로 매핑 후 반환
IntToLongFunction long applyAsLong(int value) int 값을 long 값으로 매핑 후 반환
LongToDoubleFunction double applyAsDouble(long value) long 값을 double 값으로 매핑 후 반환
LongToIntFunction int applyAsint(long value) long 값을 int 값으로 매핑 후 반환
ToDoubleBiFunction<T, U> double applyAsDouble(T t, U u) 객체 T, U를 double 값으로 매핑 후 반환
ToDoubleFunction<T> double applyAsDouble(T t) 객체 T를 double 값으로 매핑 후 반환
ToIntBiFunction<T, U> int applyAsInt(T t, U u) 객체 T, U를 int 값으로 매핑 후 반환
ToIntFunction<T> int applyAsInt(T t) 객체 T를 int 값으로 매핑 후 반환
ToLongBiFunction<T, U> long applyAsLong(T t, U u) 객체 T, U를 long 값으로 매핑 후 반환
ToLongFunction<T> long applyAsLong(T t) 객체 T를 long 값으로 매핑 후 반환

<표> Function 함수적 인터페이스의 종류

 

 

 

 

FunctionExample.java

public class FunctionExample {
	
	public static List<Student> list = Arrays.asList(
			new Student("김태호", 90, 100),
			new Student("이호진", 90, 90),
			new Student("최웅진", 100, 90)
			);
	
	public static void printString(Function<Student, String> function) {
		for (Student student : list) {
			System.out.print(function.apply(student)+" ");
		}
		System.out.println();
	}
	
	public static void printInt(ToIntFunction<Student> function) {
		for (Student student : list) {
			System.out.print(function.applyAsInt(student)+" ");
		}
		System.out.println();
	}

	public static void main(String[] args) {
		
		Function<Student, String> function = student -> {
			return student.getName();
		};		
		printString(function);
		
		ToIntFunction<Student> toIntFunction = student -> {
			return student.getKorScore()+student.getEngScore();
		};
		printInt(toIntFunction);
	}
}

 

// 실행결과 //
----------------------------------------------------
김태호 이호진 최웅진 
190 180 190 
Comments