Java新特性與Kotlin之間的比較

最近一直在寫React Native相關(guān)的東西,擔(dān)心會(huì)淡忘Java的東西,所以買了本JDK8 閑來瞅瞅,沒想到卻被其中的新特性深深的吸引,幸好沒有放棄,抓緊時(shí)間拿出來和大家探討探討。
先來說說今天的標(biāo)題,java我就不再細(xì)細(xì)介紹了,畢竟是個(gè)耳熟能詳?shù)臇|西,介紹一下Kotlin吧!

Kotlin是對(duì)java的一層包裝,而他的語(yǔ)法更加接近于函數(shù)式編程,這些大家都可以在Kotlin官網(wǎng)中找到詳細(xì)的介紹。之所以我會(huì)使用Kotlin去寫自己的項(xiàng)目主要有以下幾點(diǎn)考慮

  • 使用Kotlin可以使用lambda表達(dá)式。
  • 代碼量比原生java少了很多。
  • Kotlin 被編譯成 JVM 字節(jié)碼。
  • 空指針安全
  • 和java完美兼容

正因?yàn)檫@些優(yōu)點(diǎn),我開始去學(xué)習(xí)和使用Kotlin并且到了欲罷不能的地步。哈哈~~~~不扯淡了直接進(jìn)入今天的正題。

JDK8一書中第一章便提到了一個(gè)概念——行為參數(shù)化,說簡(jiǎn)單點(diǎn)就是將一個(gè)方法傳遞給另外一個(gè)方法。舉個(gè)栗子,假設(shè)要做一個(gè)幫助農(nóng)場(chǎng)主理解庫(kù)存的應(yīng)用。一開始,農(nóng)場(chǎng)主可能想有一個(gè)在所有庫(kù)存中找出所有綠色蘋果的功能。但是第二天他可能會(huì)告訴你他還想要找到所有重量大于150g的蘋果。兩天后,他可能又提出找到所有綠色的并且重量大于150g的蘋果。在面對(duì)這些相似的新功能時(shí),我們都想盡可能的減少開發(fā)量。

下面通過農(nóng)場(chǎng)應(yīng)用來看看面對(duì)不斷改變的需求如何將代碼寫的更靈活。先實(shí)現(xiàn)第一個(gè)功能:從一個(gè)list中過濾出所有的綠色蘋果,聽起來是不是很簡(jiǎn)單。

版本1 : 過濾出綠色蘋果
最開始想到的解決辦法可能長(zhǎng)下面的樣子:

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)場(chǎng)主還想知道紅色的蘋果呢?一個(gè)很幼稚的做法是將上面的方法復(fù)制一遍,命名為filterRedApples,更改一下if語(yǔ)句。但如果還想知道黃色的呢?一個(gè)好的做法是:試著抽象。

版本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ù)顏色來篩選。這時(shí)農(nóng)場(chǎng)主又提出了根據(jù)重量篩選,于是參照上面根據(jù)顏色篩選的方法又新增了一個(gè)根據(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;
}

這是一個(gè)解決辦法,但考慮到蘋果有許多其它特性,如果針對(duì)每一特性的篩選都復(fù)制一個(gè)方法,違背了DRY(don’t repeat yourself)原則.我們可以將顏色和重量結(jié)合到一個(gè)方法,并通過一個(gè)標(biāo)記來指明想要進(jìn)行過濾的是顏色還是重量(這樣做其實(shí)很不好,之后會(huì)解釋)。

版本3 : 在一個(gè)方法中過濾想要過濾的屬性
下面在一個(gè)方法中根據(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)場(chǎng)主還想要根據(jù)大小,產(chǎn)地,形狀來篩選就不適用了。

版本4 : 根據(jù)抽象約束過濾
一個(gè)更好的解決辦法是將過濾的標(biāo)準(zhǔn)抽象出來,我們先定義一個(gè)接口作為抽象的選擇標(biāo)準(zhǔn).

public interface ApplePredicate{
    boolean test(Apple apple);
}

接下來就可以定義多個(gè)ApplePredicate接口的實(shí)現(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);
    }
}

上面每一個(gè)實(shí)現(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)在的篩選方法比第一個(gè)版本靈活多了,如果想改變篩選標(biāo)準(zhǔn),只需創(chuàng)建不同的ApplePredicate對(duì)象,并傳入filterApples方法即可。例如新增了選出紅色并且重量大于150g的蘋果的需求,我們可以創(chuàng)建一個(gè)實(shí)現(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());

但是上面的實(shí)現(xiàn)有一個(gè)缺點(diǎn),就是太啰嗦了,每新增一個(gè)篩選標(biāo)準(zhǔn)都要新增一個(gè)類。下面來繼續(xù)優(yōu)化一下。

版本5 : 使用匿名類
匿名類是沒有名字的類,使用匿名類可以創(chuàng)建一個(gè)臨時(shí)的實(shí)現(xiàn)。下面的代碼展示了如何利用匿名類創(chuàng)建實(shí)現(xiàn)了ApplePredicate的對(duì)象。

List<Apple> redApples = filterApples(inventory, new ApplePredicate(){
    public boolean test(Apple apple){
        return "red".equals(apple.getColor());
    }
});

但是盡管匿名類解決了為一個(gè)接口聲明多個(gè)實(shí)現(xiàn)類的問題,使用匿名類還不足夠好。使用匿名類代碼看起來有些笨重,可讀性差,而且有一些開發(fā)者對(duì)匿名類感到困惑。下面我們使用Java 8引入的lambda表達(dá)式使代碼看起來更加簡(jiǎn)潔一點(diǎn)。

版本6 : 使用lambda表達(dá)式
我們可以使用lambda表達(dá)式簡(jiǎn)化代碼.

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

這樣就可以對(duì)多種類型的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é)束了,感覺就一個(gè)字——累!累!累!(重要的事情說三遍)
接著我們來瞅瞅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í)也支持鍵值對(duì)類型的數(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
}
......

源碼是不是很簡(jiǎn)單,根據(jù)傳入的范型T和具體的行為,簡(jiǎn)單優(yōu)雅的實(shí)現(xiàn)了我們想要的功能。

好了我要平復(fù)下我的心情,Kotlin的學(xué)習(xí)還是一個(gè)挺長(zhǎng)的過程,小編建議大家可以盡早去嘗試使用Kotlin,今天先到這里,接下來我會(huì)一步一步的向大家介紹JDK8中的新特性與Kotlin之間的比較,希望大家持續(xù)關(guān)注。

下期見! 小編QQ小窩:581621024

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容