只是简单给出一个小Demo。具体的理论知识可以看后面的参考链接。
package org.fan.func;
import java.util.function.Function;
/** * Created by fan on 2016/11/4. */
public class FunctionDemo {
//API which accepts an implementation of
//Function interface
static void modifyTheValue(int valueToBeOperated, Function<Integer, Integer> function){
int newValue = function.apply(valueToBeOperated);
/* * Do some operations using the new value. */
System.out.println(newValue);
}
public static void main(String[] args) {
int incr = 20; int myNumber = 10;
//val-> val + incr
//相当于定义了一个方法:
// int function(int val) {
// return val+incr;
// }
modifyTheValue(myNumber, val-> val + incr);
myNumber = 15; modifyTheValue(myNumber, val-> val * 10);
modifyTheValue(myNumber, val-> val - 100);
modifyTheValue(myNumber, val-> "somestring".length() + val - 100);
}
}
运行结果:
30
150
-85
-75
参考链接: