參考文章
1 爲什麼使用Lamdba表達式
https://dzone.com/articles/why-we-need-Lambda-expressions
2 什麼是函數式編程
http://eloquentjavascript.net/1st_edition/chapter6.html
3 lambda文章
https://testerhome.com/topics/3567
自我感受Lamdba
雖然有的時候會使用Lamdba表達式,但有的時候只是簡單使用,並沒有做深入的瞭解,當然看到一些比較複雜的表達式,還是會上網查,琢磨好久,所以想着把Lamdba從根學好,順便總結一下,輸出分享~
傳遞的是行爲,而不是值,這是讓我最興奮,最能提現他價值的地方,讓我體會了抽象,哈哈哈,我比較菜~
lambda給我的感受就是好用,讓我體會抽象,哈哈,通過參數傳遞行爲,這種感覺超棒~一個函數式接口可以搞定很多事情,不用在方法體內寫具體執行方法,對於Java本身而言好用很多。
這種圖我感覺容納了很多知識點,以後的分享從這裏的點滴分享~
什麼是Lamdba表達式
Lambda表達式是函數式接口,對於小白來說聽到函數式接口的時候,其實內心應該想的是什麼是函數式接口?簡單點說:就沒事沒有方法名,修飾符,返回值。其實跟js大同小異。
Lambda的書寫形式是: (argument) -> (body) 。 把這種格式記住,基本大同小異。有兩部分組成,參數+函數體
Lamdba表達式列舉格式
()->{System.out.print("judy")}
(String judy)->{System.out.print(judy)}
(int a1 , int a2)->{System.out.print(a+b)}
從它的格式中我們可以看出可以接受參數,也可以午餐,花括號內使用的是具體的執行行爲。
有的時候我們可以省略花括號,類似於你寫if判斷的時候只有一條語句的時候可以忽略花括號
Lamdba表達式的演進過程
1 jdk5
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
System.out.println("------java 5---");
2 jdk8
for (Integer integer : list) {
System.out.println(integer);
}
System.out.println("------java8----");
3 函數式接口
List list1 = Arrays.asList(1, 2, 3, 4);
list1.forEach(i->{
System.out.println(i);
});
4 方法引用
list1.forEach(System.out::println);
Lamdba爲什麼出現
對於Java而言他是面向對象的,但是Java缺少了函數式編程的概念,當Lamdba出現的時候正好彌補了java 這塊的缺陷
無論是什麼事物,一旦出現肯定有他的道理,我曾經有一個面試官問我爲什麼會有Lambda表達式,內心想的是當然是因爲他高效和好用啊。把原理說出來就好了
借用我看其他博主的文章的例子,我感覺千言萬語不如用代碼來說,哈哈,使用lamdba的寫法
public static void main(String[] args) {
System.out.println(test.compore(1, item -> {
return item * 2;
}));
System.out.println(test.compore(1, item -> {
return 1 % item;
}));
System.out.println(test.compore(1, item -> {
return 1 / item;
}));
System.out.println(test.compore(1, item -> {
return 1 - item;
}));
}
private int compore(int a, Function<Integer, Integer> function) {
//無論誰調用,傳遞的都是行爲。
int result = function.apply(a);
return result;
}
如果沒有使用lamdba,那麼你可能需要這樣寫。
private int method1(int a) {
return 1 * a;
}
private int method2(int b) {
return 1 / b;
}
private int method3(int b) {
return 1 % b;
}
什麼是函數式接口?
函數式接口必須滿足的條件
1 一個接口只有一個抽象方法,
2 在接口上聲明@FunctionInterface
舉個栗子
Function不陌生吧,接口上定義的就是@FunctionalInterface
/**
* Represents a function that accepts one argument and produces a result.
*
* <p>This is a <a href="package-summary.html">functional interface</a>
* whose functional method is {@link #apply(Object)}.
*
* @param <T> the type of the input to the function
* @param <R> the type of the result of the function
*
* @since 1.8
*/
@FunctionalInterface
public interface Function<T, R> {
/**
* Applies this function to the given argument.
*
* @param t the function argument
* @return the function result
*/
R apply(T t);
}
最後舉個栗子
public static void main(String[] args) {
JFrame jFrame = new JFrame("My JFrame");
JButton jButton = new JButton("My JButton");
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button Judy");
}
});
}
//雖然沒有@FunctionalInterface 修飾,但是他只有一個抽象方法。
public interface ActionListener extends EventListener {
/**
* Invoked when an action occurs.
*/
public void actionPerformed(ActionEvent e);
}
/**
* Adds an <code>ActionListener</code> to the button.
* @param l the <code>ActionListener</code> to be added
*/
public void addActionListener(ActionListener l) {
listenerList.add(ActionListener.class, l);
}
//如果用Lamdba我麼怎麼寫?
jButton.addActionListener((ActionEvent event) -> System.out.println("Button Judy"));
總結
只是簡單的做了一個前期的鋪墊,之後大概有兩篇見解lambda。原持續前進。