JAVA8函数式接口
函数式接口是java8的一种新特性,函数式接口定义了且只定义了一个抽象方法!该接口非常有用,抽象方法的签名就是可以描述lambda表达式的签名。例子如下:
//自定义了接口
@FunctionalInterface
public interface BufferReaderProcessor {
public String process(BufferedReader b) throws IOException;
}
public static String execute(BufferReaderProcessor p) throws IOException{
try(BufferedReader br = new BufferedReader(new FileReader("SVN.txt"))){
return p.process(br);
}
}
String oneLine = execute((BufferedReader br) -> br.readLine());//该lambda返回的是String
String TwoLine = execute((BufferedReader br) -> br.readLine() + br.readLine());
System.out.println(oneLine + "- -" + TwoLine);
该接口表达式功能即为 将BufferedReader转换为String
接下来是 3个常用的函数式接口:
1.Predicate接口
/**
* Predicate接口定义了一个test方法,接受泛型T对象,返回的是一个boolean。
*/
public static <T> List<T> filter(List<T> list,Predicate<T> p){
List<T> result = new ArrayList<T>();
for(T t:list){
if(p.test(t)){
result.add(t);
}
}
return result;
}
2.Consumer接口
/**
* Consumer接口定义了一个accept的方法,接受泛型T对象,无返回void
*/
public static <T> void forEach(List<T> list,Consumer<T> c){
for(T i:list){
c.accept(i);
}
}
3.Function接口
/**
* Function接口定义了一个apply的方法,接受泛型为T的对象,返回泛型为R的对象
*/
public static <T,R> List<R> map(List<T> list,Function<T,R> f){
List<R> listR = new ArrayList<>();
for(T t:list){
listR.add(f.apply(t));
}
return listR;
}
测试:
public static void main(String[] args) throws IOException {
List<String> Strings = filter(Arrays.asList("1","2","3"),(String s) -> !s.isEmpty());//Predicate示例
forEach(Strings,(String i) -> System.out.println(i));//Consumer示例
List<Integer> ints = map(Arrays.asList("Lambda","in","action"),(String s) -> s.length());//Function示例 从String类型变为Integer类型
forEach(ints,(Integer i) -> System.out.println("长度:"+i));//Consumer示例
}
附录1:Lambda及函数式接口的例子:
使用案例 | lambda例子 | 对应函数式接口 |
---|---|---|
布尔表达式 | (List<String> list) -> list.isEmpty() | Predicate<List<String>> |
创建对象 | () -> new Fan() | Supplier<Fan> |
消费一个对象void | (Fan f) -> sys…out(f.toString) | Consumer<Fan> |
从一个对象中选择提取 | (String s) -> s.length() | Function<String,Integer> 或 ToIntFunction<String> |
合并2个值 | (int a , int b) -> a+b | IntBinaryOperator |
比较2个对象 | (Fan a , Fan b) -> a.getXX().compareTo(b.getXX()) | Comparator<Fan> 或 BiFunction<Fan,Fan,Integer> 或 ToIntBiFunction<Fan.Fan> |
附录2:Java8中常用的函数式接口:
函数式接口 | 函数描述符 | 原始类型特化 |
---|---|---|
Predicate<T> | T -> boolean | IntPredicate<T>, LongPredicate<T>, DoublePredicate<T> |
Consumer<T> | T -> void | IntConsumer, LongConsumer, DoubleConsumer |
Function<T,R> | T -> R | IntFunction<R>, IntToDoubleFunction, IntToLongFunction, LongFunction<R>, LongToDoubleFunction, LongToIntFunction, DoubleFunction<R>, ToIntFunction<T>, ToDoubleFunction<T>, ToLongFunction<T> |
Supplier<T> | () -> T | BooleanSupplier, BooleanSupplier, BooleanSupplier, BooleanSupplier |
UnaryOperator<T> | T -> T | IntUnaryOperator, DoubleUnaryOperator, LongUnaryOperator |
BinaryOperator<T> | (T,T) -> T | IntBinaryOperator, DoubleBinaryOperator, LongBinaryOperator |
BiPredicate<L,R> | (L,R) -> boolean | |
BiConsumer<T,U> | (T,U) -> void | ObjIntConsumer<T>, ObjLongConsumer<T>, ObjDoubleConsumer<T> |
BiFunction<T,U,R> | (T,U) -> R | ToIntBiFunction<T,U>, ToLongBiFunction<T,U>, ToDoubleBiFunction<T,U> |