最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java8中Function函數(shù)式接口用法及說明

 更新時間:2026年04月30日 09:42:25   作者:「已注銷」  
文章詳細(xì)介紹了Java中的函數(shù)式接口,解釋了什么是函數(shù)式接口以及如何使用@FunctionalInterface注解進(jìn)行定義,同時,文章還列舉了常用的內(nèi)置函數(shù)式接口,如Function、Consumer、Predicate、Supplier等,并對它們的方法進(jìn)行了說明

1.函數(shù)式接口

函數(shù)式接口(Functional Interface)就是一個有且僅有一個抽象方法,但是可以有多個非抽象方法的接口。

Functional Interface(功能接口)為lambda表達(dá)式和方法引用(用冒號::來進(jìn)行方法的調(diào)用)提供目標(biāo)類型。每個功能接口都有一個抽象方法,稱為該功能接口的功能方法,lambda表達(dá)式的參數(shù)和返回類型與之匹配或適配。功能接口可以在多個上下文中提供目標(biāo)類型,例如賦值上下文,方法調(diào)用或強(qiáng)制轉(zhuǎn)換上下文:

// Assignment context
Predicate<String> p = String::isEmpty;
// Method invocation context
stream.filter(e -> e.getSize() > 10)...
// Cast context
stream.map((ToIntFunction) e -> e.getSize())...

函數(shù)式接口可以使用lambda表達(dá)式,方法引用或構(gòu)造函數(shù)引用創(chuàng)建功能接口的實例。

Java8為函數(shù)式接口引入了一個新注解@FunctionalInterface,主要用于編譯級錯誤檢查,加上該注解,當(dāng)接口不符合函數(shù)式接口定義的時候,編譯器會報錯。

此注解不是編譯器將接口識別為功能接口的必要條件,而僅是幫助捕獲設(shè)計意圖并獲得編譯器幫助識別意外違反設(shè)計意圖的幫助。

正確例子,沒有報錯:

@FunctionalInterface
public interface HelloWorldService {
    void sayHello(String msg);
}

錯誤例子,接口中包含了兩個抽象方法,違反了函數(shù)式接口的定義,提示在接口中找到多個非重寫的抽象方法。

注意: 加不加 @FunctionalInterface對于接口是不是函數(shù)式接口沒有影響,該注解只是提醒編譯器去檢查該接口是否僅包含一個抽象方法。

1.1允許定義默認(rèn)方法

函數(shù)式接口里是可以包含默認(rèn)方法,因為默認(rèn)方法不是抽象方法,其有一個默認(rèn)實現(xiàn),所以是符合函數(shù)式接口的定義的。

如下代碼不會報錯:

@FunctionalInterface
public interface HelloWorldService {
    void sayHello(String msg);
    default void doSomeWork1() {
        // Method body
    }
    default void doSomeWork2() {
        // Method body
    }
}

1.2允許定義靜態(tài)方法

函數(shù)式接口里是可以包含靜態(tài)方法,因為靜態(tài)方法不能是抽象方法,是一個已經(jīng)實現(xiàn)了的方法,所以是符合函數(shù)式接口的定義的。

如下代碼不會報錯:

@FunctionalInterface
public interface HelloWorldService {
    void sayHello(String msg);
    static void printHello() {
        System.out.println("Hello");
    }
}

1.3允許定義java.lang.Object的public方法

函數(shù)式接口里是可以包含Object里的public方法,這些方法對于函數(shù)式接口來說,不被當(dāng)成是抽象方法(雖然它們是抽象方法);因為任何一個函數(shù)式接口的實現(xiàn),默認(rèn)都繼承了Object類,包含了來自java.lang.Object里對這些抽象方法的實現(xiàn);

如下代碼不會報錯:

@FunctionalInterface
public interface HelloWorldService {
    void sayHello(String msg);
	@Override
    boolean equals(Object obj);
}

1.4已有函數(shù)式接口

函數(shù)式接口可以對現(xiàn)有的函數(shù)友好地支持lambda。

JDK1.8之前已有的函數(shù)式接口:

  • java.lang.Runnable
  • java.util.concurrent.Callable
  • java.security.PrivilegedAction
  • java.util.Comparator
  • java.io.FileFilter
  • java.nio.file.PathMatcher
  • java.lang.reflect.InvocationHandler
  • java.beans.PropertyChangeListener
  • java.awt.event.ActionListener
  • javax.swing.event.ChangeListener

JDK1.8新增加的函數(shù)接口:

  • java.util.function

2.Function函數(shù)

序號接口描述
1Function<T,R>接收一個參數(shù)并返回結(jié)果的函數(shù)
2BiFunction<T,U,R>接受兩個參數(shù)并返回結(jié)果的函數(shù)
3DoubleFunction<R>接收一個double類型的參數(shù)并返回結(jié)果的函數(shù)
4DoubleToIntFunction接收一個double類型的參數(shù)并返回int結(jié)果的函數(shù)
5DoubleToLongFunction接收一個double類型的參數(shù)并返回long結(jié)果的函數(shù)
6IntFunction<R>接收一個int類型的參數(shù)并返回結(jié)果的函數(shù)
7IntToDoubleFunction接收一個int類型的參數(shù)并返回double結(jié)果的函數(shù)
8IntToLongFunction接收一個int類型的參數(shù)并返回long結(jié)果的函數(shù)
9LongFunction<R>接收一個long類型的參數(shù)并返回結(jié)果的函數(shù)
10LongToDoubleFunction接收一個long類型的參數(shù)并返回double結(jié)果的函數(shù)
11LongToIntFunction接收一個long類型的參數(shù)并返回int結(jié)果的函數(shù)
12ToDoubleBiFunction<T,U>接收兩個參數(shù)并返回double結(jié)果的函數(shù)
13ToDoubleFunction<T>接收一個參數(shù)并返回double結(jié)果的函數(shù)
14ToIntBiFunction<T,U>接收兩個參數(shù)并返回int結(jié)果的函數(shù)
15ToIntFunction<T>接收一個參數(shù)并返回int結(jié)果的函數(shù)
16ToLongBiFunction<T,U>接收兩個參數(shù)并返回long結(jié)果的函數(shù)
17ToLongFunction<T>接收一個參數(shù)并返回long結(jié)果的函數(shù)

2.1Function<T, R>

接口方法方法描述
R apply(T t)將此參數(shù)應(yīng)用到函數(shù)中
Function<T, R> andThen(Function<? super R,? extends V> after)返回一個組合函數(shù),該函數(shù)結(jié)果應(yīng)用到after函數(shù)中
Function<T, R> compose(Function<? super V,? extends T> before)返回一個組合函數(shù),首先將入?yún)?yīng)用到before函數(shù),再將before函數(shù)結(jié)果應(yīng)用到該函數(shù)中

①apply(T t)

Function<String, String> function = a -> a +" Jack!";
System.out.println(function.apply("Hello")); // Hello Jack!

②andThen(Function<? super R,? extends V> after)

Function<String, String> function = a -> a +" Jack!";
Function<String, String> function1 = a -> a + " Bob!";
String greet = function.andThen(function1).apply("Hello");
System.out.println(greet); // Hello Jack! Bob!

③compose(Function<? super V,? extends T> before)

Function<String, String> function = a -> a +" Jack!";
Function<String, String> function1 = a -> a + " Bob!";
String greet = function.compose(function1).apply("Hello");
System.out.println(greet); // Hello Bob! Jack!

2.2BiFunction<T, U, R>

接口方法方法描述
R apply(T t, U u)將參數(shù)應(yīng)用于函數(shù)執(zhí)行
BiFunction<T,U,V> andThen(Function<? super R,? extends V> after)返回一個組合函數(shù),after函數(shù)應(yīng)用于該函數(shù)之后

①apply(T t, U u)

BiFunction<String, String, String> biFunction = (a, b) -> a + b;
System.out.println(biFunction.apply("Hello ", "Jack!")); // Hello Jack!

②andThen(Function<? super R,? extends V> after)

BiFunction<String, String, String> biFunction = (a, b) -> a + b;
Function<String, String> function = (a) -> a +"!!!";
System.out.println(biFunction.andThen(function).apply("Hello", " Jack")); // Hello Jack!!!

2.3DoubleFunction

接口方法方法描述
R apply(double value)根據(jù)給定參數(shù)執(zhí)行函數(shù)

①apply(double value)

DoubleFunction<String> doubleFunction = doub ->"結(jié)果:" + doub;
System.out.println(doubleFunction.apply(1.6)); // 結(jié)果:1.6

2.4DoubleToIntFunction

接口方法方法描述
int applyAsInt(double value)根據(jù)給定的參數(shù)執(zhí)行函數(shù)

①applyAsInt(double value)

DoubleToIntFunction doubleToIntFunction = doub -> Double.valueOf(doub).intValue();
System.out.println(doubleToIntFunction.applyAsInt(1.2)); // 1

2.5ToDoubleBiFunction<T,U>

接口方法方法描述
double applyAsDouble(T t, U u)根據(jù)給定的參數(shù)執(zhí)行函數(shù)

①applyAsDouble(T t, U u)

ToDoubleBiFunction<Long, Float> toDoubleBiFunction = (lon, floa) -> lon
	.doubleValue() + floa.doubleValue();
System.out.println(toDoubleBiFunction.applyAsDouble(11L, 235.5f)); // 246.5

2.6ToDoubleFunction

接口方法方法描述
double applyAsDouble(T value)根據(jù)給定參數(shù)執(zhí)行函數(shù)

①applyAsDouble(T value)

ToDoubleFunction<Float> toDoubleFunction = floa -> floa.doubleValue();
System.out.println(toDoubleFunction.applyAsDouble(12315f)); // 12315.0

3.Consumer消費者

序號接口描述
1Consumer<T>提供一個T類型的輸入?yún)?shù),不返回執(zhí)行結(jié)果
2BiConsumer<T,U>提供兩個自定義類型的輸入?yún)?shù),不返回執(zhí)行結(jié)果
3DoubleConsumer表示接受單個double值參數(shù),但不返回結(jié)果的操作
4IntConsumer表示接受單個int值參數(shù),但不返回結(jié)果的操作
5LongConsumer表示接受單個long值參數(shù),但不返回結(jié)果的操作
6ObjDoubleConsumer<T>表示接受object值和double值,但是不返回任何操作結(jié)果
7ObjIntConsumer<T>表示接受object值和int值,但是不返回任何操作結(jié)果
8ObjLongConsumer<T>表示接受object值和long值,但是不返回任何操作結(jié)果

3.1Consumer<T>

接口方法方法描述
void accept(T t)對給定的參數(shù)執(zhí)行操作
default Consumer andThen(Consumer<? super T> after)返回一個組合函數(shù),after將會在該函數(shù)執(zhí)行之后應(yīng)用

①accept(T t)

StringBuilder sb = new StringBuilder("Hello ");
Consumer<StringBuilder> consumer = (str) -> str.append("Jack!");
consumer.accept(sb);
System.out.println(sb.toString());	// Hello Jack!

②andThen(Consumer<? super T> after)

StringBuilder sb = new StringBuilder("Hello ");
Consumer<StringBuilder> consumer = (str) -> str.append("Jack!");
Consumer<StringBuilder> consumer1 = (str) -> str.append(" Bob!");
consumer.andThen(consumer1).accept(sb);
System.out.println(sb.toString());	// Hello Jack! Bob!

3.2BiConsumer<T,U>

接口方法方法描述
void accept(T t, U u)對給定的參數(shù)執(zhí)行操作
default BiConsumer<T,U> andThen(BiConsumer<? super T,? super U> after)返回一個組合函數(shù),after將會在該函數(shù)執(zhí)行之后應(yīng)用

①accept(T t, U u)

StringBuilder sb = new StringBuilder();
BiConsumer<String, String> biConsumer = (a, b) -> {
	sb.append(a);
	sb.append(b);
};
biConsumer.accept("Hello ", "Jack!");
System.out.println(sb.toString());	// Hello Jack!

②andThen(BiConsumer<? super T,? super U> after)

StringBuilder sb = new StringBuilder();
BiConsumer<String, String> biConsumer = (a, b) -> {
	sb.append(a);
	sb.append(b);
};
BiConsumer<String, String> biConsumer1 = (a, b) -> {
	System.out.println(a + b);
};
biConsumer.andThen(biConsumer1).accept("Hello", " Jack!"); // Hello Jack!

3.3DoubleConsumer

接口方法方法描述
void accept(double value)對給定的參數(shù)執(zhí)行操作
default DoubleConsumer andThen(DoubleConsumer after)返回一個組合函數(shù),after在該函數(shù)執(zhí)行之后應(yīng)用

①accept(double value)

DoubleConsumer doubleConsumer = System.out::println;
doubleConsumer.accept(1.3); // 1.3

②andThen(DoubleConsumer after)

DoubleConsumer doubleConsumer = System.out::println;
DoubleConsumer doubleConsumer1 = System.out::println;
doubleConsumer.andThen(doubleConsumer1).accept(1.3);
// 1.3  
// 1.3

3.4ObjDoubleConsumer<T>

接口方法方法描述
void accept(T t, double value)對給定的參數(shù)執(zhí)行操作

①accept(T t, double value)

ObjDoubleConsumer<String> doubleConsumer = (obj, doub)
	-> System.out.println(obj + doub);
doubleConsumer.accept("金額:", 222.66); // 金額:222.66

4.Predicate謂語

序號接口描述
1Predicate<T>對給定的輸入?yún)?shù)執(zhí)行操作,返回一個boolean類型的結(jié)果(布爾值函數(shù))
2BiPredicate<T,U>對給定的兩個輸入?yún)?shù)執(zhí)行操作,返回一個boolean類型的結(jié)果(布爾值函數(shù))
3DoublePredicate對給定的double參數(shù)執(zhí)行操作,返回一個boolean類型的結(jié)果(布爾值函數(shù))
4IntPredicate對給定的int輸入?yún)?shù)執(zhí)行操作,返回一個boolean類型的結(jié)果(布爾值函數(shù))
5LongPredicate對給定的long參數(shù)執(zhí)行操作,返回一個boolean類型的結(jié)果(布爾值函數(shù))

4.1Predicate<T>

接口方法方法描述
boolean test(T t)根據(jù)給定的參數(shù)進(jìn)行判斷
Predicate and(Predicate<? super T> other)返回一個組合判斷,將other以短路與的方式加入到函數(shù)的判斷中
Predicate or(Predicate<? super T> other)返回一個組合判斷,將other以短路或的方式加入到函數(shù)的判斷中
Predicate negate()將函數(shù)的判斷取反

①test(T t)

Predicate<Integer> predicate = number -> number !=0;
System.out.println(predicate.test(10));    //true

②and(Predicate<? super T> other)

Predicate<Integer> predicate = number -> number !=0;
predicate = predicate.and(number -> number >= 10);
System.out.println(predicate.test(10));    //true

③or(Predicate<? super T> other)

Predicate<Integer> predicate = number -> number !=0;
predicate = predicate.or(number -> number != 10);
System.out.println(predicate.test(10));    //true

④negate()

Predicate<Integer> predicate = number -> number !=0;
predicate = predicate.negate();
System.out.println(predicate.test(10));    //false

4.2BiPredicate<T,U>

接口方法方法描述
boolean test(T t, U u)根據(jù)給定的兩個輸入?yún)?shù)進(jìn)行判斷
BiPredicate<T,U> and(BiPredicate<? super T,? super U> other)返回一個組合判斷,將other以短路與的方式加入到函數(shù)的判斷中
BiPredicate<T,U> or(BiPredicate<? super T,? super U> other)返回一個組合判斷,將other以短路或的方式加入到函數(shù)的判斷中
BiPredicate<T,U> negate()將函數(shù)的判斷取反

①test(T t, U u)

BiPredicate<Integer, Integer> biPredicate = (a, b) -> a != b;
System.out.println(biPredicate.test(1, 2)); // true

②and(BiPredicate<? super T,? super U> other)

BiPredicate<Integer, Integer> biPredicate = (a, b) -> a != b;
biPredicate = biPredicate.and((a, b) -> a.equals(b));
System.out.println(biPredicate.test(1, 2)); // false

③or(BiPredicate<? super T,? super U> other)

BiPredicate<Integer, Integer> biPredicate = (a, b) -> a != b;
biPredicate = biPredicate.or((a, b) -> a == b);
System.out.println(biPredicate.test(1, 1)); // true

④negate()

BiPredicate<Integer, Integer> biPredicate = (a, b) -> a != b;
biPredicate = biPredicate.negate();
System.out.println(biPredicate.test(1, 2)); // false

4.3DoublePredicate

接口方法方法描述
boolean test(double value)根據(jù)給定的參數(shù)進(jìn)行判斷
DoublePredicate and(DoublePredicate other)返回一個組合判斷,將other以短路與的方式加入到函數(shù)的判斷中
DoublePredicate or(DoublePredicate other)返回一個組合判斷,將other以短路或的方式加入到函數(shù)的判斷中
default DoublePredicate negate()將函數(shù)的判斷取反

①test(double value)

DoublePredicate doublePredicate = doub -> doub != 0;
System.out.println(doublePredicate.test(10)); // true

②and(DoublePredicate other)

DoublePredicate doublePredicate = doub -> doub != 0;
doublePredicate = doublePredicate.and(doub -> doub < 2);
System.out.println(doublePredicate.test(1.7)); // true

③or(DoublePredicate other)

DoublePredicate doublePredicate = doub -> doub != 0;
doublePredicate = doublePredicate.or(doub -> doub > 2);
System.out.println(doublePredicate.test(1.7)); // true

④negate()

DoublePredicate doublePredicate = doub -> doub != 0;
doublePredicate = doublePredicate.negate();
System.out.println(doublePredicate.test(1.7)); // false

5.Supplier供應(yīng)商

序號接口描述
1Supplier<T>不提供輸入?yún)?shù),但是返回結(jié)果的函數(shù)
2BooleanSupplier不提供輸入?yún)?shù),但是返回boolean結(jié)果的函數(shù)
3DoubleSupplier不提供輸入?yún)?shù),但是返回double結(jié)果的函數(shù)
4IntSupplier不提供輸入?yún)?shù),但是返回int結(jié)果的函數(shù)
5LongSupplier不提供輸入?yún)?shù),但是返回long結(jié)果的函數(shù)

5.1Supplier<T>

接口方法方法描述
T get()獲取結(jié)果值

①get()

Supplier<String> supplier = () ->"Hello Jack!";
System.out.println(supplier.get()); // Hello Jack!

5.2BooleanSupplier

接口方法方法描述
boolean getAsBoolean()獲取函數(shù)的執(zhí)行結(jié)果

①getAsBoolean()

BooleanSupplier booleanSupplier = () -> true;
System.out.println(booleanSupplier.getAsBoolean()); // true

5.3DoubleSupplier

接口方法方法描述
double getAsDouble()獲取函數(shù)的執(zhí)行結(jié)果

①getAsDouble()

DoubleSupplier doubleSupplier = () -> 2.7;
System.out.println(doubleSupplier.getAsDouble()); // 2.7

6.Operator操作員

除了Function,Consumer,Predicate,Supplier這幾個基本的函數(shù)形式,還有其它派生的函數(shù)形式,它們擴(kuò)展了基本的函數(shù)形式,包括UnaryOperator (extends Function)和BinaryOperator (extends BiFunction)。

序號接口描述
1UnaryOperator<T>提供單個類型參數(shù),并且返回一個與輸入?yún)?shù)類型一致的結(jié)果
2BinaryOperator<T>提供兩個相同類型參數(shù),并且返回結(jié)果與輸入?yún)?shù)類型一致的結(jié)果
3DoubleBinaryOperator提供兩個double參數(shù)并且返回double結(jié)果
4DoubleUnaryOperator提供單個double參數(shù)并且返回double結(jié)果
5IntBinaryOperator提供兩個int參數(shù)并且返回int結(jié)果
6IntUnaryOperator提供單個int參數(shù)并且返回int結(jié)果
7LongBinaryOperator提供兩個long參數(shù)并且返回long結(jié)果
8LongUnaryOperator提供單個long參數(shù)并且返回long結(jié)果

6.1UnaryOperator<T>

接口方法方法描述
T apply(T t)將給定參數(shù)應(yīng)用到函數(shù)中
Function<T, R> andThen(Function<? super R,? extends V> after)返回一個組合函數(shù),該函數(shù)結(jié)果應(yīng)用到after函數(shù)中
Function<T, R> compose(Function<? super V,? extends T> before)返回一個組合函數(shù),首先將入?yún)?yīng)用到before函數(shù),再將before函數(shù)結(jié)果應(yīng)用到該函數(shù)中

①apply(T t)

UnaryOperator<String> unaryOperator = greet -> greet +" Bob!";
System.out.println(unaryOperator.apply("Hello")); // Hello Bob!

②andThen(Function<? super T,? extends T> after)

UnaryOperator<String> unaryOperator = greet -> greet +" Bob!";
UnaryOperator<String> unaryOperator1 = greet -> greet + " Jack!";
String greet = unaryOperator.andThen(unaryOperator1).apply("Hello");
System.out.println(greet); // Hello Bob! Jack!

③compose(Function<? super T,? extends T> before)

UnaryOperator<String> unaryOperator = greet -> greet +" Bob!";
UnaryOperator<String> unaryOperator1 = greet -> greet + " Jack!";
String greet = unaryOperator.compose(unaryOperator1).apply("Hello");
System.out.println(greet); // Hello Jack! Bob!

6.2BinaryOperator<T>

接口方法方法描述
T apply(T t, T u)根據(jù)給定參數(shù)執(zhí)行函數(shù)
BiFunction<T,T,T> andThen(Function<? super T,? extends T> after)返回一個組合函數(shù),after應(yīng)用于該函數(shù)之后
BinaryOperator maxBy(Comparator<? super T> comparator)返回二元操作本身,通過特殊比較器返回最大的元素
BinaryOperator minBy(Comparator<? super T> comparator)返回二元操作本身,通過特殊比較器返回最小的元素

①apply(T t, T u)

BinaryOperator<String> binaryOperator = (flag, flag1) -> flag + flag1;
System.out.println(binaryOperator.apply("Hello ", "Jack!")); // Hello Jack!

②andThen(Function<? super T,? extends T> after)

BinaryOperator<String> binaryOperator = (flag, flag1) -> flag + flag1;
Function<String, String> function = a -> a +"!!!";
System.out.println(binaryOperator.andThen(function).apply("Hello", " Jack")); // Hello Jack!!!

③maxBy(Comparator<? super T> comparator)

BinaryOperator<Integer> integerBinaryOperator = BinaryOperator.maxBy(Integer::compareTo);Integer max = integerBinaryOperator.apply(12, 10);
System.out.println(max); // 12

④minBy(Comparator<? super T> comparator)

BinaryOperator<Integer> integerBinaryOperator1 = BinaryOperator.minBy(Integer::compare);Integer min = integerBinaryOperator1.apply(12, 10);
System.out.println(min); // 10

6.3DoubleBinaryOperator

接口方法方法描述
double applyAsDouble(double left, double right)根據(jù)給定的參數(shù)執(zhí)行函數(shù)

①applyAsDouble(double left, double right)

DoubleBinaryOperator doubleBinaryOperator = (doub1, doub2) -> doub1
	+ doub2;
System.out.println(doubleBinaryOperator.applyAsDouble(1.1, 2.3)); // 3.4

6.4DoubleUnaryOperator

接口方法方法描述
double applyAsDouble(double operand)根據(jù)給定參數(shù)執(zhí)行函數(shù)
DoubleUnaryOperator andThen(DoubleUnaryOperator after)返回一個組合函數(shù),after應(yīng)用于該函數(shù)之后
DoubleUnaryOperator compose(DoubleUnaryOperator before)返回一個組合函數(shù),before應(yīng)用于該函數(shù)之前

①applyAsDouble(double operand)

DoubleUnaryOperator doubleUnaryOperator = doub -> doub + 2.5;
System.out.println(doubleUnaryOperator.applyAsDouble(2.6)); // 5.1

②andThen(DoubleUnaryOperator after)

DoubleUnaryOperator doubleUnaryOperator = doub -> doub + 2.5;
DoubleUnaryOperator doubleUnaryOperator1 = doub -> doub * 3;
double result = doubleUnaryOperator.andThen(doubleUnaryOperator1)
	.applyAsDouble(10); 
System.out.println(result); // (10 + 2.5) * 3 = 37.5

③compose(DoubleUnaryOperator before)

DoubleUnaryOperator doubleUnaryOperator = doub -> doub + 2.5;
DoubleUnaryOperator doubleUnaryOperator1 = doub -> doub * 3;
double result = doubleUnaryOperator.compose(doubleUnaryOperator1)
	.applyAsDouble(10);
System.out.println(result); // 10 * 3 + 2.5 = 32.5

1.函數(shù)式接口

函數(shù)式接口(Functional Interface)就是一個有且僅有一個抽象方法,但是可以有多個非抽象方法的接口。

Functional Interface(功能接口)為lambda表達(dá)式和方法引用(用冒號::來進(jìn)行方法的調(diào)用)提供目標(biāo)類型。每個功能接口都有一個抽象方法,稱為該功能接口的功能方法,lambda表達(dá)式的參數(shù)和返回類型與之匹配或適配。功能接口可以在多個上下文中提供目標(biāo)類型,例如賦值上下文,方法調(diào)用或強(qiáng)制轉(zhuǎn)換上下文:

// Assignment context
Predicate<String> p = String::isEmpty;
// Method invocation context
stream.filter(e -> e.getSize() > 10)...
// Cast context
stream.map((ToIntFunction) e -> e.getSize())...

函數(shù)式接口可以使用lambda表達(dá)式,方法引用或構(gòu)造函數(shù)引用創(chuàng)建功能接口的實例。

Java8為函數(shù)式接口引入了一個新注解@FunctionalInterface,主要用于編譯級錯誤檢查,加上該注解,當(dāng)接口不符合函數(shù)式接口定義的時候,編譯器會報錯。

此注解不是編譯器將接口識別為功能接口的必要條件,而僅是幫助捕獲設(shè)計意圖并獲得編譯器幫助識別意外違反設(shè)計意圖的幫助。

正確例子,沒有報錯:

@FunctionalInterface
public interface HelloWorldService {
    void sayHello(String msg);
}

錯誤例子,接口中包含了兩個抽象方法,違反了函數(shù)式接口的定義,提示在接口中找到多個非重寫的抽象方法。

注意: 加不加 @FunctionalInterface對于接口是不是函數(shù)式接口沒有影響,該注解只是提醒編譯器去檢查該接口是否僅包含一個抽象方法。

1.1允許定義默認(rèn)方法

函數(shù)式接口里是可以包含默認(rèn)方法,因為默認(rèn)方法不是抽象方法,其有一個默認(rèn)實現(xiàn),所以是符合函數(shù)式接口的定義的。

如下代碼不會報錯:

@FunctionalInterface
public interface HelloWorldService {
    void sayHello(String msg);
    default void doSomeWork1() {
        // Method body
    }
    default void doSomeWork2() {
        // Method body
    }
}

1.2允許定義靜態(tài)方法

函數(shù)式接口里是可以包含靜態(tài)方法,因為靜態(tài)方法不能是抽象方法,是一個已經(jīng)實現(xiàn)了的方法,所以是符合函數(shù)式接口的定義的。

如下代碼不會報錯:

@FunctionalInterface
public interface HelloWorldService {
    void sayHello(String msg);
    static void printHello() {
        System.out.println("Hello");
    }
}

1.3允許定義java.lang.Object的public方法

函數(shù)式接口里是可以包含Object里的public方法,這些方法對于函數(shù)式接口來說,不被當(dāng)成是抽象方法(雖然它們是抽象方法);因為任何一個函數(shù)式接口的實現(xiàn),默認(rèn)都繼承了Object類,包含了來自java.lang.Object里對這些抽象方法的實現(xiàn);

如下代碼不會報錯:

@FunctionalInterface
public interface HelloWorldService {
    void sayHello(String msg);
	@Override
    boolean equals(Object obj);
}

1.4已有函數(shù)式接口

函數(shù)式接口可以對現(xiàn)有的函數(shù)友好地支持lambda。

JDK1.8之前已有的函數(shù)式接口:

  • java.lang.Runnable
  • java.util.concurrent.Callable
  • java.security.PrivilegedAction
  • java.util.Comparator
  • java.io.FileFilter
  • java.nio.file.PathMatcher
  • java.lang.reflect.InvocationHandler
  • java.beans.PropertyChangeListener
  • java.awt.event.ActionListener
  • javax.swing.event.ChangeListener

JDK1.8新增加的函數(shù)接口:

  • java.util.function

2.Function函數(shù)

序號接口描述
1Function<T,R>接收一個參數(shù)并返回結(jié)果的函數(shù)
2BiFunction<T,U,R>接受兩個參數(shù)并返回結(jié)果的函數(shù)
3DoubleFunction<R>接收一個double類型的參數(shù)并返回結(jié)果的函數(shù)
4DoubleToIntFunction接收一個double類型的參數(shù)并返回int結(jié)果的函數(shù)
5DoubleToLongFunction接收一個double類型的參數(shù)并返回long結(jié)果的函數(shù)
6IntFunction<R>接收一個int類型的參數(shù)并返回結(jié)果的函數(shù)
7IntToDoubleFunction接收一個int類型的參數(shù)并返回double結(jié)果的函數(shù)
8IntToLongFunction接收一個int類型的參數(shù)并返回long結(jié)果的函數(shù)
9LongFunction<R>接收一個long類型的參數(shù)并返回結(jié)果的函數(shù)
10LongToDoubleFunction接收一個long類型的參數(shù)并返回double結(jié)果的函數(shù)
11LongToIntFunction接收一個long類型的參數(shù)并返回int結(jié)果的函數(shù)
12ToDoubleBiFunction<T,U>接收兩個參數(shù)并返回double結(jié)果的函數(shù)
13ToDoubleFunction<T>接收一個參數(shù)并返回double結(jié)果的函數(shù)
14ToIntBiFunction<T,U>接收兩個參數(shù)并返回int結(jié)果的函數(shù)
15ToIntFunction<T>接收一個參數(shù)并返回int結(jié)果的函數(shù)
16ToLongBiFunction<T,U>接收兩個參數(shù)并返回long結(jié)果的函數(shù)
17ToLongFunction<T>接收一個參數(shù)并返回long結(jié)果的函數(shù)

2.1Function<T, R>

接口方法方法描述
R apply(T t)將此參數(shù)應(yīng)用到函數(shù)中
Function<T, R> andThen(Function<? super R,? extends V> after)返回一個組合函數(shù),該函數(shù)結(jié)果應(yīng)用到after函數(shù)中
Function<T, R> compose(Function<? super V,? extends T> before)返回一個組合函數(shù),首先將入?yún)?yīng)用到before函數(shù),再將before函數(shù)結(jié)果應(yīng)用到該函數(shù)中

①apply(T t)

Function<String, String> function = a -> a +" Jack!";
System.out.println(function.apply("Hello")); // Hello Jack!

②andThen(Function<? super R,? extends V> after)

Function<String, String> function = a -> a +" Jack!";
Function<String, String> function1 = a -> a + " Bob!";
String greet = function.andThen(function1).apply("Hello");
System.out.println(greet); // Hello Jack! Bob!

③compose(Function<? super V,? extends T> before)

Function<String, String> function = a -> a +" Jack!";
Function<String, String> function1 = a -> a + " Bob!";
String greet = function.compose(function1).apply("Hello");
System.out.println(greet); // Hello Bob! Jack!

2.2BiFunction<T, U, R>

接口方法方法描述
R apply(T t, U u)將參數(shù)應(yīng)用于函數(shù)執(zhí)行
BiFunction<T,U,V> andThen(Function<? super R,? extends V> after)返回一個組合函數(shù),after函數(shù)應(yīng)用于該函數(shù)之后

①apply(T t, U u)

BiFunction<String, String, String> biFunction = (a, b) -> a + b;
System.out.println(biFunction.apply("Hello ", "Jack!")); // Hello Jack!

②andThen(Function<? super R,? extends V> after)

BiFunction<String, String, String> biFunction = (a, b) -> a + b;
Function<String, String> function = (a) -> a +"!!!";
System.out.println(biFunction.andThen(function).apply("Hello", " Jack")); // Hello Jack!!!

2.3DoubleFunction

接口方法方法描述
R apply(double value)根據(jù)給定參數(shù)執(zhí)行函數(shù)

①apply(double value)

DoubleFunction<String> doubleFunction = doub ->"結(jié)果:" + doub;
System.out.println(doubleFunction.apply(1.6)); // 結(jié)果:1.6

2.4DoubleToIntFunction

接口方法方法描述
int applyAsInt(double value)根據(jù)給定的參數(shù)執(zhí)行函數(shù)

①applyAsInt(double value)

DoubleToIntFunction doubleToIntFunction = doub -> Double.valueOf(doub).intValue();
System.out.println(doubleToIntFunction.applyAsInt(1.2)); // 1

2.5ToDoubleBiFunction<T,U>

接口方法方法描述
double applyAsDouble(T t, U u)根據(jù)給定的參數(shù)執(zhí)行函數(shù)

①applyAsDouble(T t, U u)

ToDoubleBiFunction<Long, Float> toDoubleBiFunction = (lon, floa) -> lon
	.doubleValue() + floa.doubleValue();
System.out.println(toDoubleBiFunction.applyAsDouble(11L, 235.5f)); // 246.5

2.6ToDoubleFunction

接口方法方法描述
double applyAsDouble(T value)根據(jù)給定參數(shù)執(zhí)行函數(shù)

①applyAsDouble(T value)

ToDoubleFunction<Float> toDoubleFunction = floa -> floa.doubleValue();
System.out.println(toDoubleFunction.applyAsDouble(12315f)); // 12315.0

3.Consumer消費者

序號接口描述
1Consumer<T>提供一個T類型的輸入?yún)?shù),不返回執(zhí)行結(jié)果
2BiConsumer<T,U>提供兩個自定義類型的輸入?yún)?shù),不返回執(zhí)行結(jié)果
3DoubleConsumer表示接受單個double值參數(shù),但不返回結(jié)果的操作
4IntConsumer表示接受單個int值參數(shù),但不返回結(jié)果的操作
5LongConsumer表示接受單個long值參數(shù),但不返回結(jié)果的操作
6ObjDoubleConsumer<T>表示接受object值和double值,但是不返回任何操作結(jié)果
7ObjIntConsumer<T>表示接受object值和int值,但是不返回任何操作結(jié)果
8ObjLongConsumer<T>表示接受object值和long值,但是不返回任何操作結(jié)果

3.1Consumer<T>

接口方法方法描述
void accept(T t)對給定的參數(shù)執(zhí)行操作
default Consumer andThen(Consumer<? super T> after)返回一個組合函數(shù),after將會在該函數(shù)執(zhí)行之后應(yīng)用

①accept(T t)

StringBuilder sb = new StringBuilder("Hello ");
Consumer<StringBuilder> consumer = (str) -> str.append("Jack!");
consumer.accept(sb);
System.out.println(sb.toString());	// Hello Jack!

②andThen(Consumer<? super T> after)

StringBuilder sb = new StringBuilder("Hello ");
Consumer<StringBuilder> consumer = (str) -> str.append("Jack!");
Consumer<StringBuilder> consumer1 = (str) -> str.append(" Bob!");
consumer.andThen(consumer1).accept(sb);
System.out.println(sb.toString());	// Hello Jack! Bob!

3.2BiConsumer<T,U>

接口方法方法描述
void accept(T t, U u)對給定的參數(shù)執(zhí)行操作
default BiConsumer<T,U> andThen(BiConsumer<? super T,? super U> after)返回一個組合函數(shù),after將會在該函數(shù)執(zhí)行之后應(yīng)用

①accept(T t, U u)

StringBuilder sb = new StringBuilder();
BiConsumer<String, String> biConsumer = (a, b) -> {
	sb.append(a);
	sb.append(b);
};
biConsumer.accept("Hello ", "Jack!");
System.out.println(sb.toString());	// Hello Jack!

②andThen(BiConsumer<? super T,? super U> after)

StringBuilder sb = new StringBuilder();
BiConsumer<String, String> biConsumer = (a, b) -> {
	sb.append(a);
	sb.append(b);
};
BiConsumer<String, String> biConsumer1 = (a, b) -> {
	System.out.println(a + b);
};
biConsumer.andThen(biConsumer1).accept("Hello", " Jack!"); // Hello Jack!

3.3DoubleConsumer

接口方法方法描述
void accept(double value)對給定的參數(shù)執(zhí)行操作
default DoubleConsumer andThen(DoubleConsumer after)返回一個組合函數(shù),after在該函數(shù)執(zhí)行之后應(yīng)用

①accept(double value)

DoubleConsumer doubleConsumer = System.out::println;
doubleConsumer.accept(1.3); // 1.3

②andThen(DoubleConsumer after)

DoubleConsumer doubleConsumer = System.out::println;
DoubleConsumer doubleConsumer1 = System.out::println;
doubleConsumer.andThen(doubleConsumer1).accept(1.3);
// 1.3  
// 1.3

3.4ObjDoubleConsumer<T>

接口方法方法描述
void accept(T t, double value)對給定的參數(shù)執(zhí)行操作

①accept(T t, double value)

ObjDoubleConsumer<String> doubleConsumer = (obj, doub)
	-> System.out.println(obj + doub);
doubleConsumer.accept("金額:", 222.66); // 金額:222.66

4.Predicate謂語

序號接口描述
1Predicate<T>對給定的輸入?yún)?shù)執(zhí)行操作,返回一個boolean類型的結(jié)果(布爾值函數(shù))
2BiPredicate<T,U>對給定的兩個輸入?yún)?shù)執(zhí)行操作,返回一個boolean類型的結(jié)果(布爾值函數(shù))
3DoublePredicate對給定的double參數(shù)執(zhí)行操作,返回一個boolean類型的結(jié)果(布爾值函數(shù))
4IntPredicate對給定的int輸入?yún)?shù)執(zhí)行操作,返回一個boolean類型的結(jié)果(布爾值函數(shù))
5LongPredicate對給定的long參數(shù)執(zhí)行操作,返回一個boolean類型的結(jié)果(布爾值函數(shù))

4.1Predicate<T>

接口方法方法描述
boolean test(T t)根據(jù)給定的參數(shù)進(jìn)行判斷
Predicate and(Predicate<? super T> other)返回一個組合判斷,將other以短路與的方式加入到函數(shù)的判斷中
Predicate or(Predicate<? super T> other)返回一個組合判斷,將other以短路或的方式加入到函數(shù)的判斷中
Predicate negate()將函數(shù)的判斷取反

①test(T t)

Predicate<Integer> predicate = number -> number !=0;
System.out.println(predicate.test(10));    //true

②and(Predicate<? super T> other)

Predicate<Integer> predicate = number -> number !=0;
predicate = predicate.and(number -> number >= 10);
System.out.println(predicate.test(10));    //true

③or(Predicate<? super T> other)

Predicate<Integer> predicate = number -> number !=0;
predicate = predicate.or(number -> number != 10);
System.out.println(predicate.test(10));    //true

④negate()

Predicate<Integer> predicate = number -> number !=0;
predicate = predicate.negate();
System.out.println(predicate.test(10));    //false

4.2BiPredicate<T,U>

接口方法方法描述
boolean test(T t, U u)根據(jù)給定的兩個輸入?yún)?shù)進(jìn)行判斷
BiPredicate<T,U> and(BiPredicate<? super T,? super U> other)返回一個組合判斷,將other以短路與的方式加入到函數(shù)的判斷中
BiPredicate<T,U> or(BiPredicate<? super T,? super U> other)返回一個組合判斷,將other以短路或的方式加入到函數(shù)的判斷中
BiPredicate<T,U> negate()將函數(shù)的判斷取反

①test(T t, U u)

BiPredicate<Integer, Integer> biPredicate = (a, b) -> a != b;
System.out.println(biPredicate.test(1, 2)); // true

②and(BiPredicate<? super T,? super U> other)

BiPredicate<Integer, Integer> biPredicate = (a, b) -> a != b;
biPredicate = biPredicate.and((a, b) -> a.equals(b));
System.out.println(biPredicate.test(1, 2)); // false

③or(BiPredicate<? super T,? super U> other)

BiPredicate<Integer, Integer> biPredicate = (a, b) -> a != b;
biPredicate = biPredicate.or((a, b) -> a == b);
System.out.println(biPredicate.test(1, 1)); // true

④negate()

BiPredicate<Integer, Integer> biPredicate = (a, b) -> a != b;
biPredicate = biPredicate.negate();
System.out.println(biPredicate.test(1, 2)); // false

4.3DoublePredicate

接口方法方法描述
boolean test(double value)根據(jù)給定的參數(shù)進(jìn)行判斷
DoublePredicate and(DoublePredicate other)返回一個組合判斷,將other以短路與的方式加入到函數(shù)的判斷中
DoublePredicate or(DoublePredicate other)返回一個組合判斷,將other以短路或的方式加入到函數(shù)的判斷中
default DoublePredicate negate()將函數(shù)的判斷取反

①test(double value)

DoublePredicate doublePredicate = doub -> doub != 0;
System.out.println(doublePredicate.test(10)); // true

②and(DoublePredicate other)

DoublePredicate doublePredicate = doub -> doub != 0;
doublePredicate = doublePredicate.and(doub -> doub < 2);
System.out.println(doublePredicate.test(1.7)); // true

③or(DoublePredicate other)

DoublePredicate doublePredicate = doub -> doub != 0;
doublePredicate = doublePredicate.or(doub -> doub > 2);
System.out.println(doublePredicate.test(1.7)); // true

④negate()

DoublePredicate doublePredicate = doub -> doub != 0;
doublePredicate = doublePredicate.negate();
System.out.println(doublePredicate.test(1.7)); // false

5.Supplier供應(yīng)商

序號接口描述
1Supplier<T>不提供輸入?yún)?shù),但是返回結(jié)果的函數(shù)
2BooleanSupplier不提供輸入?yún)?shù),但是返回boolean結(jié)果的函數(shù)
3DoubleSupplier不提供輸入?yún)?shù),但是返回double結(jié)果的函數(shù)
4IntSupplier不提供輸入?yún)?shù),但是返回int結(jié)果的函數(shù)
5LongSupplier不提供輸入?yún)?shù),但是返回long結(jié)果的函數(shù)

5.1Supplier<T>

接口方法方法描述
T get()獲取結(jié)果值

①get()

Supplier<String> supplier = () ->"Hello Jack!";
System.out.println(supplier.get()); // Hello Jack!

5.2BooleanSupplier

接口方法方法描述
boolean getAsBoolean()獲取函數(shù)的執(zhí)行結(jié)果

①getAsBoolean()

BooleanSupplier booleanSupplier = () -> true;
System.out.println(booleanSupplier.getAsBoolean()); // true

5.3DoubleSupplier

接口方法方法描述
double getAsDouble()獲取函數(shù)的執(zhí)行結(jié)果

①getAsDouble()

DoubleSupplier doubleSupplier = () -> 2.7;
System.out.println(doubleSupplier.getAsDouble()); // 2.7

6.Operator操作員

除了Function,Consumer,Predicate,Supplier這幾個基本的函數(shù)形式,還有其它派生的函數(shù)形式,它們擴(kuò)展了基本的函數(shù)形式,包括UnaryOperator (extends Function)和BinaryOperator (extends BiFunction)。

序號接口描述
1UnaryOperator<T>提供單個類型參數(shù),并且返回一個與輸入?yún)?shù)類型一致的結(jié)果
2BinaryOperator<T>提供兩個相同類型參數(shù),并且返回結(jié)果與輸入?yún)?shù)類型一致的結(jié)果
3DoubleBinaryOperator提供兩個double參數(shù)并且返回double結(jié)果
4DoubleUnaryOperator提供單個double參數(shù)并且返回double結(jié)果
5IntBinaryOperator提供兩個int參數(shù)并且返回int結(jié)果
6IntUnaryOperator提供單個int參數(shù)并且返回int結(jié)果
7LongBinaryOperator提供兩個long參數(shù)并且返回long結(jié)果
8LongUnaryOperator提供單個long參數(shù)并且返回long結(jié)果

6.1UnaryOperator<T>

接口方法方法描述
T apply(T t)將給定參數(shù)應(yīng)用到函數(shù)中
Function<T, R> andThen(Function<? super R,? extends V> after)返回一個組合函數(shù),該函數(shù)結(jié)果應(yīng)用到after函數(shù)中
Function<T, R> compose(Function<? super V,? extends T> before)返回一個組合函數(shù),首先將入?yún)?yīng)用到before函數(shù),再將before函數(shù)結(jié)果應(yīng)用到該函數(shù)中

①apply(T t)

UnaryOperator<String> unaryOperator = greet -> greet +" Bob!";
System.out.println(unaryOperator.apply("Hello")); // Hello Bob!

②andThen(Function<? super T,? extends T> after)

UnaryOperator<String> unaryOperator = greet -> greet +" Bob!";
UnaryOperator<String> unaryOperator1 = greet -> greet + " Jack!";
String greet = unaryOperator.andThen(unaryOperator1).apply("Hello");
System.out.println(greet); // Hello Bob! Jack!

③compose(Function<? super T,? extends T> before)

UnaryOperator<String> unaryOperator = greet -> greet +" Bob!";
UnaryOperator<String> unaryOperator1 = greet -> greet + " Jack!";
String greet = unaryOperator.compose(unaryOperator1).apply("Hello");
System.out.println(greet); // Hello Jack! Bob!

6.2BinaryOperator<T>

接口方法方法描述
T apply(T t, T u)根據(jù)給定參數(shù)執(zhí)行函數(shù)
BiFunction<T,T,T> andThen(Function<? super T,? extends T> after)返回一個組合函數(shù),after應(yīng)用于該函數(shù)之后
BinaryOperator maxBy(Comparator<? super T> comparator)返回二元操作本身,通過特殊比較器返回最大的元素
BinaryOperator minBy(Comparator<? super T> comparator)返回二元操作本身,通過特殊比較器返回最小的元素

①apply(T t, T u)

BinaryOperator<String> binaryOperator = (flag, flag1) -> flag + flag1;
System.out.println(binaryOperator.apply("Hello ", "Jack!")); // Hello Jack!

②andThen(Function<? super T,? extends T> after)

BinaryOperator<String> binaryOperator = (flag, flag1) -> flag + flag1;
Function<String, String> function = a -> a +"!!!";
System.out.println(binaryOperator.andThen(function).apply("Hello", " Jack")); // Hello Jack!!!

③maxBy(Comparator<? super T> comparator)

BinaryOperator<Integer> integerBinaryOperator = BinaryOperator.maxBy(Integer::compareTo);Integer max = integerBinaryOperator.apply(12, 10);
System.out.println(max); // 12

④minBy(Comparator<? super T> comparator)

BinaryOperator<Integer> integerBinaryOperator1 = BinaryOperator.minBy(Integer::compare);Integer min = integerBinaryOperator1.apply(12, 10);
System.out.println(min); // 10

6.3DoubleBinaryOperator

接口方法方法描述
double applyAsDouble(double left, double right)根據(jù)給定的參數(shù)執(zhí)行函數(shù)

①applyAsDouble(double left, double right)

DoubleBinaryOperator doubleBinaryOperator = (doub1, doub2) -> doub1
	+ doub2;
System.out.println(doubleBinaryOperator.applyAsDouble(1.1, 2.3)); // 3.4

6.4DoubleUnaryOperator

接口方法方法描述
double applyAsDouble(double operand)根據(jù)給定參數(shù)執(zhí)行函數(shù)
DoubleUnaryOperator andThen(DoubleUnaryOperator after)返回一個組合函數(shù),after應(yīng)用于該函數(shù)之后
DoubleUnaryOperator compose(DoubleUnaryOperator before)返回一個組合函數(shù),before應(yīng)用于該函數(shù)之前

①applyAsDouble(double operand)

DoubleUnaryOperator doubleUnaryOperator = doub -> doub + 2.5;
System.out.println(doubleUnaryOperator.applyAsDouble(2.6)); // 5.1

②andThen(DoubleUnaryOperator after)

DoubleUnaryOperator doubleUnaryOperator = doub -> doub + 2.5;
DoubleUnaryOperator doubleUnaryOperator1 = doub -> doub * 3;
double result = doubleUnaryOperator.andThen(doubleUnaryOperator1)
	.applyAsDouble(10); 
System.out.println(result); // (10 + 2.5) * 3 = 37.5

③compose(DoubleUnaryOperator before)

DoubleUnaryOperator doubleUnaryOperator = doub -> doub + 2.5;
DoubleUnaryOperator doubleUnaryOperator1 = doub -> doub * 3;
double result = doubleUnaryOperator.compose(doubleUnaryOperator1)
	.applyAsDouble(10);
System.out.println(result); // 10 * 3 + 2.5 = 32.5

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java中ThreadPoolExecutor拒絕策略踩坑

    Java中ThreadPoolExecutor拒絕策略踩坑

    本文主要介紹了Java中ThreadPoolExecutor拒絕策略踩坑,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • 全面解析java中的hashtable

    全面解析java中的hashtable

    以下是對java中的hashtable進(jìn)行了詳細(xì)的分析介紹。需要的朋友可以過來參考下
    2013-08-08
  • 詳解Java多線程與并發(fā)

    詳解Java多線程與并發(fā)

    多線程是一個進(jìn)程在執(zhí)行過程中產(chǎn)生多個更小的程序單元,這些更小的單元稱為線程,這些線程可以同時存在,同時運行,一個進(jìn)程可能包含多個同時執(zhí)行的線程。多線程是實現(xiàn)并發(fā)機(jī)制的一種有效手段。進(jìn)程和線程一樣,都是實現(xiàn)并發(fā)的一個基本單位。
    2021-06-06
  • 解決swaggerUI頁面沒有顯示Controller方法的坑

    解決swaggerUI頁面沒有顯示Controller方法的坑

    這篇文章主要介紹了解決swaggerUI頁面沒有顯示Controller方法的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Spring AOP的入門教程

    Spring AOP的入門教程

    Spring AOP是Spring框架的一個模塊,本文主要介紹了Spring AOP的入門教程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-11-11
  • Java操作MinIO實現(xiàn)文件的上傳和刪除

    Java操作MinIO實現(xiàn)文件的上傳和刪除

    本文主要介紹如何將本地Java項目resources目錄下的一個PNG圖片上傳到MinIO,然后將上傳的圖片刪除,文中有詳細(xì)的流程步驟和示例代碼,需要的朋友可以參考下
    2023-06-06
  • Java8中Stream的使用方式

    Java8中Stream的使用方式

    這篇文章主要介紹了Java8中Stream的使用方式,文章通過Stream的創(chuàng)建展開詳細(xì)的介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-04-04
  • Java編寫程序之輸入一個數(shù)字實現(xiàn)該數(shù)字階乘的計算

    Java編寫程序之輸入一個數(shù)字實現(xiàn)該數(shù)字階乘的計算

    這篇文章主要介紹了Java編寫程序之輸入一個數(shù)字實現(xiàn)該數(shù)字階乘的計算,本文通過實例代碼給大家介紹的非常想詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • 詳解使用Spring Security OAuth 實現(xiàn)OAuth 2.0 授權(quán)

    詳解使用Spring Security OAuth 實現(xiàn)OAuth 2.0 授權(quán)

    本篇文章主要介紹了詳解使用Spring Security OAuth 實現(xiàn)OAuth 2.0 授權(quán),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • 使用Java實現(xiàn)將PowerPoint轉(zhuǎn)換為PDF的完整指南

    使用Java實現(xiàn)將PowerPoint轉(zhuǎn)換為PDF的完整指南

    將 PowerPoint 轉(zhuǎn)換為 PDF 就是一個規(guī)避兼容問題的不二選擇,通過 Java 完成這一自動化流程,則能進(jìn)一步應(yīng)用到辦公自動化和在線文檔系統(tǒng)中,下面我們就來看看具體實現(xiàn)步驟吧
    2025-12-12

最新評論

玉树县| 洪江市| 铅山县| 武定县| 玛纳斯县| 宜宾县| 吉林省| 辽阳市| 探索| 三河市| 呼图壁县| 三江| 曲周县| 中阳县| 黄龙县| 志丹县| 筠连县| 张家界市| 偃师市| 大余县| 来宾市| 长沙县| 广安市| 上犹县| 云梦县| 黄梅县| 常熟市| 宁陵县| 青岛市| 东丽区| 满城县| 凉城县| 通辽市| 正定县| 城口县| 通河县| 海门市| 华亭县| 松潘县| 阜宁县| 正定县|