You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@FunctionalInterfacepublicinterfaceRunnable {
/** * When an object implementing interface <code>Runnable</code> is used * to create a thread, starting the thread causes the object's * <code>run</code> method to be called in that separately executing * thread. * <p> * The general contract of the method <code>run</code> is that it may * take any action whatsoever. * * @see java.lang.Thread#run() */publicabstractvoidrun();
}
기존부터 존재하던 인터페이스로 스레드를 생성할때 주로사용하였으며 가장 기본적인 함수형 인터페이스다.
@FunctionalInterfacepublicinterfaceSupplier<T> {
/** * Gets a result. * * @return a result */Tget();
}
항상 같은 값을 반환하는 메서드
Example
Supplier<String> s = () -> "hello supplier";
Stringresult = s.get();
Consumer<T>
Inteface
@FunctionalInterfacepublicinterfaceConsumer<T> {
/** * Performs this operation on the given argument. * * @param t the input argument */voidaccept(Tt);
/** * Returns a composed {@code Consumer} that performs, in sequence, this * operation followed by the {@code after} operation. If performing either * operation throws an exception, it is relayed to the caller of the * composed operation. If performing this operation throws an exception, * the {@code after} operation will not be performed. * * @param after the operation to perform after this operation * @return a composed {@code Consumer} that performs in sequence this * operation followed by the {@code after} operation * @throws NullPointerException if {@code after} is null */defaultConsumer<T> andThen(Consumer<? superT> after) {
Objects.requireNonNull(after);
return (Tt) -> { accept(t); after.accept(t); };
}
}
리턴하지 않고, 인자를 받는 메서드를 가지고 있다. 인자를 받아 소모한다.
Example
Consumer<String> c = str -> System.out.println(str);
c.accept("hello consumer");
Function<T, R>
Interface
@FunctionalInterfacepublicinterfaceFunction<T, R> {
/** * Applies this function to the given argument. * * @param t the function argument * @return the function result */Rapply(Tt);
/** * Returns a composed function that first applies the {@code before} * function to its input, and then applies this function to the result. * If evaluation of either function throws an exception, it is relayed to * the caller of the composed function. * * @param <V> the type of input to the {@code before} function, and to the * composed function * @param before the function to apply before this function is applied * @return a composed function that first applies the {@code before} * function and then applies this function * @throws NullPointerException if before is null * * @see #andThen(Function) */default <V> Function<V, R> compose(Function<? superV, ? extendsT> before) {
Objects.requireNonNull(before);
return (Vv) -> apply(before.apply(v));
}
/** * Returns a composed function that first applies this function to * its input, and then applies the {@code after} function to the result. * If evaluation of either function throws an exception, it is relayed to * the caller of the composed function. * * @param <V> the type of output of the {@code after} function, and of the * composed function * @param after the function to apply after this function is applied * @return a composed function that first applies this function and then * applies the {@code after} function * @throws NullPointerException if after is null * * @see #compose(Function) */default <V> Function<T, V> andThen(Function<? superR, ? extendsV> after) {
Objects.requireNonNull(after);
return (Tt) -> after.apply(apply(t));
}
/** * Returns a function that always returns its input argument. * * @param <T> the type of the input and output objects to the function * @return a function that always returns its input argument */static <T> Function<T, T> identity() {
returnt -> t;
}
}
인터페이스 명칭에서부터 알 수 있듯이 전형적인 함수를 지원한다고 보면 된다. 하나의 인자와 리턴타입을 가지며 그걸 제네릭으로 지정해줄수있다. 그래서 타입파라미터(Type Parameter)가 2개다.
Example
Function<String, Integer> f = str -> Integer.parseInt(str);
Integerresult = f.apply("1");
Predicate
Interface
@FunctionalInterfacepublicinterfacePredicate<T> {
/** * Evaluates this predicate on the given argument. * * @param t the input argument * @return {@code true} if the input argument matches the predicate, * otherwise {@code false} */booleantest(Tt);
/** * Returns a composed predicate that represents a short-circuiting logical * AND of this predicate and another. When evaluating the composed * predicate, if this predicate is {@code false}, then the {@code other} * predicate is not evaluated. * * <p>Any exceptions thrown during evaluation of either predicate are relayed * to the caller; if evaluation of this predicate throws an exception, the * {@code other} predicate will not be evaluated. * * @param other a predicate that will be logically-ANDed with this * predicate * @return a composed predicate that represents the short-circuiting logical * AND of this predicate and the {@code other} predicate * @throws NullPointerException if other is null */defaultPredicate<T> and(Predicate<? superT> other) {
Objects.requireNonNull(other);
return (t) -> test(t) && other.test(t);
}
/** * Returns a predicate that represents the logical negation of this * predicate. * * @return a predicate that represents the logical negation of this * predicate */defaultPredicate<T> negate() {
return (t) -> !test(t);
}
/** * Returns a composed predicate that represents a short-circuiting logical * OR of this predicate and another. When evaluating the composed * predicate, if this predicate is {@code true}, then the {@code other} * predicate is not evaluated. * * <p>Any exceptions thrown during evaluation of either predicate are relayed * to the caller; if evaluation of this predicate throws an exception, the * {@code other} predicate will not be evaluated. * * @param other a predicate that will be logically-ORed with this * predicate * @return a composed predicate that represents the short-circuiting logical * OR of this predicate and the {@code other} predicate * @throws NullPointerException if other is null */defaultPredicate<T> or(Predicate<? superT> other) {
Objects.requireNonNull(other);
return (t) -> test(t) || other.test(t);
}
/** * Returns a predicate that tests if two arguments are equal according * to {@link Objects#equals(Object, Object)}. * * @param <T> the type of arguments to the predicate * @param targetRef the object reference with which to compare for equality, * which may be {@code null} * @return a predicate that tests if two arguments are equal according * to {@link Objects#equals(Object, Object)} */static <T> Predicate<T> isEqual(ObjecttargetRef) {
return (null == targetRef)
? Objects::isNull
: object -> targetRef.equals(object);
}
}
불리언 타입을 반환한는 인터페이스
Example
Predicate<String> p = str -> str.isEmpty();
booleanresult = p.test("hello");
UnaryOperator<T>
Interface
@FunctionalInterfacepublicinterfaceUnaryOperator<T> extendsFunction<T, T> {
/** * Returns a unary operator that always returns its input argument. * * @param <T> the type of the input and output of the operator * @return a unary operator that always returns its input argument */static <T> UnaryOperator<T> identity() {
returnt -> t;
}
}
@FunctionalInterfacepublicinterfaceBinaryOperator<T> extendsBiFunction<T,T,T> {
/** * Returns a {@link BinaryOperator} which returns the lesser of two elements * according to the specified {@code Comparator}. * * @param <T> the type of the input arguments of the comparator * @param comparator a {@code Comparator} for comparing the two values * @return a {@code BinaryOperator} which returns the lesser of its operands, * according to the supplied {@code Comparator} * @throws NullPointerException if the argument is null */publicstatic <T> BinaryOperator<T> minBy(Comparator<? superT> comparator) {
Objects.requireNonNull(comparator);
return (a, b) -> comparator.compare(a, b) <= 0 ? a : b;
}
/** * Returns a {@link BinaryOperator} which returns the greater of two elements * according to the specified {@code Comparator}. * * @param <T> the type of the input arguments of the comparator * @param comparator a {@code Comparator} for comparing the two values * @return a {@code BinaryOperator} which returns the greater of its operands, * according to the supplied {@code Comparator} * @throws NullPointerException if the argument is null */publicstatic <T> BinaryOperator<T> maxBy(Comparator<? superT> comparator) {
Objects.requireNonNull(comparator);
return (a, b) -> comparator.compare(a, b) >= 0 ? a : b;
}
}
@FunctionalInterfacepublicinterfaceBiPredicate<T, U> {
/** * Evaluates this predicate on the given arguments. * * @param t the first input argument * @param u the second input argument * @return {@code true} if the input arguments match the predicate, * otherwise {@code false} */booleantest(Tt, Uu);
/** * Returns a composed predicate that represents a short-circuiting logical * AND of this predicate and another. When evaluating the composed * predicate, if this predicate is {@code false}, then the {@code other} * predicate is not evaluated. * * <p>Any exceptions thrown during evaluation of either predicate are relayed * to the caller; if evaluation of this predicate throws an exception, the * {@code other} predicate will not be evaluated. * * @param other a predicate that will be logically-ANDed with this * predicate * @return a composed predicate that represents the short-circuiting logical * AND of this predicate and the {@code other} predicate * @throws NullPointerException if other is null */defaultBiPredicate<T, U> and(BiPredicate<? superT, ? superU> other) {
Objects.requireNonNull(other);
return (Tt, Uu) -> test(t, u) && other.test(t, u);
}
/** * Returns a predicate that represents the logical negation of this * predicate. * * @return a predicate that represents the logical negation of this * predicate */defaultBiPredicate<T, U> negate() {
return (Tt, Uu) -> !test(t, u);
}
/** * Returns a composed predicate that represents a short-circuiting logical * OR of this predicate and another. When evaluating the composed * predicate, if this predicate is {@code true}, then the {@code other} * predicate is not evaluated. * * <p>Any exceptions thrown during evaluation of either predicate are relayed * to the caller; if evaluation of this predicate throws an exception, the * {@code other} predicate will not be evaluated. * * @param other a predicate that will be logically-ORed with this * predicate * @return a composed predicate that represents the short-circuiting logical * OR of this predicate and the {@code other} predicate * @throws NullPointerException if other is null */defaultBiPredicate<T, U> or(BiPredicate<? superT, ? superU> other) {
Objects.requireNonNull(other);
return (Tt, Uu) -> test(t, u) || other.test(t, u);
}
}
@FunctionalInterfacepublicinterfaceBiConsumer<T, U> {
/** * Performs this operation on the given arguments. * * @param t the first input argument * @param u the second input argument */voidaccept(Tt, Uu);
/** * Returns a composed {@code BiConsumer} that performs, in sequence, this * operation followed by the {@code after} operation. If performing either * operation throws an exception, it is relayed to the caller of the * composed operation. If performing this operation throws an exception, * the {@code after} operation will not be performed. * * @param after the operation to perform after this operation * @return a composed {@code BiConsumer} that performs in sequence this * operation followed by the {@code after} operation * @throws NullPointerException if {@code after} is null */defaultBiConsumer<T, U> andThen(BiConsumer<? superT, ? superU> after) {
Objects.requireNonNull(after);
return (l, r) -> {
accept(l, r);
after.accept(l, r);
};
}
}
@FunctionalInterfacepublicinterfaceBiFunction<T, U, R> {
/** * Applies this function to the given arguments. * * @param t the first function argument * @param u the second function argument * @return the function result */Rapply(Tt, Uu);
/** * Returns a composed function that first applies this function to * its input, and then applies the {@code after} function to the result. * If evaluation of either function throws an exception, it is relayed to * the caller of the composed function. * * @param <V> the type of output of the {@code after} function, and of the * composed function * @param after the function to apply after this function is applied * @return a composed function that first applies this function and then * applies the {@code after} function * @throws NullPointerException if after is null */default <V> BiFunction<T, U, V> andThen(Function<? superR, ? extendsV> after) {
Objects.requireNonNull(after);
return (Tt, Uu) -> after.apply(apply(t, u));
}
}