Java JDK8新特性Lambda表达式
1.Lambda表達式
Lambd表達式可以理解為一種可傳遞的匿名函數。它沒有名稱,但有參數列表、函數主體、返回類型,還有可能會拋出異常。在package java.util中有一個接口,接著創建蘋果類:
@FunctionalInterface public interface Comparator<T> {int compare(T o1, T o2);... } public class Apple {private int weight;private String color;public Apple(int weight, String color) {this.weight = weight;this.color = color;}public int getWeight() {return weight;}public void setWeight(int weight) {this.weight = weight;}public String getColor() {return color;}public void setColor(String color) {this.color = color;} }????????匿名類的表達形式:
Comparator<Apple> appleComparator = new Comparator<Apple>() {@Overridepublic int compare(Apple o1, Apple o2) {return o1.getWeight()-o2.getWeight();} };? ? ? ? Lambda表達式形式:
Comparator<Apple> appleComparator1 = (Apple apple1, Apple apple2)->apple1.getWeight()-apple1.getWeight();? ? ? ? ?可以看到Lambda表達式更簡單:
????????????????1. ()->{}? 無參,{}做處理;
? ? ? ? ? ? ? ? 2.()->retrun 值; 無參,返回值;
? ? ? ? ? ? ? ? 3.(String s)->s 有參,返回值;
? ? ? ? ? ? ? ? 4.(String s)->{} 有參,做處理。
2.函數式接口
? ? ? ? 只定義一個抽象方法的接口。它就是函數式接口。@FuntionallInterface注解,表示會設計成一個函數接口,不符合規范,會編譯報錯。
? ? ? ? jdk8 自定義了簡單的四個函數式接口:
@FunctionalInterface public interface Predicate<T> {//接收一個參數t,返回booleanboolean test(T t);//創建predicate實例, predicate.and(other),結果為 predicate.test(t) and other.test(t)default Predicate<T> and(Predicate<? super T> other);//創建predicate實例, predicate.or(other),結果為 predicate.test(t) or other.test(t)default Predicate<T> or(Predicate<? super T> other);//創建predicate實例, predicate.negate()結果為 !predicate.test(t)default Predicate<T> negate()// Predicate.isEuqal("true").test("true") static <T> Predicate<T> isEqual(Object targetRef) }對應的測試結果:
Predicate<String> predicate1=s->Boolean.parseBoolean(s); System.out.println(predicate1.test("true")); //true System.out.println(predicate1.and(s -> Boolean.parseBoolean(s)).test("false"));//false System.out.println(predicate1.or(s -> Boolean.parseBoolean(s)).test("false"));//false System.out.println(predicate1.negate().test("true"));//false System.out.println(Predicate.isEqual("true").test("true"));//true @FunctionalInterface public interface Consumer<T> {//接受一個參數tvoid accept(T t);//創建一個consumer實例, consumer.andThen(after)結果 先運行consumer.accept(t) ,接著運行 after.accept(t)default Consumer<T> andThen(Consumer<? super T> after); }對應的測試結果:
Consumer<String> consumer= s-> System.out.println(s+"1"); consumer.accept("1");//11 consumer.andThen(s-> System.out.println(s+"2")).accept("1");//輸出11 換行12 @FunctionalInterface public interface Function<T, R> {//輸入t,返回rR apply(T t);//創建一個function實例, r= function.compose(before)的結果 t = before.apply(v); r=function.apply(t)default <V> Function<V, R> compose(Function<? super V, ? extends T> before);//創建一個function實例, r=function.andThen(after)的結果 v = function.apply(t); r=after.apply(v)default <V> Function<T, V> andThen(Function<? super R, ? extends V> after);//t = Function.identity().apply(t)static <T> Function<T, T> identity() {return t -> t;} }對應的測試結果:
Function<String,String> function = s-> s+"1"; System.out.println(function.apply("1"));//11 System.out.println(function.andThen(s -> s + "2").apply("1"));//112 System.out.println(function.compose(s -> s + "2").apply("1"));//121 System.out.println(Function.identity().apply("1"));//1 @FunctionalInterface public interface Supplier<T> {//返回T類型T get(); }對應的測試結果:
Supplier<String> supplier = ()->"1"; System.out.println(supplier.get());//13.方法引用
?3.1 類名::靜態方法名
?3.2 instance::instance方法名
?3.3 類名::instance方法名
?3.4 類名::new
????????
[1]: Java8 實戰 java 8 in Action Mario Fusco 著 Alan Mycroft 陸明剛 勞佳 譯
總結
以上是生活随笔為你收集整理的Java JDK8新特性Lambda表达式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机多媒体是,计算机多媒体技术
- 下一篇: 摄像头poe供电原理_POE供电的原理以