/**
* Accepts a function that extracts an {@code int} sort key from a type
* {@code T}, and returns a {@code Comparator<T>} that compares by that
* sort key.
*
* <p>The returned comparator is serializable if the specified function
* is also serializable.
*
* @param <T> the type of element to be compared
* @param keyExtractor the function used to extract the integer sort key
* @return a comparator that compares by an extracted key
* @see #comparing(Function)
* @throws NullPointerException if the argument is null
* @since 1.8
*/publicstatic<T> Comparator<T>comparingInt(ToIntFunction<?super T> keyExtractor){
Objects.requireNonNull(keyExtractor);return(Comparator<T>& Serializable)(c1, c2)-> Integer.compare(keyExtractor.applyAsInt(c1), keyExtractor.applyAsInt(c2));}
程序输出如下:
jdk中常用的函数式接口
Supplier接口
生产型接口:指定接口的泛型是什么类型,那么接口中的get方法就会产生什么类型的数据。
@FunctionalInterfacepublicinterfaceSupplier<T>{/**
* Gets a result.
*
* @return a result
*/
T get();}
@FunctionalInterfacepublicinterfaceConsumer<T>{/**
* Performs this operation on the given argument.
*
* @param t the input argument
*/voidaccept(T t);}