当前位置: 移动技术网 > IT编程>开发语言>Java > 如何更好的使用Java8中方法引用详解

如何更好的使用Java8中方法引用详解

2019年07月19日  | 移动技术网IT编程  | 我要评论

前言

方法引用是用来直接访问类或者实例的已经存在的方法或者构造方法。方法引用提供了一种引用而不执行方法的方式,它需要由兼容的函数式接口构成的目标类型上下文。计算时,方法引用会创建函数式接口的一个实例。

当lambda表达式中只是执行一个方法调用时,不用lambda表达式,直接通过方法引用的形式可读性更高一些。方法引用是一种更简洁易懂的lambda表达式。

注意:方法引用是一个lambda表达式,其中方法引用的操作符是双冒号"::"。

在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<string>
function<string, boolean>

为了解决上述的问题,我们可以通过某种机制显式地将方法引用转换为一个函数接口:

public static <t> predicate<t> as(predicate<t> 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 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();

英文原文:

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网