Android 比Java 特有類、接口筆記

1.pair

pair 英[pe?] 美[p?r]
n. 一對(duì),一雙,一副
vt. 把…組成一對(duì)

源碼:

package android.util;

import java.util.Objects;

/**
 * Container to ease passing around a tuple of two objects. This object provides a sensible
 * implementation of equals(), returning true if equals() is true on each of the contained  
 * objects.
 */
public class Pair<F, S> {
    public final F first;
    public final S second;

    /**
     * Constructor for a Pair. 構(gòu)造函數(shù)
     *
     * @param first the first object in the Pair   Pair中的第一個(gè)對(duì)象
     * @param second the second object in the pair  Pair中的第二個(gè)對(duì)象
     */
    public Pair(F first, S second) {
        this.first = first;
        this.second = second;
    }

    /**
     * Checks the two objects for equality by delegating to their respective
     * {@link Object#equals(Object)} methods.
     *
     * @param o the {@link Pair} to which this one is to be checked for equality
     * @return true if the underlying objects of the Pair are both considered
     *         equal
     */
    @Override
    public boolean equals(Object o) {
        if (!(o instanceof Pair)) {
            return false;
        }
        Pair<?, ?> p = (Pair<?, ?>) o;
        return Objects.equals(p.first, first) && Objects.equals(p.second, second);
    }

    /**
     * Compute a hash code using the hash codes of the underlying objects
     *
     * @return a hashcode of the Pair
     */
    @Override
    public int hashCode() {
        return (first == null ? 0 : first.hashCode()) ^ (second == null ? 0 : second.hashCode());
    }

    @Override
    public String toString() {
        return "Pair{" + String.valueOf(first) + " " + String.valueOf(second) + "}";
    }

    /**
     * Convenience method for creating an appropriately typed pair. 一個(gè)便捷的創(chuàng)建適當(dāng)?shù)?pair
     * @param a the first object in the Pair  Pair中的第一個(gè)對(duì)象
     * @param b the second object in the pair  Pair中的第二個(gè)對(duì)象
     * @return a Pair that is templatized with the types of a and b 一個(gè)Pair 模版 使用a和b 的類型
     */
    public static <A, B> Pair <A, B> create(A a, B b) {
        return new Pair<A, B>(a, b);
    }
}

源碼簡(jiǎn)單明了,初始化有兩種方式:

Pair p1 = new Pair(18,"張三"); // 通過 構(gòu)造函數(shù) 實(shí)例化對(duì)象
Pair p2 = Pair.create(20, "李四");//通過 create方法以泛型的方式 實(shí)例化對(duì)象

取值用的時(shí)候 ,因?yàn)槭?定義為public

public final F first;
public final S second;

所以是可以用int a= p1.first;
Pair的 equals 是值比較,而不是地址比較,當(dāng)且僅當(dāng)元素值一致時(shí)返回true
上邊是util包下的,導(dǎo)入的時(shí)候有兩個(gè)一個(gè)是v4包下的:


image.png

其中不同的是比較部分,以下是v4包的源碼不同部分:

 @Override
public boolean equals(Object o) {
    if (!(o instanceof Pair)) {
        return false;
    }
    Pair<?, ?> p = (Pair<?, ?>) o;
    return objectsEqual(p.first, first) && objectsEqual(p.second, second);
}

private static boolean objectsEqual(Object a, Object b) {
    return a == b || (a != null && a.equals(b));
}

查閱后的解釋是:v4采用自己寫的一個(gè)方法,而常規(guī)Pair則使用Objects類的equals方法,而Objects的equals方法實(shí)際上內(nèi)部就是v4的objectsEqual使用的邏輯。
Objects這個(gè)類是Java7才有的類,而Android是從4.4KitKat開始支持JDK7編譯,因此為了保證4.4之前的版本不會(huì)有空指針問題(4.4前的常規(guī)Pair),在v4中加入了一個(gè)不依賴JDK7的獨(dú)立類。

場(chǎng)景:既需要已鍵值的方式存儲(chǔ)數(shù)據(jù)列表,還需要在輸出的時(shí)候保持順序。HashMap滿足前者,ArrayList則滿足后者,再不打算去多做修改且數(shù)據(jù)類型相對(duì)簡(jiǎn)單時(shí),可以選擇Pair(搭配ArrayList)。

2.Parcelable接口

“它是基于內(nèi)存的序列化和反序列化,而且不會(huì)像Serializable一樣使用磁盤并通過反射來進(jìn)行序列化和反序列化”
實(shí)現(xiàn)Parcelable接口比實(shí)現(xiàn)Serializable復(fù)雜了很多,但效率更高,是Android推薦的序列化方式。

/**
 * 實(shí)現(xiàn)Parcelable
 */
public class Teacher implements Parcelable {
private String name;
private String sex;


@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(this.name);
    dest.writeString(this.sex);
}

public Teacher() {
}

public Teacher(String name, String sex) {
    this.name = name;
    this.sex = sex;
}

protected Teacher(Parcel in) {
    this.name = in.readString();
    this.sex = in.readString();
}

public static final Parcelable.Creator<Teacher> CREATOR = new Parcelable.Creator<Teacher>() {
    @Override
    public Teacher createFromParcel(Parcel source) {
        return new Teacher(source);
    }

    @Override
    public Teacher[] newArray(int size) {
        return new Teacher[size];
    }
};

@Override
public String toString() {
    return "Teacher{" +
            "name='" + name + '\'' +
            ", sex='" + sex + '\'' +
            '}';
}
}

用AndroidStudio的的可以安裝一個(gè)插件,可以自動(dòng)的將實(shí)現(xiàn)Parcelable接口。(插件的名字叫Android parcelable code generator)

為什么效率高,待 研究補(bǔ)充 2017年11月27日 17:17:45

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容