Java 8中如何使用方法引用
发表于:2025-12-03 作者:千家信息网编辑
千家信息网最后更新 2025年12月03日,本篇内容主要讲解"Java 8中如何使用方法引用",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Java 8中如何使用方法引用"吧!在Java8中,使用方法
千家信息网最后更新 2025年12月03日Java 8中如何使用方法引用
本篇内容主要讲解"Java 8中如何使用方法引用",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Java 8中如何使用方法引用"吧!
在Java8中,使用方法引用非常简单,如String::isEmpty,但无法使用它否定的方法引用。本文内容即如何解决此问题使得我们能够更加全面地使用方法引用。
首先看一个使用方法引用的例子:
Stream.of("A", "", "B").filter(String::isEmpty).count()上面代码的输出为1,即空字符串的数目。
如果我们想要获取非空字符串的数目,就不能直接使用方法引用了。
Stream.of("A", "", "B").filter(s -> !s.isEmpty()).count()
Java8中的Predicate,有predicate.negate()可以转换为断言的否定形式,但String::isEmpty却无法这么做(String::isEmpty.negate()或者!String::isEmpty)。
因为方法引用并不是一个lambda或者函数接口,它能够被解析为一个或者多个函数接口。如,String::isEmpty至少可以被解析如下:
Predicate
Function
为了解决上述的问题,我们可以通过某种机制显式地将方法引用转换为一个函数接口:
public staticPredicate as(Predicate predicate) { return predicate;}
通过使用一个静态方法,接受方法引用参数,返回一个函数接口,即可实现方法引用到函数接口的转换。接着,我们就可以使用方法引用来实现上面例子中的获取非空字符串的数目。Stream.of("A", "", "B").filter(as(String::isEmpty).negate()).count();
进一步还能使用各种组合的Predicate。
.filter(as(String::isEmpty).negate().and("A"::equals))由于一个方法引用可能会被解析为多种函数接口,因此如果我们实现很多参数不同的as方法,那么很容易造成混淆。更好的方式则是在方法名中加入函数参数的类型来区分。
import java.util.function.*;public class FunctionCastUtil {public staticBiConsumer asBiConsumer(BiConsumer biConsumer) { return biConsumer;}public staticBiFunction asBiFunction(BiFunction biFunction) { return biFunction;}public staticBinaryOperator asBinaryOperator(BinaryOperator binaryOperator) { return binaryOperator;}public staticBiPredicate asBiPredicate(BiPredicate biPredicate) { return biPredicate;}public static BooleanSupplier asBooleanSupplier(BooleanSupplier booleanSupplier) {return booleanSupplier;}public staticConsumer asConsumer(Consumer consumer) { return consumer;}public static DoubleBinaryOperator asDoubleBinaryOperator(DoubleBinaryOperator doubleBinaryOperator) {return doubleBinaryOperator;}public static DoubleConsumer asDoubleConsumer(DoubleConsumer doubleConsumer) {return doubleConsumer;}public staticDoubleFunction asDoubleFunction(DoubleFunction 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 staticFunction asFunction(Function function) { return function;}public static IntBinaryOperator asIntBinaryOperator(IntBinaryOperator intBinaryOperator) {return intBinaryOperator;}public static IntConsumer asIntConsumer(IntConsumer intConsumer) {return intConsumer;}public staticIntFunction asIntFunction(IntFunction 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 staticLongFunction asLongFunction(LongFunction longFunction) { return longFunction;}public static LongPredicate asLongPredicate(LongPredicate longPredicate) {return longPredicate;}public staticLongSupplier 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 staticObjDoubleConsumer asObjDoubleConsumer(ObjDoubleConsumer objDoubleConsumer) { return objDoubleConsumer;}public staticObjIntConsumer asObjIntConsumer(ObjIntConsumer objIntConsumer) { return objIntConsumer;}public staticObjLongConsumer asObjLongConsumer(ObjLongConsumer objLongConsumer) { return objLongConsumer;}public staticPredicate asPredicate(Predicate predicate) { return predicate;}public staticSupplier asSupplier(Supplier supplier) { return supplier;}public staticToDoubleBiFunction asToDoubleBiFunction(ToDoubleBiFunction toDoubleBiFunction) { return toDoubleBiFunction;}public staticToDoubleFunction asToDoubleFunction(ToDoubleFunction toDoubleFunction) { return toDoubleFunction;}public staticToIntBiFunction asToIntBiFunction(ToIntBiFunction toIntBiFunction) { return toIntBiFunction;}public staticToIntFunction asToIntFunction(ToIntFunction ioIntFunction) { return ioIntFunction;}public staticToLongBiFunction asToLongBiFunction(ToLongBiFunction toLongBiFunction) { return toLongBiFunction;}public staticToLongFunction asToLongFunction(ToLongFunction toLongFunction) { return toLongFunction;}public staticUnaryOperator asUnaryOperator(UnaryOperator unaryOperator) { return unaryOperator;}private FunctionCastUtil() {}}Stream.of("A", "", "B").filter(asPredicate(String::isEmpty).negate()).count()
原文:https://dzone.com/articles/put-your-java-8-method-references-to-work
译文:https://www.rowkey.me/blog/2017/09/02/java8-method-reference-work/
到此,相信大家对"Java 8中如何使用方法引用"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
方法
使用方法
函数
接口
内容
参数
数目
空字符
例子
问题
学习
不同
实用
更深
代码
兴趣
原文
可以通过
多个
多种
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
网络技术的重要性
北京网络安全设备
惠民软件开发公司王秀兰
无锡动能服务器多少钱
网络安全和信息化安全汇报
杭州服务器淘汰公司哪家好
数据库多例 会存在什么问题
全程无忧软件开发
nosql数据库的种类
证华网络技术
sql数据库基本语句大全
数据库发现论文空白
pb数据库数据导入
航空工业软件开发成都
序列号算法软件开发
长虹电视无法连接dns服务器
付费保安全 服务器快照
服务器主机区别
pg数据库获取表结构
网络安全工作的核心
安徽服务器机柜定做虚拟主机
大兴回收服务器价格
网络安全年会2017
nosql数据库的种类
网络安全小剧本小学生
c4top网络技术挑战赛
ibm 3500 服务器
数据库事务控制的关键字
图数据库应用大数据分析
软件开发需要动脑吗