gpt4 book ai didi

java - java.util.function包中函数式接口(interface)的参数和返回类型汇总

转载 作者:搜寻专家 更新时间:2023-11-01 01:21:14 24 4
gpt4 key购买 nike

我正在寻找 java.util.function 中所有接口(interface)的单个​​抽象方法 (SAM) 的参数和返回类型表。

最佳答案

这是包中所有 43 个接口(interface)的表格,以及一些其他值得注意的接口(interface)。这种安排应该可以很容易地看到包中的命名模式。该表旨在用作 .java 类文件中的注释。在 eclipse(或任何其他可以在注释中解析类名的 IDE)中打开文件。您应该能够将鼠标悬停在名称上并查看它们的 javadoc。 ctrl-click 将打开接口(interface)源代码,如果你已经正确 attached the java source code .

(令人惊讶的是,这在 InteliJ 中似乎不起作用。如果我缺少某个设置,请告诉我。)

import java.util.function.Function;  //Prevent "which package?" popups
import java.util.function.Predicate;

抽象方法声明“抛出异常”的接口(interface)用 * 表示

/*  Param\Return   void                  boolean                R                  
---- ------- -
void Runnable BooleanSupplier Supplier<R>
void AutoCloseable* Callable<R>*

T Consumer<T> Predicate<T> Function<T,R>
R UnaryOperator<R>

T, U BiConsumer<T,U> BiPredicate<T,U> BiFunction<T,U,R>
R, R BinaryOperator<R>

int IntConsumer IntPredicate IntFunction<R>
T, int ObjIntConsumer<T>

long LongConsumer LongPredicate LongFunction<R>
T, long ObjLongConsumer<T>

double DoubleConsumer DoublePredicate DoubleFunction<R>
T, double ObjDoubleConsumer<T>

Param\Return int long double
--- ---- ------
void IntSupplier LongSupplier DoubleSupplier

T ToIntFunction<T> ToLongFunction<T> ToDoubleFunction<T>

T,U ToIntBiFunction<T,U> ToLongBiFunction<T,U> ToDoubleBiFunction<T,U>

int IntUnaryOperator IntToLongFunction IntToDoubleFunction
int, int IntBinaryOperator

long LongToIntFunction LongUnaryOperator LongToDoubleFunction
long, long LongBinaryOperator

double DoubleToIntFunction DoubleToLongFunction DoubleUnaryOperator
double, double DoubleBinaryOperator */

一些使用示例:

// Lambda using Runnable
new Thread(() -> System.out.println(Thread.currentThread().getName())).start();

Optional<String> opt = Optional.of("Meh");

// Lambda using Predicate<? super String>;
opt = opt.filter( s->s.equalsIgnoreCase("meh") );
System.out.println(opt+" <-- opt");

// Lambda using Consumer<? super String>;
opt.ifPresent( s->System.out.println(s) );

// Lambda using Function<? super String, ? extends String>;
opt = opt.map(s->s+"!").map(s->s+"!");
System.out.println(opt+" <-- opt");

// Lambda using Supplier<? extends IllegalArgumentException>;
opt.orElseThrow( ()->new IllegalArgumentException("Should not be empty.") );
opt = Optional.empty();
opt.orElseThrow(
()->new IllegalArgumentException("Empty? Who said you could be empty?")
);

Thread-0
Optional[Meh] <-- opt
Meh
Optional[Meh!!] <-- opt
Exception in thread "main" java.lang.IllegalArgumentException:
Empty? Who said you could be empty?
at functionalinterfacestudy.AllLambdas.lambda$6(AllLambdas.java:110)
at functionalinterfacestudy.AllLambdas$$Lambda$7/1392838282.get(Unknown Source)
at java.util.Optional.orElseThrow(Unknown Source)
at functionalinterfacestudy.AllLambdas.main(AllLambdas.java:110)

此外,本书还提供了一些包装 detailed tables .

而且,虽然它不是一张表格,但阅读 official package summery 总是好的.

JDK 实际上有 57 个带有 @FunctionalInterface annotation 的接口(interface).上面没有提到的包括:

import java.io.FileFilter;       // Aren't name collisions fun?
import java.io.FilenameFilter;
import java.util.Comparator;
import java.util.logging.Filter;

/*

Interface Single Abstract Method
--------- ----------------------
KeyEventDispatcher: boolean dispatchKeyEvent(KeyEvent e);
KeyEventPostProcessor: boolean postProcessKeyEvent(KeyEvent e);
FileFilter: boolean accept(File pathname);
FilenameFilter: boolean accept(File dir, String name);
Thread.UncaughtExceptionHandler: void uncaughtException(Thread t, Throwable e);
DirectoryStream<T>.Filter<T>: boolean accept(T entry) throws IOException;
PathMatcher: boolean matches(Path path);
TemporalAdjuster: Temporal adjustInto(Temporal temporal);
TemporalQuery<R>: R queryFrom(TemporalAccessor temporal);
Comparator<T>: int compare(T o1, T o2);
Filter: public boolean isLoggable(LogRecord record);
PreferenceChangeListener: void preferenceChange(PreferenceChangeEvent evt);

*/

然而,JDK 有许多接口(interface)满足所有要求成为函数式接口(interface),但没有@FunctionalInterface 注解(例如AutoClosable)。缺少的注释不会阻止它们作为功能接口(interface)工作。它用于在接口(interface)违反功能接口(interface)的定义时强制编译器抛出错误。在某种程度上,它 promise 在实现接口(interface)时不扩展必须重写的抽象方法集(可以添加默认方法,因为它们总是有自己的实现)。这让我想知道:why it isn't @FunctionalInterface used on all the interfaces in the JDK that qualify?

关于java - java.util.function包中函数式接口(interface)的参数和返回类型汇总,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27743315/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com