Android 比Java 特有類、接口筆記

1.pair

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

源碼:

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. 構造函數
     *
     * @param first the first object in the Pair   Pair中的第一個對象
     * @param second the second object in the pair  Pair中的第二個對象
     */
    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. 一個便捷的創建適當的 pair
     * @param a the first object in the Pair  Pair中的第一個對象
     * @param b the second object in the pair  Pair中的第二個對象
     * @return a Pair that is templatized with the types of a and b 一個Pair 模版 使用a和b 的類型
     */
    public static <A, B> Pair <A, B> create(A a, B b) {
        return new Pair<A, B>(a, b);
    }
}

源碼簡單明了,初始化有兩種方式:

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

取值用的時候 ,因為是 定義為public

public final F first;
public final S second;

所以是可以用int a= p1.first;
Pair的 equals 是值比較,而不是地址比較,當且僅當元素值一致時返回true
上邊是util包下的,導入的時候有兩個一個是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采用自己寫的一個方法,而常規Pair則使用Objects類的equals方法,而Objects的equals方法實際上內部就是v4的objectsEqual使用的邏輯。
Objects這個類是Java7才有的類,而Android是從4.4KitKat開始支持JDK7編譯,因此為了保證4.4之前的版本不會有空指針問題(4.4前的常規Pair),在v4中加入了一個不依賴JDK7的獨立類。

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

2.Parcelable接口

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

/**
 * 實現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的的可以安裝一個插件,可以自動的將實現Parcelable接口。(插件的名字叫Android parcelable code generator)

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

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容