首页 / JAVA / Java8新特性探索之函数式接口
Java8新特性探索之函数式接口
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java8新特性探索之函数式接口,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含11939字,纯文字阅读大概需要18分钟。
内容图文
![Java8新特性探索之函数式接口](/upload/InfoBanner/zyjiaocheng/623/9c89acc1e278443885ec6e5a4721add6.jpg)
一、为什么引入函数式接口
作为Java函数式编程爱好者,我们都知道方法引用和 Lambda 表达式都必须被赋值,同时赋值需要类型信息才能使编译器保证类型的正确性。
我们先看一个Lambda代码示例:
x -> x.toString()
我们清楚这里返回类型必须是 String,但 x 是什么类型呢?
Lambda 表达式包含类型推导(编译器会自动推导出类型信息,避免了程序员显式地声明),编译器必须能够以某种方式推导出 x 的类型以生成正确的代码。
同样方法引用也存在此问题,假设你要传递 System.out :: println 到你正在编写的方法 ,你怎么知道传递给方法的参数的类型?
为了解决上述问题,Java 8 引入了函数式接口,在 java.util.function 包,它包含一组接口,这些接口是 Lambda 表达式和方法引用的目标类型,每个接口只包含一个抽象方法,称为函数式方法。只有确保接口中有且仅有一个抽象方法,Lambda表达式的类型信息才能顺利地进行推导。
二、如何使用函数式接口
在编写接口时,可以使用 @FunctionalInterface 注解强制执行此函数式方法模式:
在接口上使用注解 @FunctionalInterface ,一旦使用该注解来定义接口,编译器将会强制检查该接口是否确实有且仅有一个抽象方法,否则将会报错。
@FunctionalInterface
public interface MyFunction {
/**
- 自定义的抽象方法
*/
void run();
}
在函数式接口,有且仅有一个抽象方法,Object的public方法除外
@FunctionalInterface
public interface MyFunction {
/**
- 自定义的抽象方法
*/
void run();
/**
- Object的equals方法
- @param obj
- @return
*/
@Override
boolean equals(Object obj);
/**
- Object的toString方法
- @return
*/
@Override
String toString();
/**
- Object的hashCode方法
- @return
*/
@Override
int hashCode();
}
在函数式接口中,我们可以使用default修饰符定义默认方法,使用static修饰符定义静态方法
@FunctionalInterface
public interface MyFunction {
/**
- 自定义的抽象方法
*/
void run();
/**
-
static修饰符定义静态方法
*/
static void staticRun() {
System.out.println("接口中的静态方法");
}/**
- default修饰符定义默认方法
*/
default void defaultRun() {
System.out.println("接口中的默认方法");
}
- default修饰符定义默认方法
}
为大家演示下自定义无泛型的函数式接口测试实例:
/**
- 自定义的无泛型函数式接口
*/
@FunctionalInterface
public interface MyFunction {
/**
-
自定义的抽象方法
-
@param x
*/
void run(Integer x);/**
- default修饰符定义默认方法
- @param x
*/
default void defaultMethod(Integer x) {
System.out.println("接口中的默认方法,接收参数是:" + x);
}
}
/**
- 测试类
*/
public class MyFunctionTest {
@Test
public void functionTest() {
test(6, (x) -> System.out.println("接口中的抽象run方法,接收参数是:" + x));
}
public void test(int n, MyFunction function) {
System.out.println(n);
function.defaultMethod(n);
function.run(n);
}
}
输出结果:
6
接口中的默认方法,接收参数是:6
接口中的抽象run方法,接收参数是:6
为大家演示下自定义有泛型的函数式接口测试实例:
/**
- 自定义的有泛型函数式接口
*/
@FunctionalInterface
public interface MyFunctionGeneric{
/**
- 转换值
- @param t
- @return
*/
T convertValue(T t);
}
/**
- 测试类
*/
public class MyFunctionGenericTest {
@Test
public void convertValueTest() {
String result = toLowerCase((x) -> x.toLowerCase(), "ABC");
System.out.println(result);
}
public String toLowerCase(MyFunctionGeneric
return functionGeneric.convertValue(value);
}
}
输出结果:
abc
注意:作为参数传递 Lambda 表达式:为了将 Lambda 表达式作为参数传递,接收Lambda 表达式的参数类型必须是与该 Lambda 表达式兼容的函数式接口 的类型。
三、Java8四大内置核心函数式接口
首先总览下四大函数式接口的特点说明:
接口 参数类型 返回类型 方法 说明
Consumer T void void accept(T t) 消费型接口,对类型T参数操作,无返回结果
Supplier - T T get() 供给型接口,创造T类型参数
Function T R R apply(T t) 函数型接口,对类型T参数操作,返回R类型参数
Predicate T boolean boolean test(T t) 断言型接口,对类型T进行条件筛选操作
消费型接口Consumer
java.util.function.Consumer
接口源码:
package java.util.function;
import java.util.Objects;
@FunctionalInterface
public interface Consumer
void accept(T t);
default Consumer<T> andThen(Consumer<? super T> after) {
Objects.requireNonNull(after);
return (T t) -> { accept(t); after.accept(t); };
}
}
抽象方法:void accept(T t),接收并消费一个指定泛型的数据,无需返回结果。
默认方法:default Consumer
public class ConsumerTest {
/**
- 先计算总分,再计算平均分
*/
@Test
public void calculate() {
Integer[] fraction = new Integer[] { 65, 76, 85, 92, 88, 99 };
consumer(fraction, x -> System.out.println(Arrays.stream(x).mapToInt(Integer::intValue).sum()),
y -> System.out.println(Arrays.stream(y).mapToInt(Integer::intValue).average().getAsDouble()));
}
public void consumer(Integer[] fraction, Consumer<Integer[]> x, Consumer<Integer[]> y) {
x.andThen(y).accept(fraction);
}
}
输出结果:
505
84.16666666666667
由于Consumer的default方法所带来的嵌套调用(连锁调用),对行为的抽象的函数式编程理念,展示的淋漓尽致。
其他的消费型函数式接口汇总说明:
接口名称 方法名称 方法签名
DoubleConsumer accept (double) -> void
IntConsumer accept (int) -> void
LongConsumer accept (long) -> void
ObjDoubleConsumer accept (T, double) -> void
ObjIntConsumer accept (T, int) -> void
ObjLongConsumer accept (T, long) -> void
供给型接口Supplier
java.util.function.Supplier
接口源码:
package java.util.function;
@FunctionalInterface
public interface Supplier
T get();
}
由于这是一个函数式接口,意味着对应的Lambda表达式需要对外提供一个符合泛型类型的对象数据。
public class SupplierTest {
public int getMax(Supplier
return supplier.get();
}
/**
- 获取数组元素最大值
*/
@Test
public void getMaxTest() {
Integer[] data = new Integer[] { 5, 4, 6, 3, 2, 1 };
int result = getMax(() -> {
int max = 0;
for (int i = 0; i < data.length; i++) {
max = Math.max(max, data[i]);
}
return max;
});
System.out.println(result);
}
}
其他的供给型函数式接口汇总说明:
接口名称 方法名称 方法签名
BooleanSupplier getAsBoolean () -> boolean
DoubleSupplier getAsDouble () -> double
IntSupplier getAsInt () -> int
LongSupplier getAsLong () -> long
函数型接口Function
java.util.function.Function<T,R> 接口用来根据一个类型的数据得到另一个类型的数据,前者称为前置条件,后者称为后置条件。
接口源码:
package java.util.function;
import java.util.Objects;
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
static <T> Function<T, T> identity() {
return t -> t;
}
}
抽象方法 apply(T t):该方法接收入参是一个泛型T对象,并返回一个泛型T对象。
默认方法
andThen(Function<? super R, ? extends V> after):该方法接受一个行为,并将父方法处理过的结果作为参数再处理。
compose(Function<? super V, ? extends T> before):该方法正好与andThen相反,它是先自己处理然后将结果作为参数传给父方法执行。
@Test
public void andThenAndComposeTest() {
// 计算公式相同
Function<Integer, Integer> andThen1 = x -> x + 1;
Function<Integer, Integer> andThen2 = x -> x * 2;
Function<Integer, Integer> compose1 = y -> y + 1;
Function<Integer, Integer> compose2 = y -> y * 2;
// 注意调用的先后顺序
// 传入参数2后,先执行andThen1计算,将结果再传入andThen2计算
System.out.println(andThen1.andThen(andThen2).apply(2));
// 传入参数2后,先执行compose2计算,将结果再传入compose1计算
System.out.println(compose1.compose(compose2).apply(2));
}
输出结果:
6
5
静态方法identity():获取到一个输入参数和返回结果一样的Function实例。
来一个自驾九寨沟的代码示例:
public class FunctionTest {
@Test
public void findByFunctionTest() {
Function<BigDecimal, BigDecimal> getMoney = m -> m.add(new BigDecimal(1000));
BigDecimal totalCost = getMoney.apply(new BigDecimal(500));
System.out.println("张三的钱包原本只有500元,自驾川西得去银行再取1000元,取钱后张三钱包总共有" + Function.identity().apply(totalCost) + "元");
BigDecimal surplus = cost(totalCost, (m) -> {
System.out.println("第二天出发前发现油不足,加油前有" + m + "元");
BigDecimal lubricate = m.subtract(new BigDecimal(300));
System.out.println("加油300后还剩余" + lubricate + "元");
return lubricate;
}, (m) -> {
System.out.println("到达景区门口,买景区票前有" + m + "元");
BigDecimal tickets = m.subtract(new BigDecimal(290));
System.out.println("买景区票290后还剩余" + tickets + "元");
return tickets;
});
System.out.println("最后张三返程到家还剩余" + surplus + "元");
}
public BigDecimal cost(BigDecimal money, Function<BigDecimal, BigDecimal> lubricateCost,
Function<BigDecimal, BigDecimal> ticketsCost) {
Function<BigDecimal, BigDecimal> firstNight = (m) -> {
System.out.println("第一晚在成都住宿前有" + m + "元");
BigDecimal first = m.subtract(new BigDecimal(200));
System.out.println("交完200住宿费还剩余" + first + "元");
return first;
};
Function<BigDecimal, BigDecimal> secondNight = (m) -> {
System.out.println("第二晚在九寨县住宿前有" + m + "元");
BigDecimal second = m.subtract(new BigDecimal(200));
System.out.println("交完200住宿费还剩余" + second + "元");
return second;
};
return lubricateCost.andThen(ticketsCost).andThen(secondNight).compose(firstNight).apply(money);
}
}
输出结果:
张三的钱包原本只有500元,自驾川西得去银行再取1000元,取钱后张三钱包总共有1500元
第一晚在成都住宿前有1500元
交完200住宿费还剩余1300元
第二天出发前发现油不足,加油前有1300元
加油300后还剩余1000元
到达景区门口,买景区票前有1000元
买景区票290后还剩余710元
第二晚在九寨县住宿前有710元
交完200住宿费还剩余510元
最后张三返程到家还剩余510元
其他的函数型函数式接口汇总说明:
接口名称 方法名称 方法签名
BiFunction apply (T, U) -> R
DoubleFunction apply (double) -> R
DoubleToIntFunction applyAsInt (double) -> int
DoubleToLongFunction applyAsLong (double) -> long
IntFunction apply (int) -> R
IntToDoubleFunction applyAsDouble (int) -> double
IntToLongFunction applyAsLong (int) -> long
LongFunction apply (long) -> R
LongToDoubleFunction applyAsDouble (long) -> double
LongToIntFunction applyAsInt (long) -> int
ToDoubleFunction applyAsDouble (T) -> double
ToDoubleBiFunction applyAsDouble (T, U) -> double
ToIntFunction applyAsInt (T) -> int
ToIntBiFunction applyAsInt (T, U) -> int
ToLongFunction applyAsLong (T) -> long
ToLongBiFunction applyAsLong (T, U) -> long
断言型接口Predicate
java.util.function.Predicate
接口源码:
package java.util.function;
import java.util.Objects;
@FunctionalInterface
public interface Predicate
boolean test(T t);
default Predicate<T> and(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) && other.test(t);
}
default Predicate<T> negate() {
return (t) -> !test(t);
}
default Predicate<T> or(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) || other.test(t);
}
static <T> Predicate<T> isEqual(Object targetRef) {
return (null == targetRef)
? Objects::isNull
: object -> targetRef.equals(object);
}
}
既然是条件判断,就会存在与、或、非三种常见的逻辑关系。其中将两个 Predicate 条件使用与逻辑连接起来实现并且的效果时,类始于 Consumer接口 andThen()函数 其他三个雷同。
public class PredicateTest {
/**
- 查找在渝北的Jack
*/
@Test
public void findByPredicateTest() {
Listlist = Lists.newArrayList(new User("Johnson", "渝北"), new User("Tom", "渝中"), new User("Jack", "渝北"));
getNameAndAddress(list, (x) -> x.getAddress().equals("渝北"), (x) -> x.getName().equals("Jack"));
}
public void getNameAndAddress(List
users.stream().filter(user -> name.and(address).test(user)).forEach(user -> System.out.println(user.toString()));
}
}
输出结果:
User [name=Jack, address=渝北]
其他的断言型函数式接口汇总说明:
接口名称 方法名称 方法签名
BiPredicate test (T, U) -> boolean
DoublePredicate test (double) -> boolean
IntPredicate test (int) -> boolean
LongPredicate test (long) -> boolean
四、总结
Lambda 表达式和方法引用并没有将 Java 转换成函数式语言,而是提供了对函数式编程的支持。这对 Java 来说是一个巨大的改进,因为这允许你编写更简洁明了,易于理解的代码。
内容总结
以上是互联网集市为您收集整理的Java8新特性探索之函数式接口全部内容,希望文章能够帮你解决Java8新特性探索之函数式接口所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。