日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java.util.function包

發布時間:2024/4/13 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java.util.function包 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

Supplier

參數個數擴展

參數類型擴展

特殊變形

Function,r>

參數個數擴展

參數類型擴展

特殊變形

Consumer

參數個數擴展

參數類型擴展

特殊變形

Predicate

參數個數擴展

參數類型擴展

特殊變形


java.util.function包下主要為函數接口,主要包含4類函數接口:

  • Supplier<T>: 數據提供器,可以提供 T 類型對象;無參的構造器,提供了 get 方法;
  • Function<T,R>: 數據轉換器,接收一個 T 類型的對象,返回一個 R類型的對象; 單參數單返回值的行為接口;提供了 apply, compose, andThen, identity 方法;
  • Consumer<T>: 數據消費器, 接收一個 T類型的對象,無返回值,通常用于設置T對象的值; 單參數無返回值的行為接口;提供了 accept, andThen 方法;
  • Predicate<T>: 條件測試器,接收一個 T 類型的對象,返回布爾值,通常用于傳遞條件函數; 單參數布爾值的條件性接口。提供了 test (條件測試) , and-or- negate(與或非) 方法。

Consumer與Function都有andThen方法,都返回一個function,但是意義不一樣。

Consumer是在輸入參數上先應用this對象的apply,再應用after的apply。

Function是先應用this對象的apply()方法,產生一個結果,再把此結果作為輸入參數,應用到after的apply()方法。

Consumer:

(T t, U u) -> after.apply(apply(t, u));

Function:

(T t) -> after.apply(apply(t));

?

Supplier<T>

@FunctionalInterface
public interface Supplier<T> {

??? /**
???? * Gets a result.
???? *
???? * @return a result
???? */
??? T get();
}

參數個數擴展

參數類型擴展

BooleanSupplier

public interface BooleanSupplier {
??? boolean getAsBoolean();
}

DoubleSupplier

public interface DoubleSupplier {
??? double getAsDouble();
}


IntSupplier

public interface IntSupplier {
??? int getAsInt();
}

?


LongSupplier

public interface LongSupplier {
??? long getAsLong();
}

?

特殊變形

Function<T,R>

@FunctionalInterface
public interface Function<T, R> {

??? /**
???? * Applies this function to the given argument.
???? */
??? R apply(T t);

??? /**
???? * Returns a composed function that first applies the {@code before}
???? * function to its input, and then applies this function to the result.
???? */
??? default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
??????? Objects.requireNonNull(before);

?????? //before.apply(v):輸入類型V,返回類型T

????? //最終函數:輸入類型T,返回類型R
??????? return (V v) -> 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.
???? */
??? default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
??????? Objects.requireNonNull(after);

?????? //apply(t):輸入類型T,返回類型R

????? //最終函數:輸入類型R,返回類型V

??????? return (T t) -> after.apply(apply(t));
??? }

??? /**
???? * Returns a function that always returns its input argument.
???? */
??? static <T> Function<T, T> identity() {
??????? return t -> t;
??? }
}

參數個數擴展

BiFunction

public interface BiFunction<T, U, R> {
??? R apply(T t, U u);

???
??? default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after) {
??????? Objects.requireNonNull(after);
??????? return (T t, U u) -> after.apply(apply(t, u));
??? }
}

?

?

ToDoubleBiFunction

public interface ToDoubleBiFunction<T, U> {


??? double applyAsDouble(T t, U u);
}

?


ToIntBiFunction

public interface ToIntBiFunction<T, U> {


??? int applyAsInt(T t, U u);
}

?


ToLongBiFunction

public interface ToLongBiFunction<T, U> {
??? long applyAsLong(T t, U u);
}

?

參數類型擴展

DoubleFunction

public interface DoubleFunction<R> {


??? R apply(double value);
}

?


DoubleToIntFunction

public interface DoubleToIntFunction {


??? int applyAsInt(double value);
}

?


DoubleToLongFunction

public interface DoubleToLongFunction {


??? long applyAsLong(double value);
}

?


IntFunction

public interface IntFunction<R> {


??? R apply(int value);
}


IntToDoubleFunction

public interface IntToDoubleFunction {


??? double applyAsDouble(int value);
}

?


IntToLongFunction

public interface IntToLongFunction {


??? long applyAsLong(int value);
}

?


LongFunction

public interface LongFunction<R> {


??? R apply(long value);
}


LongToDoubleFunction

public interface LongToDoubleFunction {


??? double applyAsDouble(long value);
}

?


LongToIntFunction

public interface LongToIntFunction {


??? int applyAsInt(long value);
}

?


ToDoubleFunction

public interface ToDoubleFunction<T> {


??? double applyAsDouble(T value);
}

?


ToIntFunction

public interface ToIntFunction<T> {


??? int applyAsInt(T value);
}


ToLongFunction

public interface ToLongFunction<T> {


??? long applyAsLong(T value);
}

?

?

特殊變形

UnaryOperator

public interface UnaryOperator<T> extends Function<T, T> {

//返回值是一個Function
??? static <T> UnaryOperator<T> identity() {
??????? return t -> t;
??? }
}

?


IntUnaryOperator

public interface IntUnaryOperator {


??? int applyAsInt(int operand);

//返回值為一個Function
??? default IntUnaryOperator compose(IntUnaryOperator before) {
??????? Objects.requireNonNull(before);
??????? return (int v) -> applyAsInt(before.applyAsInt(v));
??? }

//返回值為一個Function
??? default IntUnaryOperator andThen(IntUnaryOperator after) {
??????? Objects.requireNonNull(after);
??????? return (int t) -> after.applyAsInt(applyAsInt(t));
??? }


??? static IntUnaryOperator identity() {
??????? return t -> t;
??? }
}


LongUnaryOperator

public interface LongUnaryOperator {


??? long applyAsLong(long operand);


??? default LongUnaryOperator compose(LongUnaryOperator before) {
??????? Objects.requireNonNull(before);
??????? return (long v) -> applyAsLong(before.applyAsLong(v));
??? }


??? default LongUnaryOperator andThen(LongUnaryOperator after) {
??????? Objects.requireNonNull(after);
??????? return (long t) -> after.applyAsLong(applyAsLong(t));
??? }


??? static LongUnaryOperator identity() {
??????? return t -> t;
??? }
}


DoubleUnaryOperator

public interface DoubleUnaryOperator {


??? double applyAsDouble(double operand);


??? default DoubleUnaryOperator compose(DoubleUnaryOperator before) {
??????? Objects.requireNonNull(before);
??????? return (double v) -> applyAsDouble(before.applyAsDouble(v));
??? }


??? default DoubleUnaryOperator andThen(DoubleUnaryOperator after) {
??????? Objects.requireNonNull(after);
??????? return (double t) -> after.applyAsDouble(applyAsDouble(t));
??? }


??? static DoubleUnaryOperator identity() {
??????? return t -> t;
??? }
}

?

BinaryOperator

public interface BinaryOperator<T> extends BiFunction<T,T,T> {

??? public static <T> BinaryOperator<T> minBy(Comparator<? super T> comparator) {
??????? Objects.requireNonNull(comparator);
??????? return (a, b) -> comparator.compare(a, b) <= 0 ? a : b;
??? }


??? public static <T> BinaryOperator<T> maxBy(Comparator<? super T> comparator) {
??????? Objects.requireNonNull(comparator);
??????? return (a, b) -> comparator.compare(a, b) >= 0 ? a : b;
??? }
}


DoubleBinaryOperator

public interface DoubleBinaryOperator {

??? double applyAsDouble(double left, double right);
}

?


IntBinaryOperator

public interface IntBinaryOperator {


??? int applyAsInt(int left, int right);
}

?


LongBinaryOperator

public interface LongBinaryOperator {


??? long applyAsLong(long left, long right);
}

?

?

Consumer<T>

@FunctionalInterface
public interface Consumer<T> {

??? /**
???? * Performs this operation on the given argument.
???? *
???? * @param t the input argument
???? */
??? void accept(T t);

??? /**
???? * 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.
???? */
??? default Consumer<T> andThen(Consumer<? super T> after) {
??????? Objects.requireNonNull(after);

?????? //accept(t):輸入類型T,無返回值。

????? //繼續應用after.accept,輸入類型T
??????? return (T t) -> { accept(t); after.accept(t); };
??? }
}

參數個數擴展

參數類型擴展

DoubleConsumer

public interface DoubleConsumer {


??? void accept(double value);


??? default DoubleConsumer andThen(DoubleConsumer after) {
??????? Objects.requireNonNull(after);
??????? return (double t) -> { accept(t); after.accept(t); };
??? }
}


IntConsumer

public interface IntConsumer {


??? void accept(int value);


??? default IntConsumer andThen(IntConsumer after) {
??????? Objects.requireNonNull(after);
??????? return (int t) -> { accept(t); after.accept(t); };
??? }
}

?


LongConsumer

public interface LongConsumer {


??? void accept(long value);


??? default LongConsumer andThen(LongConsumer after) {
??????? Objects.requireNonNull(after);
??????? return (long t) -> { accept(t); after.accept(t); };
??? }
}


ObjDoubleConsumer

public interface ObjDoubleConsumer<T> {


??? void accept(T t, double value);
}

?


ObjIntConsumer

public interface ObjIntConsumer<T> {


??? void accept(T t, int value);
}

?


ObjLongConsumer

public interface ObjLongConsumer<T> {


??? void accept(T t, long value);
}

?

?

特殊變形

Predicate<T>

@FunctionalInterface
public interface Predicate<T> {

??? /**
???? * Evaluates this predicate on the given argument.
???? */
??? boolean test(T t);

??? /**
???? * 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.
???? */
??? default Predicate<T> and(Predicate<? super T> other) {
??????? Objects.requireNonNull(other);
??????? return (t) -> test(t) && other.test(t);
??? }

??? /**
???? * Returns a predicate that represents the logical negation of this
???? * predicate.
???? */
??? default Predicate<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.
?
???? */
??? default Predicate<T> or(Predicate<? super T> 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)}.
???? */
??? static <T> Predicate<T> isEqual(Object targetRef) {
??????? return (null == targetRef)
??????????????? ? Objects::isNull
??????????????? : object -> targetRef.equals(object);
??? }
}

?

參數個數擴展

BiPredicate

public interface BiPredicate<T, U> {


??? boolean test(T t, U u);

??
??? default BiPredicate<T, U> and(BiPredicate<? super T, ? super U> other) {
??????? Objects.requireNonNull(other);
??????? return (T t, U u) -> test(t, u) && other.test(t, u);
??? }

???
??? default BiPredicate<T, U> negate() {
??????? return (T t, U u) -> !test(t, u);
??? }

?
??? default BiPredicate<T, U> or(BiPredicate<? super T, ? super U> other) {
??????? Objects.requireNonNull(other);
??????? return (T t, U u) -> test(t, u) || other.test(t, u);
??? }
}

?

參數類型擴展

DoublePredicate

public interface DoublePredicate {


??? boolean test(double value);

??
??? default DoublePredicate and(DoublePredicate other) {
??????? Objects.requireNonNull(other);
??????? return (value) -> test(value) && other.test(value);
??? }

???
??? default DoublePredicate negate() {
??????? return (value) -> !test(value);
??? }

?
??? default DoublePredicate or(DoublePredicate other) {
??????? Objects.requireNonNull(other);
??????? return (value) -> test(value) || other.test(value);
??? }
}

?

?

IntPredicate

public interface IntPredicate {

??
??? boolean test(int value);

??
??? default IntPredicate and(IntPredicate other) {
??????? Objects.requireNonNull(other);
??????? return (value) -> test(value) && other.test(value);
??? }


??? default IntPredicate negate() {
??????? return (value) -> !test(value);
??? }

?
??? default IntPredicate or(IntPredicate other) {
??????? Objects.requireNonNull(other);
??????? return (value) -> test(value) || other.test(value);
??? }
}

?

?

LongPredicate

public interface LongPredicate {

??
??? boolean test(long value);

???
??? default LongPredicate and(LongPredicate other) {
??????? Objects.requireNonNull(other);
??????? return (value) -> test(value) && other.test(value);
??? }

??
??? default LongPredicate negate() {
??????? return (value) -> !test(value);
??? }

???
??? default LongPredicate or(LongPredicate other) {
??????? Objects.requireNonNull(other);
??????? return (value) -> test(value) || other.test(value);
??? }
}

?

特殊變形

?

?

?

總結

以上是生活随笔為你收集整理的java.util.function包的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。