五级偏黄视频网站建设,asp.net 网站开发实例,WordPress切换中英文,专门做网站推广的平台方法参考 众所周知#xff0c;我们可以使用Java 8中的方法引用 #xff08;例如String::isEmpty来引用例如在元素上流式传输时使用的方法。 看一下以下代码片段#xff1a; Stream.of(A, , B).filter(Stream::isEmpty).count();它将产… 方法参考 众所周知我们可以使用Java 8中的方法引用 例如String::isEmpty来引用例如在元素上流式传输时使用的方法。 看一下以下代码片段 Stream.of(A, , B).filter(Stream::isEmpty).count(); 它将产生结果1因为流中只有一个空元素。 但是如果要过滤掉非空字符串则需要编写.filter(s - !s.isEmpty()) 这是一个Lambda。 显然这里有一个令人讨厌的不对称。 我们可以使用方法参考但不能使用它的否定。 我们可以编写predicate.negate()但不能编写Stream::isEmpty.negate()或!Stream::isEmpty 。 这是为什么 这是因为方法引用不是Lambda或功能接口。 但是可以使用Java的类型推断将方法引用解析为一个或多个功能接口。 实际上我们的示例String::isEmpty至少可以解析为 PredicateString FunctionString, Boolean 因此我们需要以某种方式解决所有潜在的歧义并确定我们要将方法参考转换为哪个功能接口。 阅读这篇文章了解如何部分解决此问题。 我使用了开源项目Speedment中提供的代码该代码使数据库看起来像Java 8 Streams。 随意尝试Speedment out。 Speedment还包含谓词生成器使您可以直接使用诸如Entity.NAME::isEmpty和Entity.NAME::isNotEmpty之类的函数。 解析方法参考 通过以静态方法的形式引入一些“管道”可以部分解决该问题该方法采用“方法参考”并将其作为特定功能接口的视图返回。 考虑以下简短的静态方法 public static T PredicateT as(PredicateT predicate) {return predicate;} 现在如果我们静态导入该方法实际上我们可以更轻松地使用“方法引用”如以下简短示例所示 Stream.of(A, , B).filter(as(String::isEmpty).negate()).count(); 该代码将返回2这是流中非空元素的数量。 在方法参考用法方面这是向前迈出的一步。 另一个好处是此解决方案使我们可以更轻松地组成谓词如下所示 .filter(as(String::isEmpty).negate().and(A::equals))解决所有方法参考 但是我们仍然需要解决一个问题。 我们不能简单地开始创建很多静态的as()函数因为方法引用可能可以用本文开头列出的相同方式解析为几种潜在的as()方法。 因此更好的方法是将功能接口类型名称附加到每个静态方法从而使我们能够以编程方式选择特定的方法参考到功能接口转换方法。 这是一个实用程序类它允许将方法引用转换为驻留在标准Java包java.util.function中的任何匹配的Functional Interface。 在此处直接从GitHub下拉最新版本 import java.util.function.*;/**** author Per Minborg*/
public class FunctionCastUtil {public static T, U BiConsumerT, U asBiConsumer(BiConsumerT, U biConsumer) {return biConsumer;}public static T, U, R BiFunctionT, U, R asBiFunction(BiFunctionT, U, R biFunction) {return biFunction;}public static T BinaryOperatorT asBinaryOperator(BinaryOperatorT binaryOperator) {return binaryOperator;}public static T, U BiPredicateT, U asBiPredicate(BiPredicateT, U biPredicate) {return biPredicate;}public static BooleanSupplier asBooleanSupplier(BooleanSupplier booleanSupplier) {return booleanSupplier;}public static T ConsumerT asConsumer(ConsumerT consumer) {return consumer;}public static DoubleBinaryOperator asDoubleBinaryOperator(DoubleBinaryOperator doubleBinaryOperator) {return doubleBinaryOperator;}public static DoubleConsumer asDoubleConsumer(DoubleConsumer doubleConsumer) {return doubleConsumer;}public static R DoubleFunctionR asDoubleFunction(DoubleFunctionR doubleFunction) {return doubleFunction;}public static DoublePredicate asDoublePredicate(DoublePredicate doublePredicate) {return doublePredicate;}public static DoubleToIntFunction asDoubleToIntFunction(DoubleToIntFunction doubleToIntFunctiontem) {return doubleToIntFunctiontem;}public static DoubleToLongFunction asDoubleToLongFunction(DoubleToLongFunction doubleToLongFunction) {return doubleToLongFunction;}public static DoubleUnaryOperator asDoubleUnaryOperator(DoubleUnaryOperator doubleUnaryOperator) {return doubleUnaryOperator;}public static T, R FunctionT, R asFunction(FunctionT, R function) {return function;}public static IntBinaryOperator asIntBinaryOperator(IntBinaryOperator intBinaryOperator) {return intBinaryOperator;}public static IntConsumer asIntConsumer(IntConsumer intConsumer) {return intConsumer;}public static R IntFunctionR asIntFunction(IntFunctionR intFunction) {return intFunction;}public static IntPredicate asIntPredicate(IntPredicate intPredicate) {return intPredicate;}public static IntSupplier asIntSupplier(IntSupplier intSupplier) {return intSupplier;}public static IntToDoubleFunction asIntToDoubleFunction(IntToDoubleFunction intToDoubleFunction) {return intToDoubleFunction;}public static IntToLongFunction asIntToLongFunction(IntToLongFunction intToLongFunction) {return intToLongFunction;}public static IntUnaryOperator asIntUnaryOperator(IntUnaryOperator intUnaryOperator) {return intUnaryOperator;}public static LongBinaryOperator asLongBinaryOperator(LongBinaryOperator longBinaryOperator) {return longBinaryOperator;}public static LongConsumer asLongConsumer(LongConsumer longConsumer) {return longConsumer;}public static R LongFunctionR asLongFunction(LongFunctionR longFunction) {return longFunction;}public static LongPredicate asLongPredicate(LongPredicate longPredicate) {return longPredicate;}public static T LongSupplier asLongSupplier(LongSupplier longSupplier) {return longSupplier;}public static LongToDoubleFunction asLongToDoubleFunction(LongToDoubleFunction longToDoubleFunction) {return longToDoubleFunction;}public static LongToIntFunction asLongToIntFunction(LongToIntFunction longToIntFunction) {return longToIntFunction;}public static LongUnaryOperator asLongUnaryOperator(LongUnaryOperator longUnaryOperator) {return longUnaryOperator;}public static T ObjDoubleConsumerT asObjDoubleConsumer(ObjDoubleConsumerT objDoubleConsumer) {return objDoubleConsumer;}public static T ObjIntConsumerT asObjIntConsumer(ObjIntConsumerT objIntConsumer) {return objIntConsumer;}public static T ObjLongConsumerT asObjLongConsumer(ObjLongConsumerT objLongConsumer) {return objLongConsumer;}public static T PredicateT asPredicate(PredicateT predicate) {return predicate;}public static T SupplierT asSupplier(SupplierT supplier) {return supplier;}public static T, U ToDoubleBiFunctionT, U asToDoubleBiFunction(ToDoubleBiFunctionT, U toDoubleBiFunction) {return toDoubleBiFunction;}public static T ToDoubleFunctionT asToDoubleFunction(ToDoubleFunctionT toDoubleFunction) {return toDoubleFunction;}public static T, U ToIntBiFunctionT, U asToIntBiFunction(ToIntBiFunctionT, U toIntBiFunction) {return toIntBiFunction;}public static T ToIntFunctionT asToIntFunction(ToIntFunctionT ioIntFunction) {return ioIntFunction;}public static T, U ToLongBiFunctionT, U asToLongBiFunction(ToLongBiFunctionT, U toLongBiFunction) {return toLongBiFunction;}public static T ToLongFunctionT asToLongFunction(ToLongFunctionT toLongFunction) {return toLongFunction;}public static T UnaryOperatorT asUnaryOperator(UnaryOperatorT unaryOperator) {return unaryOperator;}private FunctionCastUtil() {}} 因此在静态导入相关方法之后我们可以编写 Stream.of(A, , B).filter(asPredicate(String::isEmpty).negate()).count();更好的解决方案 如果所有功能接口本身都包含一个静态方法该方法可以采用适当的“方法引用”并将其转换为类型化的功能接口那将更好。 例如标准的Java Predicate功能接口将如下所示 FunctionalInterface
public interface PredicateT {boolean test(T t);default PredicateT and(Predicate? super T other) {...}default PredicateT negate() {...}default PredicateT or(Predicate? super T other) {...}static T PredicateT isEqual(Object targetRef) {...}// New proposed support method to return a // Predicate view of a Functional Reference public static T PredicateT of(PredicateT predicate) {return predicate;}} 这将使我们能够编写 Stream.of(A, , B).filter(Predicate.of(String::isEmpty).negate()).count(); 我个人认为看起来不错 与您最近的Open JDK开发人员联系并提出建议 翻译自: https://www.javacodegeeks.com/2016/03/put-java-8-method-references-work.html