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();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章