Thinking in java 讀書筆記(八.1:持有對象以及collection源碼閱讀)

其實thinking in java 這章大略的介紹了泛型(runtime type)以及collection的一些API。所以這裏是打算直接閱讀collection源碼。接下來寫寫閱讀源碼的感想。
這裏看的是jdk8
一、持有對象
泛型:
在SE1.5前沒有泛型,很多容器存儲對象的時候(比如ArrayList)直接作爲Object存儲,取出的時候再強制轉換。
二、collection接口
1、在java8之前所有的接口都是默認public abstract,但是java8開始接口可以使用default 和static來修飾方法,並且這兩個關鍵字修飾的方法是可以在接口實現的,好處是,往接口新增一個Default 方法,而不破壞現有的實現架構。而這裏collection中有四個方法是default修飾的

default boolean removeIf();
default Spliterator<E> spliterator();
default Stream<E> stream();
default Stream<E> parallelStream();

這裏四個方法都是java8的特性,暫且略過。
2、hashcode()
其實這裏是個坑= =,有被面試官問爲什麼會出現hash衝突。真的是一臉懵逼,hash算法我並不知道,當時只是粗略的知道hashcode是和對象內存有關。
然後這裏看到了需要重寫hashcode,就在Object類看了一眼。但是Object的hashcode方法是native的。native關鍵字就是說具體實現並不在java中,而是在c++中實現。具體情況參照以下鏈接hashcode的實現。由於內存回收,hashcode並不是根據內存地址算出來的!那hashcode就真的失去了唯一性,所以在容器持有的數量過於巨大的時候hashcode會重複也就不奇怪了。
3、迭代器模式和foreach
首先,實現迭代器的class都是可以使用foreach來遍歷的。
然後這裏之前沒有注意,就是foreach是不會改變容器本身的,但是Iterator是可以對容器本身進行修改的。比如

 StringBuilder[] stringBuilders = new StringBuilder[10];
        for (StringBuilder stringBuilder:stringBuilders){
            stringBuilder = new StringBuilder("11");
        }
        System.out.println(stringBuilders[0]);
       // 這裏控制檯顯示stringbuilder[0] 依然是null;
        List list = new ArrayList();
       for (int i=0;i<10;i++){
           list.add(i);
       }
       Iterator iterator = list.iterator();

        for (int i=0;i<5;i++){
            iterator.next();
            iterator.remove();
        }
        System.out.println(list);
        //這裏最後控制檯顯示確實是將前五個元素消除了。

4:貼一下抄的collection接口
emmm其實一般自己用list的時候都只用到了collection的接口方法,ArrayList本身有帶了很多的初始容量設定等根據實際情況提高性能的方法。

    /**
     * return the element number of this collection
     */
    int size();

    /**
     * return if this collection is empty
     * */
    boolean isEmpty();

    /**
     * if this collection contains the specified element
     * */
    boolean contains(Object o);

    /**
     * return an iterator over the elements in this collection
     * */
    Iterator<E> iterator();

    /**
     * return an Array contains all of the element in this collection
     * */
    Object[] toArray();

    /**
     * return an Array contains all of the element in this collection;
     * runtime type of the return array is specified Array;
     * */
    <T> T[] toArray(T[] a);


    //Modification Operations

    //這裏很奇怪,爲什麼remove用的Object,而add用的泛型

    /**
     * add an element in the last of array
     * return true if the collection changed as a result of the call
     * */
    boolean add(E e);

    /**
     * remove an element in the last of array;
     * return true if the an element was removed as a result of this call
     * */
    boolean remove(Object o);


    //Bulk Operations

    /**
     * return true if this collection contains all of the elements in the specified collection
     * */
    boolean containsAll(MyCollection<?> collection);

    /**
     * add all of the elements in specified collection to this collection
     * return true if collection changed as result of this call
     * */
    boolean addAll(MyCollection<? extends E> collection);

    /**
     * remove all of the collections's element that are also contains in specified collection
     * return true if any element be removed
     * */
    boolean removeAll(MyCollection<?> collection);

    /**
     * clear this collection
     * */
    void clear();


    //Comparison and hashing


    boolean equals(Object o);

    int hashCode();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章