使您的Java 8方法引用生效

方法参考

众所周知,我们可以使用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至少可以解析为:

  • Predicate<String>
  • Function<String, Boolean>

因此,我们需要以某种方式解决所有潜在的歧义,并确定我们要将方法参考转换为哪个功能接口。 阅读这篇文章,了解如何部分解决此问题。 我使用了开源项目Speedment中提供的代码,该代码使数据库看起来像Java 8 Streams。 随意尝试Speedment out。

Speedment还包含谓词生成器,使您可以直接使用诸如Entity.NAME::isEmpty和Entity.NAME::isNotEmpty之类的函数。

解析方法参考

通过以静态方法的形式引入一些“管道”,可以部分解决该问题,该方法采用“方法参考”并将其作为特定功能接口的视图返回。 考虑以下简短的静态方法:

public static <T> Predicate<T> as(Predicate<T> 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> BiConsumer<T, U> asBiConsumer(BiConsumer<T, U> biConsumer) {return biConsumer;}public static <T, U, R> BiFunction<T, U, R> asBiFunction(BiFunction<T, U, R> biFunction) {return biFunction;}public static <T> BinaryOperator<T> asBinaryOperator(BinaryOperator<T> binaryOperator) {return binaryOperator;}public static <T, U> BiPredicate<T, U> asBiPredicate(BiPredicate<T, U> biPredicate) {return biPredicate;}public static BooleanSupplier asBooleanSupplier(BooleanSupplier booleanSupplier) {return booleanSupplier;}public static <T> Consumer<T> asConsumer(Consumer<T> consumer) {return consumer;}public static DoubleBinaryOperator asDoubleBinaryOperator(DoubleBinaryOperator doubleBinaryOperator) {return doubleBinaryOperator;}public static DoubleConsumer asDoubleConsumer(DoubleConsumer doubleConsumer) {return doubleConsumer;}public static <R> DoubleFunction<R> asDoubleFunction(DoubleFunction<R> 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> Function<T, R> asFunction(Function<T, R> function) {return function;}public static IntBinaryOperator asIntBinaryOperator(IntBinaryOperator intBinaryOperator) {return intBinaryOperator;}public static IntConsumer asIntConsumer(IntConsumer intConsumer) {return intConsumer;}public static <R> IntFunction<R> asIntFunction(IntFunction<R> 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> LongFunction<R> asLongFunction(LongFunction<R> 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> ObjDoubleConsumer<T> asObjDoubleConsumer(ObjDoubleConsumer<T> objDoubleConsumer) {return objDoubleConsumer;}public static <T> ObjIntConsumer<T> asObjIntConsumer(ObjIntConsumer<T> objIntConsumer) {return objIntConsumer;}public static <T> ObjLongConsumer<T> asObjLongConsumer(ObjLongConsumer<T> objLongConsumer) {return objLongConsumer;}public static <T> Predicate<T> asPredicate(Predicate<T> predicate) {return predicate;}public static <T> Supplier<T> asSupplier(Supplier<T> supplier) {return supplier;}public static <T, U> ToDoubleBiFunction<T, U> asToDoubleBiFunction(ToDoubleBiFunction<T, U> toDoubleBiFunction) {return toDoubleBiFunction;}public static <T> ToDoubleFunction<T> asToDoubleFunction(ToDoubleFunction<T> toDoubleFunction) {return toDoubleFunction;}public static <T, U> ToIntBiFunction<T, U> asToIntBiFunction(ToIntBiFunction<T, U> toIntBiFunction) {return toIntBiFunction;}public static <T> ToIntFunction<T> asToIntFunction(ToIntFunction<T> ioIntFunction) {return ioIntFunction;}public static <T, U> ToLongBiFunction<T, U> asToLongBiFunction(ToLongBiFunction<T, U> toLongBiFunction) {return toLongBiFunction;}public static <T> ToLongFunction<T> asToLongFunction(ToLongFunction<T> toLongFunction) {return toLongFunction;}public static <T> UnaryOperator<T> asUnaryOperator(UnaryOperator<T> unaryOperator) {return unaryOperator;}private FunctionCastUtil() {}}

因此,在静态导入相关方法之后,我们可以编写:

Stream.of("A", "", "B").filter(asPredicate(String::isEmpty).negate()).count();

更好的解决方案

如果所有功能接口本身都包含一个静态方法,该方法可以采用适当的“方法引用”并将其转换为类型化的功能接口,那将更好。 例如,标准的Java Predicate功能接口将如下所示:

@FunctionalInterface
public interface Predicate<T> {boolean test(T t);default Predicate<T> and(Predicate<? super T> other) {...}default Predicate<T> negate() {...}default Predicate<T> or(Predicate<? super T> other) {...}static <T> Predicate<T> isEqual(Object targetRef) {...}// New proposed support method to return a // Predicate view of a Functional Reference public static <T> Predicate<T> of(Predicate<T> 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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/354916.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

down.php无法打开,php下载文件 图片不能打开,该怎么解决

php下载文件 图片不能打开function fileDown($file_name){$file_name iconv("utf-8","gb2312",$file_name);$file_path "E:/php/down/".$file_name;if(!file_exists($file_path)){echo "文件不存在";return;}$fp fopen($file_path,…

【转】Linux如何在系统启动时自动加载模块

1、Linux安装驱动程序 tar zxf ixgbe-<x.x.x>.tar.gz cd ixgbe-<x.x.x>/src/ make install modprobe <ixgbe> 卸载驱动 cd ixgbe-<x.x.x>/src/ make uninstall 2、Linux如何在系统启动时自动加载模块 原文&#xff1a;http://www.cnblogs.com/image-ey…

MATLAB小记_fread的用法

“fread”以二进制形式&#xff0c;从文件读出数据。语法1&#xff1a;[a,count]fread(fid,size,precision)语法2&#xff1a;[a,count]fread(fid,size,precision,skip)size: 不指定 &#xff1a;到尾返回读。N : 读出N个数据&#xff0c;构成列向量。inf …

mvvm 后端_ZK实际应用:MVVM –与ZK客户端API一起使用

mvvm 后端在以前的文章中&#xff0c;我们已经使用ZK的MVVM实现了以下功能&#xff1a; 将数据加载到表中 使用表单绑定保存数据 删除条目并以编程方式更新视图 ZK MVVM和ZK MVC实现方式之间的主要区别是&#xff0c;我们不直接在controller&#xff08;ViewModel&#xff0…

微信分享朋友圈固定缩略图 php,微信转发或分享朋友圈带缩略图、标题和描述的实现方法...

自己做博客以来&#xff0c;很早之前分享过文章至朋友圈&#xff0c;那个时候分享过去的文章自动获取页面的比例适合的图片为所缩略图&#xff1a;后期就很少分享至朋友圈&#xff0c; 近来分享文章给朋友后&#xff0c;发现不带缩略图和简介了&#xff0c;觉得这样很不好看&am…

Oracle 用户、表空间、授权、备份、导入等操作相关

一、基础操作 闲来无事&#xff0c;整理oracle数据库相关操作&#xff0c;以后备用。。。。。 ps&#xff1a; satp 为用户 satp_data 为表空间 1 1.删除表空间2 DROP TABLESPACE satp_data INCLUDING CONTENTS AND DATAFILES;3 4 2.删除用户5 drop user satp cascade;6 …

看一下即将发布的JSF 2.3 Push支持

如前几篇文章所述&#xff0c;下一版本的JavaServer Faces&#xff08;Mojarra&#xff09;已添加了许多增强功能。 JSF 2.3计划于2017年与Java EE 8一起发布&#xff0c;但是您现在可以通过从源代码构建或运行里程碑版本来尝试JSF的一些增强和更新&#xff0c;以进行测试。 对…

java mvc中重复提交表单,spring mvc 防止重复提交表单的两种方法,推荐第二种

第一种方法&#xff1a;判断session中保存的token比较麻烦&#xff0c;每次在提交表单时都必须传入上次的token。而且当一个页面使用ajax时&#xff0c;多个表单提交就会有问题。注解Token代码&#xff1a;package com.thinkgem.jeesite.common.repeat_form_validator;import j…

3415 最小和

3415 最小和 链接&#xff1a;http://codevs.cn/problem/3415/ CodeVS原创 时间限制: 1 s空间限制: 64000 KB题目描述 Description小浣熊松松来到文具店&#xff0c;选择了K支自己喜欢的水彩笔&#xff0c;并抄下了它们的价格。可是到结算时&#xff0c;他发现自己抄价格时抄…

java监控rabbitMq服务状态,SpringCloud-Turbine【RabbitMQ服务监控】

前面我们介绍了通过turbine直接聚合多个服务的监控信息&#xff0c;实现了服务的监控&#xff0c;但是这种方式有个不太好的地方就是turbine和服务的耦合性太强了&#xff0c;针对这个问题&#xff0c;我们可以将服务的监控消息发送到RabbitMQ中&#xff0c;然后turbine中Rabbi…

SVN提示被锁定的解决方法(转)

1、&#xff08;常用&#xff09;出现这个问题后使用“清理”即"Clean up"功能&#xff0c;如果还不行&#xff0c;就直接到上一级目录&#xff0c;再执行“清理”&#xff0c;然后再“更新”。 2、&#xff08;没试过&#xff09;有时候如果看到某个包里面的文件夹没…

jpa 查询 列表_终极JPA查询和技巧列表–第1部分

jpa 查询 列表我们可以在Internet上找到一些JPA“如何做”&#xff0c;在本博客的此处&#xff0c;教您如何使用JPA执行多项任务。 通常&#xff0c;我看到有人问有关使用JPA进行查询的问题。 通常&#xff0c;为了回答此类问题&#xff0c;提供了几个链接以尝试找到该问题的解…

windows下php swoole扩展,Windows 下安装 swoole 图文教程(php)

Windows 下安装 swoole 具体步骤&#xff1a;Swoole,原本不支持在Windows下安装的&#xff0c;所以我们要安装Cygwin来使用。在安装Cygwin下遇到了很多坑&#xff0c;百度经验上的文档不是很全&#xff0c;所以我把自己安装Cygwin和Swoole写下来相当于对自己的沉淀吧。首先准备…

软件工程实践2017第一次作业

&#xff08;1&#xff09;回想一下你初入大学时对计算机专业的畅想 当初你是如何做出选择计算机专业的决定的&#xff1f; 高考之后&#xff0c;综合分数、地理位置、专业考虑&#xff0c;搭上了福州大学这趟动车&#xff0c;不过选报的专业给我打了个折&#xff0c;意外的在生…

将JQGrid与Spring MVC和Gson集成

我在一个单页面应用程序上工作&#xff0c;我想在使用Spring MVC的应用程序的一部分中使用网格功能。 自从我上次使用JQGrid以来已经有一段时间了&#xff0c;找到让我起床所需的信息有点困难。 在这篇文章中&#xff0c;我想整理所有信息并将其放入教程中以供遵循&#xff0c;…

php文件写入加1,PHP关于文件与目录(1) 写入文件 文件权限 三、锁定文件

一、文件权限总之一切都是为了保证目录的安全&#xff0c;保证目录的安全比保证文件的安全更重要。二、写入文件file_put_contents($file,$data); //如果没有的话会创建&#xff0c;有的话覆盖原文件&#xff1b;file_put_contents($file,$data,FILE_APPEND); //没的话会创建&a…

DB2 SQL性能调优秘笈pdf

下载地址&#xff1a;网盘下载 简介编辑《DB2 SQL性能调优秘笈》是一本不可多得的DB2数据库性能调优秘笈&#xff0c;由拥有20余年DB2工作经验的资深数据库专家撰写&#xff0c;Amazon全五星评价畅销书。《DB2 SQL性能调优秘笈》不仅详尽阐述了100余条SQL语句优化的技巧和最佳实…

matlab点到曲线的距离视频,使用MATLAB计算点到直线距离

给定平面上 x1, x2, x3三个点&#xff0c;求x1到x2、x3之间的直线的距离。我是用的是内积的方法求解&#xff0c;依据原理是使用内积求出直线x2x1 与 直线x3x1的夹角cos值&#xff0c;进而求出sin值&#xff0c;再求出x1到x2x3的距离。这种方法的不足之处是&#xff0c;当三点之…

新CalendarFX视图:MonthGridView!

我和我的团队最近开始为CalendarFX创建新视图&#xff0c;其最初目标是在垂直列中显示整年。 该视图的名称是MonthGridView。 像往常一样&#xff0c;编码时目标略有变化。 该视图现在可以显示任意数量的月份&#xff0c;并且可以在前面或后面添加额外的月份。 现在&#xff0c…

php如何和c进行数据交换,PHP与 后台c交换数据 | 学步园

为什么要用json跟XML相比&#xff0c;JSON的优势在于格式简洁短小&#xff0c;特别是在处理大量复杂数据的时候&#xff0c;这个优势便显得非常突出。从各浏览器的支持来看&#xff0c;JSON解决了因不同浏览器对XML DOM解析方式不同而引起的问题。目前&#xff0c;JSON已经成为…