最近一直在寫React Native相關(guān)的東西嘶是,擔心會淡忘Java的東西闻丑,所以買了本JDK8 閑來瞅瞅关顷,沒想到卻被其中的新特性深深的吸引邢享,幸好沒有放棄洽腺,抓緊時間拿出來和大家探討探討脚粟。
先來說說今天的標題,java我就不再細細介紹了蘸朋,畢竟是個耳熟能詳?shù)臇|西核无,介紹一下Kotlin吧!
Kotlin是對java的一層包裝藕坯,而他的語法更加接近于函數(shù)式編程团南,這些大家都可以在Kotlin官網(wǎng)中找到詳細的介紹。之所以我會使用Kotlin去寫自己的項目主要有以下幾點考慮
- 使用Kotlin可以使用lambda表達式炼彪。
- 代碼量比原生java少了很多吐根。
- Kotlin 被編譯成 JVM 字節(jié)碼。
- 空指針安全
- 和java完美兼容
正因為這些優(yōu)點辐马,我開始去學習和使用Kotlin并且到了欲罷不能的地步拷橘。哈哈~~~~不扯淡了直接進入今天的正題。
JDK8一書中第一章便提到了一個概念——行為參數(shù)化,說簡單點就是將一個方法傳遞給另外一個方法冗疮。舉個栗子萄唇,假設(shè)要做一個幫助農(nóng)場主理解庫存的應(yīng)用。一開始术幔,農(nóng)場主可能想有一個在所有庫存中找出所有綠色蘋果的功能另萤。但是第二天他可能會告訴你他還想要找到所有重量大于150g的蘋果。兩天后诅挑,他可能又提出找到所有綠色的并且重量大于150g的蘋果四敞。在面對這些相似的新功能時,我們都想盡可能的減少開發(fā)量揍障。
下面通過農(nóng)場應(yīng)用來看看面對不斷改變的需求如何將代碼寫的更靈活目养。先實現(xiàn)第一個功能:從一個list中過濾出所有的綠色蘋果,聽起來是不是很簡單。
版本1 : 過濾出綠色蘋果
最開始想到的解決辦法可能長下面的樣子:
public static List<Apple> filterGreenApples(List<Apple> inventory){
List<Apple> result = new ArrayList<>(); // An accumulator list for apples
for(Apple apple : inventory){
if( "green".equals(apple.getColor())){
// Select only green apples
result.add(apple);
}
}
return result;
}
上面的方法可以過濾出綠色的蘋果毒嫡,但是如果農(nóng)場主還想知道紅色的蘋果呢癌蚁?一個很幼稚的做法是將上面的方法復制一遍,命名為filterRedApples兜畸,更改一下if語句努释。但如果還想知道黃色的呢?一個好的做法是:試著抽象咬摇。
版本2 : 將顏色作為參數(shù)
可以在方法中加入顏色作為參數(shù)伐蒂,使代碼更靈活。
public static List<Apple> filterApplesByColor(List<Apple> inventory,String color) {
List<Apple> result = new ArrayList<>();
for (Apple apple: inventory){
if ( apple.getColor().equals(color) ) {
result.add(apple);
}
}
return result;
}
這樣就可以靈活的根據(jù)顏色來篩選肛鹏。這時農(nóng)場主又提出了根據(jù)重量篩選逸邦,于是參照上面根據(jù)顏色篩選的方法又新增了一個根據(jù)重量篩選的方法,如下:
public static List<Apple> filterApplesByWeight(List<Apple> inventory,int weight) {
List<Apple> result = new ArrayList<>();
for (Apple apple: inventory){
if ( apple.getWeight()>weight ) {
result.add(apple);
}
}
return result;
}
這是一個解決辦法在扰,但考慮到蘋果有許多其它特性缕减,如果針對每一特性的篩選都復制一個方法,違背了DRY(don’t repeat yourself)原則.我們可以將顏色和重量結(jié)合到一個方法芒珠,并通過一個標記來指明想要進行過濾的是顏色還是重量(這樣做其實很不好桥狡,之后會解釋)。
版本3 : 在一個方法中過濾想要過濾的屬性
下面在一個方法中根據(jù)flag值的不同過濾不同的屬性(這樣做法很不好)皱卓。
public static List<Apple> filterApples(List<Apple> inventory,String color, int weight, boolean flag){
List<Apple> result = new ArrayList<>();
for(Apple apple : inventory){
if((flag&&apple.getColor().equals(color)) ||
(!flag && apple.getWeight() > weight)){
result.add(apple);
}
}
return result;
}
上面的代碼很丑陋而且也沒有解決需求變化的問題裹芝,比如如果農(nóng)場主還想要根據(jù)大小,產(chǎn)地娜汁,形狀來篩選就不適用了嫂易。
版本4 : 根據(jù)抽象約束過濾
一個更好的解決辦法是將過濾的標準抽象出來,我們先定義一個接口作為抽象的選擇標準.
public interface ApplePredicate{
boolean test(Apple apple);
}
接下來就可以定義多個ApplePredicate接口的實現(xiàn)類來代表不同的過濾標準存炮。如下圖:
//select only heavy apple
public class AppleHeavyWeightPredicate implements ApplePredicate{
public boolean test(Apple apple){
return apple.getWeight() > 150;
}
}
//select only green apple
public class AppleGreenColorPredicate implements ApplePredicate{
public boolean test(Apple apple){
return "green".equals(apple.getColor);
}
}
上面每一個實現(xiàn)了ApplePredicate接口的類都代表了一種篩選策略炬搭。在此基礎(chǔ)上蜈漓,我們可以將篩選方法修改成下面的樣子,將ApplePredicate作為參數(shù)傳入。
public static List<Apple> filterApples(List<Apple> inventory, ApplePredicate p){
List<Apple> result = new ArrayList<>();
for(Apple apple : inventory){
if(p.test(apple)){
result.add(apple);
}
}
return result;
}
現(xiàn)在的篩選方法比第一個版本靈活多了宫盔,如果想改變篩選標準融虽,只需創(chuàng)建不同的ApplePredicate對象,并傳入filterApples方法即可灼芭。例如新增了選出紅色并且重量大于150g的蘋果的需求有额,我們可以創(chuàng)建一個實現(xiàn)ApplePredicate接口的類即可,代碼如下:
public class AppleRedAndHeavyPredicate implements ApplePredicate{
public boolean test(Apple apple){
return "red".equals(apple.getColor()) && apple.getWeight() > 150;
}
}
List<Apple> redAndHeavyApples = filter(inventory, new AppleRedAndHeavyPredicate());
但是上面的實現(xiàn)有一個缺點彼绷,就是太啰嗦了巍佑,每新增一個篩選標準都要新增一個類。下面來繼續(xù)優(yōu)化一下寄悯。
版本5 : 使用匿名類
匿名類是沒有名字的類萤衰,使用匿名類可以創(chuàng)建一個臨時的實現(xiàn)。下面的代碼展示了如何利用匿名類創(chuàng)建實現(xiàn)了ApplePredicate的對象猜旬。
List<Apple> redApples = filterApples(inventory, new ApplePredicate(){
public boolean test(Apple apple){
return "red".equals(apple.getColor());
}
});
但是盡管匿名類解決了為一個接口聲明多個實現(xiàn)類的問題脆栋,使用匿名類還不足夠好。使用匿名類代碼看起來有些笨重洒擦,可讀性差椿争,而且有一些開發(fā)者對匿名類感到困惑。下面我們使用Java 8引入的lambda表達式使代碼看起來更加簡潔一點熟嫩。
版本6 : 使用lambda表達式
我們可以使用lambda表達式簡化代碼.
List<Apple> result = filterApples(inventory,(Apple apple) -> "red".equals(apple.getColor()));
最終版 : 使用泛型秦踪,抽象列表的類型
我們可以繼續(xù)做一些抽象。目前掸茅,filterApples方法只可以過濾元素類型為Apple的List椅邓。我們可以將列表的類型抽象出來,使得我們的過濾方法變得更加通用昧狮,代碼如下:
public interface Predicate<T>{
boolean test(T t);
}
pucblic static <T> List<T> filter(List<T> list, Predicate<T> p){
List<T> result = new ArrayList<>();
for(T e: list){
if(p.test(e)){
result.add(e);
}
}
return result;
}
這樣就可以對多種類型的list進行過濾了:
List<Apple> redApples = filter(inventory, (Apple apple) -> "red".equals(apple.getColor()));
List<String> evenNumber = filter(numbers, (Integer i) -> i%2 == 0);
到此JDK8版本的篩選蘋果算是結(jié)束了希坚,感覺就一個字——累!累陵且!累!(重要的事情說三遍)
接著我們來瞅瞅Kotlin版本的
fun main(args: Array<String>) {
var appleList = arrayListOf(Apple(1, 2, "黃色"))
//根據(jù)顏色區(qū)分
val appleByColor = appleList.all { it.color == "指定的顏色" }
//根據(jù)大小區(qū)分
val appleBySize = appleList.all { it.size > 10 }
//根據(jù)重量區(qū)分
val appleByWeight = appleList.all { it.weight > 5 }
//根據(jù)重量區(qū)分 多種篩選條件自由發(fā)揮
val apple = appleList.all { it.weight > 5 && it.color == "指定的顏色" }
}
class Apple(var size: Int, var weight: Int, var color: String)
爽不爽个束?爽不爽慕购?爽不爽?(重要的事情說三遍)就問你爽不爽茬底,一句代碼搞定沪悲。Kotlin不僅僅支持集合類,同時也支持鍵值對類型的數(shù)據(jù)阱表,不說了我直接上源碼殿如,相信大家一看就明白了
......
/**
* Returns a set containing all distinct elements from both collections.
*
* The returned set preserves the element iteration order of the original collection.
* Those elements of the [other] collection that are unique are iterated in the end
* in the order of the [other] collection.
*/
public infix fun <T> Iterable<T>.union(other: Iterable<T>): Set<T> {
val set = this.toMutableSet()
set.addAll(other)
return set
}
/**
* Returns `true` if all elements match the given [predicate].
*/
public inline fun <T> Iterable<T>.all(predicate: (T) -> Boolean): Boolean {
for (element in this) if (!predicate(element)) return false
return true
}
/**
* Returns `true` if collection has at least one element.
*/
public fun <T> Iterable<T>.any(): Boolean {
for (element in this) return true
return false
}
/**
* Returns `true` if at least one element matches the given [predicate].
*/
public inline fun <T> Iterable<T>.any(predicate: (T) -> Boolean): Boolean {
for (element in this) if (predicate(element)) return true
return false
}
......
源碼是不是很簡單贡珊,根據(jù)傳入的范型T和具體的行為,簡單優(yōu)雅的實現(xiàn)了我們想要的功能涉馁。
好了我要平復下我的心情门岔,Kotlin的學習還是一個挺長的過程,小編建議大家可以盡早去嘗試使用Kotlin烤送,今天先到這里寒随,接下來我會一步一步的向大家介紹JDK8中的新特性與Kotlin之間的比較,希望大家持續(xù)關(guān)注帮坚。
下期見! 小編QQ小窩:581621024