最近一直在寫React Native相關(guān)的東西,擔(dān)心會淡忘Java的東西,所以買了本JDK8 閑來瞅瞅,沒想到卻被其中的新特性深深的吸引,幸好沒有放棄,抓緊時間拿出來和大家探討探討。
先來說說今天的標(biāo)題,java我就不再細(xì)細(xì)介紹了,畢竟是個耳熟能詳?shù)臇|西,介紹一下Kotlin吧!
Kotlin是對java的一層包裝,而他的語法更加接近于函數(shù)式編程,這些大家都可以在Kotlin官網(wǎng)中找到詳細(xì)的介紹。之所以我會使用Kotlin去寫自己的項目主要有以下幾點考慮
- 使用Kotlin可以使用lambda表達(dá)式。
- 代碼量比原生java少了很多。
- Kotlin 被編譯成 JVM 字節(jié)碼。
- 空指針安全
- 和java完美兼容
正因為這些優(yōu)點,我開始去學(xué)習(xí)和使用Kotlin并且到了欲罷不能的地步。哈哈~~~~不扯淡了直接進(jìn)入今天的正題。
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)場主還想知道紅色的蘋果呢?一個很幼稚的做法是將上面的方法復(fù)制一遍,命名為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;
}
這是一個解決辦法,但考慮到蘋果有許多其它特性,如果針對每一特性的篩選都復(fù)制一個方法,違背了DRY(don’t repeat yourself)原則.我們可以將顏色和重量結(jié)合到一個方法,并通過一個標(biāo)記來指明想要進(jìn)行過濾的是顏色還是重量(這樣做其實很不好,之后會解釋)。
版本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ù)抽象約束過濾
一個更好的解決辦法是將過濾的標(biāo)準(zhǔn)抽象出來,我們先定義一個接口作為抽象的選擇標(biāo)準(zhǔn).
public interface ApplePredicate{
boolean test(Apple apple);
}
接下來就可以定義多個ApplePredicate接口的實現(xiàn)類來代表不同的過濾標(biāo)準(zhǔ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)在的篩選方法比第一個版本靈活多了,如果想改變篩選標(biāo)準(zhǔ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)有一個缺點,就是太啰嗦了,每新增一個篩選標(biāo)準(zhǔ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表達(dá)式使代碼看起來更加簡潔一點。
版本6 : 使用lambda表達(dá)式
我們可以使用lambda表達(dá)式簡化代碼.
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進(jìn)行過濾了:
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)了我們想要的功能。
好了我要平復(fù)下我的心情,Kotlin的學(xué)習(xí)還是一個挺長的過程,小編建議大家可以盡早去嘗試使用Kotlin,今天先到這里,接下來我會一步一步的向大家介紹JDK8中的新特性與Kotlin之間的比較,希望大家持續(xù)關(guān)注。
下期見! 小編QQ小窩:581621024