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> |