20200320——java中的集合 关于父接口Collection

前言
本篇博客着重介绍Collection接口下面继承List接口的子实现类。

List接口

在这里插入图片描述
List接口继承于父接口Collection
他下面的重点的子实现类 ArrayList LinkedList Vector
Vector下面还有一个Stack栈

源码分析

Iterable

public interface Iterable<T> {
    /**
     * Returns an iterator over elements of type {@code T}.
     *
     * @return an Iterator.
     */
    Iterator<T> iterator();

主要返回对象中元素的迭代器。

Collection接口

/**
 * 集合层次结构中的根接口。
 * 一个集合表示一组对象,称之为元素,有些集合允许重复元素,而其他集合不允许。
 * 一些是有序的,另一些是无序的。 
 * JDK不提供此接口的任何直接实现:它提供了更多特定子接口的实现,如Set和List
 * 
 * 所有通用的Collection实现类(通常通过其子接口间接实现Collection)应该提供两个“标准”构造函数:
 * void(无参数) 构造函数,它创建一个空集合,以及一个带有Collection类型的单个参数的构造函数,
 * 它创建一个与它的参数具有相同元素的新集合。实际上,后者的构造函数允许用户复制任何集合,生成所需实现类型的等效集合。
 * 没有办法强制执行这个约定(因为接口不能包含构造函数),但是Java平台库中的所有通用Collection实现都符合。
 *   
 * 如果这个集合不支持某个操作的话,调用这些方法可能(但不是必需)抛出 UnsupportedOperationException  
 **/
public interface Collection<E> extends Iterable<E> {

    /**
     * 尝试将对象添加到此集合的内容(可选)。
     *
     * 此方法成功完成后,可确保对象包含在集合中。
     *
     * 如果集合被修改,则返回true,如果没有更改,则返回false。
     *
     * Collection的一个实现可能缩小了接受对象的集合,但它必须在文档中指定。 如果要添加的对象不符合此限制,则抛出IllegalArgumentException。
     *
     * 如果集合尚未包含要添加的对象并添加对象失败,则此方法<i>必须</ i>抛出适当的未选中的异常。 在这种情况下,不允许返回false,因为在该方法完成后,它将违反元素将成为集合的一部分的后置条件。
     */
    public boolean add(E object);

    /**
     * 清空集合元素
     */
    public void clear();

    /**
     * 测试此集合是否包含指定的对象。 如果且仅当此Collection中的至少一个元素元素满足以下要求(object == null?elem == null:object.equals(elem))},则返回true。
     */
    public boolean contains(Object object);

    /**
     * 此集合是否包含指定集合中所有的元素
     */
    public boolean containsAll(Collection<?> collection);

    /**
     * 如果对象与此对象相同,则返回true,如果该对象与此对象不同,则返回false。
     */
    public boolean equals(Object object);

    /**
     * 返回接收器的整数哈希码。 相同的对象为此方法返回相同的值。
     */
    public int hashCode();

    /**
     * 如果此Collection不包含元素,则返回true
     */
    public boolean isEmpty();

    /**
     * 返回可用于访问此Collection所包含的对象的Iterator实例。 没有定义迭代器返回元素的顺序。 只有当Collection的实例具有定义的顺序时,才能按照该顺序返回元素。
     */
    public Iterator<E> iterator();

    /**
     * 从此集合中删除指定对象的一个实例
     */
    public boolean remove(Object object);

    /**
     * 删除指定集合(可选)中每个对象的{@code Collection}中的所有事件。 在此方法返回后,该集合中的所有元素都不能在此集合中找到。
     */
    public boolean removeAll(Collection<?> collection);

    /**
     * 从集合中删除在集合中未找到的所有对象(可选)。 在此方法返回后,此集合将只包含可以在传递给此方法的集合中找到的元素。(求交集)
     */
    public boolean retainAll(Collection<?> collection);

    /**
     *返回此集合包含多少对象的计数。
     */
    public int size();

    /**
     * 返回一个包含此集合中包含的所有元素的新数组。
     *
     * 如果实现已经排序了元素,它将以与迭代器返回的顺序相同的顺序返回元素数组。
     *
     * 返回的数组不反映集合的任何更改。 即使底层数据结构已经是一个数组,也创建一个新数组。
     */
    public Object[] toArray();

    /**
     * 返回包含此集合中包含的所有元素的数组。 如果指定的数组足够大以容纳元素,则使用指定的数组,否则将创建相同类型的数组。 如果使用指定的数组并且大于此集合,则Collection元素之后的数组元素将设置为null。
     */
    public <T> T[] toArray(T[] array);
}

Collection接口是Java语言中最基本的集合接口,在JDK中没有直接提供Collection接口的具体实现类,Collection的功能实现类主要是对它的三个更具体的子接口List、Set和Queue的具体实现类。但是在Collection接口中定义了一套通用操作的实现方法和命名规则。
List、Set、Queue接口都继承自Collection并定义了各自不同的方法。

List接口

/** 
 * List是维护其元素的排序的集合。 List中的每个元素都有一个索引。 因此,每个元素可以被其索引访问,第一个索引为零。 
 * 通常,与集合相比,List允许重复元素,其中元素必须是唯一的。
 * 有序集合(也称为<i>序列</ i>)。 该接口的用户可以精确控制每个元素插入到列表中的哪个位置。 
 * 用户可以通过整数索引(列表中的位置)访问元素,并搜索列表中的元素。
 * 
 * 列表允许重复元素,也允许null元素插入
 */  
public interface List<E> extends Collection<E> {  
    /**List作为Collection的子接口 
     * 提供了Collection接口定义的方法 
     * 这些方法在Collection源码学习中 
     * 已经分析过了,就不在说明了 
     * */  
    int size();  
    boolean isEmpty();  
    boolean contains(Object o);  
    Iterator<E> iterator();  
    Object[] toArray();  
    <T> T[] toArray(T[] a);  
    boolean add(E e);  
    boolean remove(Object o);  
    boolean containsAll(Collection<?> c);  
    boolean addAll(Collection<? extends E> c);  
    boolean addAll(int index, Collection<? extends E> c);  
    boolean removeAll(Collection<?> c);  
    boolean retainAll(Collection<?> c);  
    void clear();  
    boolean equals(Object o);  
    int hashCode();  
    /**同时List接口定义了一些自己的方法 
     * 来实现“有序”这一功能特点*/  
    /** 
     *返回列表中指定索引的元素 
     *return E 
     *throws IndexOutofBoundException(index<0||index>=size()) 
     * */  
    E get(int index);  
    /** 
     *设定某个列表索引的值  
     *throws: 
     *UnsupportedOperationException  
      ClassCastException  
      NullPointerException  
      IllegalArgumentException  
      IndexOutOfBoundsException  
     * */  
    E set(int index, E element);  
    /** 
     *在指定位置插入元素,当前元素和后续元素后移 
     *这是可选操作,类似于顺序表的插入操作 */  
    void add(int index, E element);  
    /** 
     * 删除指定位置的元素(可选操作)*/  
    E remove(int index);  
    /** 
     * 获得指定对象的最小索引位置, 
     * 没有则返回-1*/  
    int indexOf(Object o);  
    /**获得指定对象的最大索引位置 
     * 可以知道的是若集合中无给定元素的重复元素下 
     * 其和indexOf返回值是一样的*/  
    int lastIndexOf(Object o);  
    /**一种更加强大的迭代器,支持向前遍历,向后遍历 
     * 插入删除操作,此处不解释*/  
    ListIterator<E> listIterator();  
    ListIterator<E> listIterator(int index);  
    /** 
     * 返回索引fromIndex(包括)和toIndex(不包括) 
     * 之间的视图。*/  
    List<E> subList(int fromIndex, int toIndex);  
}  

List接口特点

  • 内部元素是有序
  • 元素是可以重复
  • List接口有3个常用的实现类,分别是ArrayList、LinkedList、Vector。

Set 接口探索

/**
 * Set是不允许重复元素的数据结构。
 */
public interface Set<E> extends Collection<E> {
    /**Set作为Collection的子接口 
     * 提供了Collection接口定义的方法 
     * 这些方法在Collection源码学习中 
     * 已经分析过了,就不在说明了 
     * */  
    public boolean add(E object);
    public boolean addAll(Collection<? extends E> collection);
    public void clear();
    public boolean contains(Object object);
    public boolean containsAll(Collection<?> collection);
    public boolean equals(Object object);
    public int hashCode();
    public Iterator<E> iterator();
    public boolean remove(Object object);
    public boolean removeAll(Collection<?> collection);
    public boolean retainAll(Collection<?> collection);
    public Object[] toArray();
    public <T> T[] toArray(T[] array);
    public boolean isEmpty();
    public int size();
}
Set接口特点 
* 内部元素是无序的 
* 元素是不可以重复的 
* Set接口有2个常用的实现类,HashSet、TreeSet。 
* Set基于哈希算法实现

Queue 接口探索

public interface Queue<E> extends Collection<E> {
    /**
     * 队列插入元素
     *
     * @param e the element to add
     * @return <tt>true</tt> (as specified by {@link Collection#add})
     * @throws IllegalStateException if the element cannot be added at this
     *         time due to capacity restrictions
     * @throws ClassCastException if the class of the specified element
     *         prevents it from being added to this queue
     * @throws NullPointerException if the specified element is null and
     *         this queue does not permit null elements
     * @throws IllegalArgumentException if some property of this element
     *         prevents it from being added to this queue
     */
    boolean add(E e);

    /**
     * 队列插入元素
     * 插入失败,抛出异常
     * @param e the element to add
     * @return <tt>true</tt> if the element was added to this queue, else
     *         <tt>false</tt>
     * @throws ClassCastException if the class of the specified element
     *         prevents it from being added to this queue
     * @throws NullPointerException if the specified element is null and
     *         this queue does not permit null elements
     * @throws IllegalArgumentException if some property of this element
     *         prevents it from being added to this queue
     */
    boolean offer(E e);

    /**
     * 获取队顶元素,并删除该元素
     * 空的时候,抛出异常
     * @return the head of this queue
     * @throws NoSuchElementException if this queue is empty
     */
    E remove();

    /**
     * 获取队顶元素,并删除该元素
     * 空的时候,返回null
     * @return the head of this queue, or <tt>null</tt> if this queue is empty
     */
    E poll();

    /**
     * 查看队顶元素,但是不删除该元素、
     * 空的时候,抛出异常
     * @return the head of this queue
     * @throws NoSuchElementException if this queue is empty
     */
    E element();

    /**
     * 查看队顶元素,但是不删除该元素、
     * 空的时候,返回 null
     *
     * @return the head of this queue, or <tt>null</tt> if this queue is empty
     */
    E peek();
}

Queue接口特点
先进先出的数据结构,即从容器的一端放入对象,从另一端取出,并且对象放入容器的顺序与取出的顺序是相同的。

虽然Queue接口继承Collection接口,但是Queue接口中的方法都是独立的,在创建具有Queue功能的实现时,不需要使用Collection方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值