Function接口 – Java8中java.util.function包下的函数式接口

Introduction to Functional Interfaces – A concept recreated in Java 8

Any java developer around the world would have used at least one of the following interfaces: java.lang.Runnable,java.awt.event.ActionListener, java.util.Comparator,java.util.concurrent.Callable. There is some common feature among the stated interfaces and that feature is they have only one method declared in their interface definition. There are lot more such interfaces in JDK and also lot more created by java developers. These interfaces are also called Single Abstract Method interfaces (SAM Interfaces). And a popular way in which these are used is by creating Anonymous Inner classes using these interfaces, something like:
[java]
public class AnonymousInnerClassTest {
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Aad created and running …");
}
}).start();
}
}
[/java]

With Java 8 the same concept of SAM interfaces is recreated and are called Functional interfaces. These can be represented using Lambda expressions, Method reference and constructor references(I will cover these two topics in the upcoming blog posts). There’s an annotation introduced- @FunctionalInterface which can be used for compiler level errors when the interface you have annotated is not a valid Functional Interface. Lets try to have a look at a simple functional interface with only one abstract method:
[java]
@FunctionalInterface
public interface SimpleFuncInterface {
public void doWork();
}
[/java]
The interface can also declare the abstract methods from thejava.lang.Object class, but still the interface can be called as a Functional Interface:
[java]
@FunctionalInterface
public interface SimpleFuncInterface {
public void doWork();
public String toString();
public boolean equals(Object o);
}
[/java]
Once you add another abstract method to the interface then the compiler/IDE will flag it as an error as shown in the screenshot below:
Function接口 – Java8中java.util.function包下的函数式接口
Interface can extend another interface and in case the Interface it is extending in functional and it doesn’t declare any new abstract methods then the new interface is also functional. But an interface can have one abstract method and any number of default methods and the interface would still be called an functional interface. To get an idea of default methods please read here.
[java]
@FunctionalInterface
public interface ComplexFunctionalInterface extends SimpleFuncInterface {
default public void doSomeWork(){
System.out.println("Doing work in interface impl…");
}
default public void doSomeOtherWork(){
System.out.println("Doing other work in interface impl…");
}
}
[/java]
The above interface is still a valid functional interface. Now lets see how we can use the lambda expression as against anonymous inner class for implementing functional interfaces:
[java]
/*
* Implementing the interface by creating an
* anonymous inner class versus using
* lambda expression.
*/
public class SimpleFunInterfaceTest {
public static void main(String[] args) {
carryOutWork(new SimpleFuncInterface() {
@Override
public void doWork() {
System.out.println("Do in SimpleFun impl…");
}
});
carryOutWork(() -> System.out.println("Do in lambda exp impl…"));
}
public static void carryOutWork(SimpleFuncInterface sfi){
sfi.doWork();
}
}
[/java]
And the output would be …
[shell]
Do work in SimpleFun impl…
Do work in lambda exp impl…
[/shell]
In case you are using an IDE which supports the Java Lambda expression syntax(Netbeans 8 Nightly builds) then it provides an hint when you use an anonymous inner class as used above
Function接口 – Java8中java.util.function包下的函数式接口
This was a brief introduction to the concept of functional interfaces in java 8 and also how they can be implemented using Lambda expressions.

 

如果你正在浏览Java8的API,你会发现java.util.function中 Function, Supplier, Consumer, Predicate和其他函数式接口广泛用在支持lambda表达式的API中。这些接口有一个抽象方法,会被lambda表达式的定义所覆盖。在这篇文章中,我会简单描述Function接口,该接口目前已发布在java.util.function中。

Function接口的主要方法:

R apply(T t) – 将Function对象应用到输入的参数上,然后返回计算结果。

default ‹V› Function‹T,V› – 将两个Function整合,并返回一个能够执行两个Function对象功能的Function对象。

译者注:Function接口中除了apply()之外全部接口如下:

default <V> Function<T,V> andThen(Function<? super R,? extends V> after) 返回一个先执行当前函数对象apply方法再执行after函数对象apply方法的函数对象。

default <V> Function<T,V> compose(Function<? super V,? extends T> before)返回一个先执行before函数对象apply方法再执行当前函数对象apply方法的函数对象。

static <T> Function<T,T> identity() 返回一个执行了apply()方法之后只会返回输入参数的函数对象。

本章节将会通过创建接受Function接口和参数并调用相应方法的例子探讨apply方法的使用。我们同样能够看到API的调用者如何利用lambda表达式替代接口的实现。除了传递lambda表达式之外,API使用者同样可以传递方法的引用,但这样的例子不在本篇文章中。

如果你想把接受一些输入参数并将对输入参数处理过后的结果返回的功能封装到一个方法内,Function接口是一个不错的选择。输入的参数类型和输出的结果类型可以一致或者不一致。

一起来看看接受Function接口实现作为参数的方法的例子:

01 public class FunctionDemo {
02  
03     //API which accepts an implementation of
04  
05     //Function interface
06  
07     static void modifyTheValue(int valueToBeOperated, Function<Integer, Integer> function){
08  
09         int newValue = function.apply(valueToBeOperated);
10  
11         /*     
12          * Do some operations using the new value.     
13          */
14  
15         System.out.println(newValue);
16  
17     }
18  
19 }

下面是调用上述方法的例子:

01 public static void main(String[] args) {
02  
03     int incr = 20;  int myNumber = 10;
04  
05     modifyTheValue(myNumber, val-> val + incr);
06  
07     myNumber = 15;  modifyTheValue(myNumber, val-> val * 10);
08  
09     modifyTheValue(myNumber, val-> val - 100);
10  
11     modifyTheValue(myNumber, val-> "somestring".length() + val - 100);
12  
13 }

你可以看到,接受1个参数并返回执行结果的lambda表达式创建在例子中。这个例子的输入如下:

1 30
2  
3 150
4  
5 -85
6  
7 -75
上一篇:ado执行sql查询出现“发送数据流时出现算术溢出”错误


下一篇:《Entity Framework 6 Recipes》中文翻译系列 (34) ------ 第六章 继承与建模高级应用之多条件与QueryView