集合

Collection

The root interface in the collection hierarchy. A collection represents a group of objects, known as its <i>elements</i>.
一些集合允许元素重复,一些则不然.一些集合是有序的,一些是无序的.JDK没有提供该接口的直接实现,所有的实现都是基于Collection的子接口进行的实现,比如Set,List等等.

Collection默认提供两个标准的构造函数:

1.没有参数的构造函数,创建一个空的集合类
2.有一个类型与基类(Collection或Map)相同的构造函数,创建一个与给定参数具有相同元素的新集合类
  • 怎么来讲,Collection就像一个封装了一个微型的数据库,也可以称之为一个容器,对外提供了增删改查的接口,同时,为我们封装好了这个容器应该具有的一些其他操作,比如获取这个容器的大小.容器是否为空等等一些所有容器类东西的通用操作.
  • 这些容器不仅限于一下:Set,List,Map,SortedSet,SortedMap,HashSet,TreeSet,ArrayList,LinkedList,Vector,Collections, Arrays, AbstractCollection.上述的容器不一定实现了Collection接口,但是他们都会很好的提供自身与Collection转化的方法,一般都会包含Collection,比如AbstractHashMap如下图.
AbstractHashMap
  • 设计理念:

Collection提供了一套小而美的API,API接口需要对程序员友好,增加新功能时,能让程序员快速上手.
Collection与Map接口不会区分该集合是否可变(mutability),是否可更改(modifiability),是否可改变大小(resizability)这些细微的差别.相反,一些操作是可选的,在实现时抛出UnsupportedOperationException即可表示集合不支持该操作。集合的实现者必须在文档中声明那些操作是不支持的。

  • 类图:

那么,一个一个来:
Set

Iterator


package java.util;

/**
集合之上的Iterator,是所有集合实现的一个接口
设计目的:抽象遍历集合的逻辑,避免向用户暴露集合内部过多的结构与实现
 */
public interface Iterator<E> {
    /**当前遍历器有下一个元素是,返回true;以免抛出异常
*/
    boolean hasNext();

    /**
     返回下一个元素;如果没有,则抛出NoSuchElementException异常
     */
    E next();

    /**
    移除当前遍历器中的最后一个元素,每次使用next()方法,只能使用一次该方法。
当前迭代器有对集合修改的动作时,该集合的遍历器的行为是不确定的。这句话有些抽象,
可以下面的example 1。
同样,该方法用了java8的特性,default-虚拟扩展方法
使用目的:在该接口中进行默认实现,提高接口的扩展性,避免在接口扩展的时候,破坏原有的实现
     */
    default void remove();
}

example 1

Iterable

/*实现该接口的object可以使用“for-each loop”表达式*/
public interface Iterable<T> {
    /*返回一个基于泛型T类型的Iterator*/
    Iterator<T> iterator();

    /**
    对每一个元素执行给出的动作。 
     * @implSpec
     * <p>The default implementation behaves as if:
     * <pre>{@code
     *     for (T t : this)
     *         action.accept(t);
     * }</pre>
     *
     * @param action The action to be performed for each element
     * @throws NullPointerException if the specified action is null
     * @since 1.8
     */
    default void forEach(Consumer<? super T> action) {
        Objects.requireNonNull(action);
        for (T t : this) {
            action.accept(t);
        }
    }

    /**
     * Creates a {@link Spliterator} over the elements described by this
     * {@code Iterable}.
     *
     * @implSpec
     * The default implementation creates an
     * <em><a href="Spliterator.html#binding">early-binding</a></em>
     * spliterator from the iterable's {@code Iterator}.  The spliterator
     * inherits the <em>fail-fast</em> properties of the iterable's iterator.
     *
     * @implNote
     * The default implementation should usually be overridden.  The
     * spliterator returned by the default implementation has poor splitting
     * capabilities, is unsized, and does not report any spliterator
     * characteristics. Implementing classes can nearly always provide a
     * better implementation.
     *
     * @return a {@code Spliterator} over the elements described by this
     * {@code Iterable}.
     * @since 1.8
     */
    default Spliterator<T> spliterator() {
        return Spliterators.spliteratorUnknownSize(iterator(), 0);
    }
}

ListIterator

package java.util;

/**
Iterator接口的扩展,为list实现的双向遍历,并且能获取到当前遍历的位置,
 */
public interface ListIterator<E> extends Iterator<E> {
    boolean hasNext();
    E next();
    /**
是否有上一个参数。详细参考一下链接: http://sungang-1120.iteye.com/blog/1747384
     */
    boolean hasPrevious();
   /*返回上一个元素*/
    E previous();
    /**返回下一个元素的坐标,直到返回list.size
     */
    int nextIndex()
    /**
    返回上一个坐标,直到返回-1
     */
    int previousIndex();
    /**
     移除刚刚指向的元素(next()或者previous()操作之后的指向的那个元素)
     */
    void remove()
    /**
    元素插入到list末尾
     */
    void set(E e);
    /**
    插入元素到列表头部,详细参考:http://www.cnblogs.com/baiqiantao/p/5449434.html
     */
    void add(E e);
}

Vector

构造方法:
 public Vector(int initialCapacity, int capacityIncrement) {
        super();
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        this.elementData = new Object[initialCapacity];
        this.capacityIncrement = capacityIncrement;
    }
    public Vector(int initialCapacity) {
        this(initialCapacity, 0);
    }
    public Vector() {
        this(10);
    }
public Vector(Collection<? extends E> c) {
        elementData = c.toArray();
        elementCount = elementData.length;
        // c.toArray might (incorrectly) not return Object[] (see 6260652)
        if (elementData.getClass() != Object[].class)
            elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
    }
/×将vetcor中的元素拷贝到anArray数组中,同步方法×/
    public synchronized void copyInto(Object[] anArray) {
        System.arraycopy(elementData, 0, anArray, 0, elementCount);
    }
/×将当前数组的容量改为当前数组数据数量的大小×/`
public synchronized void trimToSize() {
        modCount++;
        int oldCapacity = elementData.length;
        if (elementCount < oldCapacity) {
            elementData = Arrays.copyOf(elementData, elementCount);
        }
    }

AbstractList

/*在对一个集合对象进行跌代操作的同时,并不限制对集合对象的元素进行操 作,这些操作包括一些可能引起
跌代错误的add()或remove()等危险操作。在AbstractList中,使用了一个简单的机制来规避这些风险。 这就是
modCount(存在于AbstractList)和expectedModCount(存在于该类实现的接口iterator中)的作用所在。详
细参考:http://liuzhaomin.iteye.com/blog/1038289*/
protected transient int modCount = 0;
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Collection ├List │├LinkedList │├ArrayList │└Vector │└Stac...
    AndyZX阅读 1,016评论 0赞 1
  • 集合类简介 为什么出现集合类?面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,就要对对象进...
    阿敏其人阅读 1,613评论 0赞 7
  • 概述 Java集合框架由Java类库的一系列接口、抽象类以及具体实现类组成。我们这里所说的集合就是把一组对象组织到...
    absfree阅读 1,472评论 0赞 10
  • 1.Java集合框架是什么?说出一些集合框架的优点? 每种编程语言中都有集合,最初的Java版本包含几种集合类:V...
    joshul阅读 435评论 0赞 2
  • 1.Java集合框架是什么?说出一些集合框架的优点?每种编程语言中都有集合,最初的Java版本包含几种集合类:Ve...
    yjaal阅读 1,295评论 1赞 10

友情链接更多精彩内容