IntFunction

IntFunction (int 함수형 인터페이스)

int 값 인자를 받아 결과를 생성하는 함수를 나타내는 함수형 인터페이스예요. Function의 int 소비 원시 특수화예요.

출처: Java API Reference

본문

IntFunction<R>의 추상 메서드는 apply(int)로 int 인자를 받아 결과 R을 반환해요. 원시 int를 받으므로 박싱 없이 동작해요.

@FunctionalInterface
public interface IntFunction<R>
IntFunction<String> toHex = n -> Integer.toHexString(n);
IntFunction<int[]> sized = n -> new int[n];

String h = toHex.apply(255);   // "ff"
int[] arr = sized.apply(5);    // 크기 5 배열

대표적으로 IntStream.mapToObj에 사용해 int를 객체로 변환해요.

List<String> labels = IntStream.range(0, 3)
    .mapToObj(i -> "item-" + i)
    .toList(); // [item-0, item-1, item-2]

함수형 메서드는 apply(int)예요. 코드와 시그니처는 원문 그대로 보존돼요.

더 알아보기