Java新特性与Kotlin之间的比较

最近一直在写React Native相关的东西,担心会淡忘Java的东西,所以买了本JDK8 闲来瞅瞅,没想到却被其中的新特性深深的吸引,幸好没有放弃,抓紧时间拿出来和大家探讨探讨。
先来说说今天的标题,java我就不再细细介绍了,毕竟是个耳熟能详的东西,介绍一下Kotlin吧!

Kotlin是对java的一层包装,而他的语法更加接近于函数式编程,这些大家都可以在Kotlin官网中找到详细的介绍。之所以我会使用Kotlin去写自己的项目主要有以下几点考虑

  • 使用Kotlin可以使用lambda表达式。
  • 代码量比原生java少了很多。
  • Kotlin 被编译成 JVM 字节码。
  • 空指针安全
  • 和java完美兼容

正因为这些优点,我开始去学习和使用Kotlin并且到了欲罢不能的地步。哈哈~~~~不扯淡了直接进入今天的正题。

JDK8一书中第一章便提到了一个概念——行为参数化,说简单点就是将一个方法传递给另外一个方法。举个栗子,假设要做一个帮助农场主理解库存的应用。一开始,农场主可能想有一个在所有库存中找出所有绿色苹果的功能。但是第二天他可能会告诉你他还想要找到所有重量大于150g的苹果。两天后,他可能又提出找到所有绿色的并且重量大于150g的苹果。在面对这些相似的新功能时,我们都想尽可能的减少开发量。

下面通过农场应用来看看面对不断改变的需求如何将代码写的更灵活。先实现第一个功能:从一个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;
    }

上面的方法可以过滤出绿色的苹果,但是如果农场主还想知道红色的苹果呢?一个很幼稚的做法是将上面的方法复制一遍,命名为filterRedApples,更改一下if语句。但如果还想知道黄色的呢?一个好的做法是:试着抽象。

版本2 : 将颜色作为参数
可以在方法中加入颜色作为参数,使代码更灵活。

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

这样就可以灵活的根据颜色来筛选。这时农场主又提出了根据重量筛选,于是参照上面根据颜色筛选的方法又新增了一个根据重量筛选的方法,如下:

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)原则.我们可以将颜色和重量结合到一个方法,并通过一个标记来指明想要进行过滤的是颜色还是重量(这样做其实很不好,之后会解释)。

版本3 : 在一个方法中过滤想要过滤的属性
下面在一个方法中根据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;
    }

上面的代码很丑陋而且也没有解决需求变化的问题,比如如果农场主还想要根据大小,产地,形状来筛选就不适用了。

版本4 : 根据抽象约束过滤
一个更好的解决办法是将过滤的标准抽象出来,我们先定义一个接口作为抽象的选择标准.

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

接下来就可以定义多个ApplePredicate接口的实现类来代表不同的过滤标准。如下图:

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

上面每一个实现了ApplePredicate接口的类都代表了一种筛选策略。在此基础上,我们可以将筛选方法修改成下面的样子,将ApplePredicate作为参数传入。

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

现在的筛选方法比第一个版本灵活多了,如果想改变筛选标准,只需创建不同的ApplePredicate对象,并传入filterApples方法即可。例如新增了选出红色并且重量大于150g的苹果的需求,我们可以创建一个实现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());

但是上面的实现有一个缺点,就是太啰嗦了,每新增一个筛选标准都要新增一个类。下面来继续优化一下。

版本5 : 使用匿名类
匿名类是没有名字的类,使用匿名类可以创建一个临时的实现。下面的代码展示了如何利用匿名类创建实现了ApplePredicate的对象。

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

但是尽管匿名类解决了为一个接口声明多个实现类的问题,使用匿名类还不足够好。使用匿名类代码看起来有些笨重,可读性差,而且有一些开发者对匿名类感到困惑。下面我们使用Java 8引入的lambda表达式使代码看起来更加简洁一点。

版本6 : 使用lambda表达式
我们可以使用lambda表达式简化代码.

List<Apple> result = filterApples(inventory,(Apple apple) -> "red".equals(apple.getColor()));

最终版 : 使用泛型,抽象列表的类型
我们可以继续做一些抽象。目前,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版本的筛选苹果算是结束了,感觉就一个字——累!累!累!(重要的事情说三遍)
接着我们来瞅瞅Kotlin版本的

fun main(args: Array<String>) {
    var appleList = arrayListOf(Apple(1, 2, "黄色"))
    //根据颜色区分
    val appleByColor = appleList.all { it.color == "指定的颜色" }

    //根据大小区分
    val appleBySize = appleList.all { it.size > 10 }

    //根据重量区分
    val appleByWeight = appleList.all { it.weight > 5 }

    //根据重量区分 多种筛选条件自由发挥
    val apple = appleList.all { it.weight > 5 &&  it.color == "指定的颜色" }
}
class Apple(var size: Int, var weight: Int, var color: String)

爽不爽?爽不爽?爽不爽?(重要的事情说三遍)就问你爽不爽,一句代码搞定。Kotlin不仅仅支持集合类,同时也支持键值对类型的数据,不说了我直接上源码,相信大家一看就明白了

......
/**
 * 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
}
......

源码是不是很简单,根据传入的范型T和具体的行为,简单优雅的实现了我们想要的功能。

好了我要平复下我的心情,Kotlin的学习还是一个挺长的过程,小编建议大家可以尽早去尝试使用Kotlin,今天先到这里,接下来我会一步一步的向大家介绍JDK8中的新特性与Kotlin之间的比较,希望大家持续关注。

下期见! 小编QQ小窝:581621024

    原文作者:ShunQ_Wang
    原文地址: https://www.jianshu.com/p/6783d3fe2c19
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞