类集框架(一)--Collection接口

1.类集框架定义
jdk提供的一系列类和接口,位于java.util包中,主要用于存储和管理对象。主要分为三大类:集合、列表和映射。
集合Set:用于存储一系列对象的集合(无序且不允许元素重复)。
列表List:用来存储有顺序的一组数据的集合(有序,元素可以重复)。
映射Map:以键值对的方式进行数据存储的集合(键不可以重复,值可重复)。
最常用到的集合API如下(Map事实上并没有继承Collection这个接口,但Map也应该被当作CollectionFramework中的一份子):
这里写图片描述
2.Collection接口
  1>常用方法:

boolean add(Object o) 向集合当中加入一个对象
void clear() 删除集合当中的所有对象
boolean isEmpty() 判断集合是否为空
remove(Object o)从集合中删除一个对象的引用
int size()返回集合中元素的数目
boolean contains()如果此 collection 包含指定的元素,则返回 true

  2>源码分析
  

package java.util;

import java.util.function.Predicate;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
/**
 * @param <E> the type of elements in this collection
 *
 * @author  Josh Bloch
 * @author  Neal Gafter
 * @see     Set
 * @see     List
 * @see     Map
 * @see     SortedSet
 * @see     SortedMap
 * @see     HashSet
 * @see     TreeSet
 * @see     ArrayList
 * @see     LinkedList
 * @see     Vector
 * @see     Collections
 * @see     Arrays
 * @see     AbstractCollection
 * @since 1.2
 */
public interface Collection<E> extends Iterable<E> {
    /**
    * 返回该集合元素的数量
    * @return the number of elements in this collection.
    */
    int size();

    /**
    * 判断该集合是否含有元素,如果有返回true,否则返回false
    * @return <tt>true</tt> if this collection contains no elements
    */
    boolean isEmpty();

    /**
     * 判断集合中是否含有指定元素如果含有指定的元素Object o,返回true.
     * @param o 是待测元素
     * @return <tt>true</tt> 如果集合含有o
     * @throws ClassCastException 类型转换异常
     * @throws NullPointerException 集合为空,抛出空指针异常
     */
    boolean contains(Object o);

    /**
     * 返回一个该集合的迭代器,用以遍历该集合
     * @return an <tt>Iterator</tt> over the elements in this collection
     */
    Iterator<E> iterator();

    /**
     * 返回集合中所有元素组成的数组,数组元素的返回顺序要和迭代器访问  
     * 集合元素的返回顺序一样  
     * @return an array containing all of the elements in this collection
     */
    Object[] toArray();

    /**
     * 返回一个包含所有元素集合的数组;
     * 返回指定类型的数组,包含collection的全部元素,如果
     * collection正好可以放进参数数组里,那么返回这个参数数组的引
     * 用。否则,返回一个新开辟的数组,大小和collection相同.
     * 如果参数数组长度比collection大,那么后面的位置都赋值为null。
     * 如果iterator方法返回有序,那么此方法返回同样顺序。
     * @param <T> 包含集合的运行时数组类型
     * @param a 集合元素将要放入的数组
     * @return an array containing all of the elements in this collection
     * @throws ArrayStoreException if the runtime type of 
     * the specified arrayis not a supertype of the runtime 
     * type of every element in this collection
     * @throws NullPointerException if the specified array 
     * is null
     */
    <T> T[] toArray(T[] a);

     /**
     * 确保此 collection 包含指定的元素
     * 如果集合不允许重复元素,且集合中已经含有该元素,返回false 
     *
     * @param e 确定集合是否含有该元素
     * @return <tt>true</tt> if this collection changed as 
     * a result of the call
    */
    boolean add(E e);

    /**
     * 从此 collection 中移除指定元素的单个实例,如果集合中存在指定
     * 元素返回true。
     * @param o element to be removed from this collection, 
     * if present
     * @return <tt>true</tt> if an element was removed as a 
     * result of this call
     */
    boolean remove(Object o);

    // Bulk Operations

    /**
     * 如果此 collection 包含指定 collection 中的所有元素,
     * 则返回 true
     * @param  c collection to be checked for containment 
     * in this collection
     * @return <tt>true</tt> if this collection contains 
     * all of the elements in the specified collection
     */
    boolean containsAll(Collection<?> c);

    /**
     * 将指定 collection 中的所有元素都添加到此 collection 中
     * @param c collection containing elements to be added to this collection
     * @return <tt>true</tt> if this collection changed as 
     * a result of the call
     * @see #add(Object)
     */
    boolean addAll(Collection<? extends E> c);

    /**
     *   移除此 collection 中那些也包含在指定 collection 中的所有
     *   元素
     * @param c collection containing elements to be 
     * removed from this collection
     * @return <tt>true</tt> if this collection changed as 
     * a result of the  call       
     * @see #remove(Object)
     * @see #contains(Object)
     */
    boolean removeAll(Collection<?> c);

    /**
     * Removes all of the elements of this collection that satisfy the given
     * predicate.  Errors or runtime exceptions thrown 
     * during iteration or by the predicate are relayed to 
     * the caller.
     * @implSpec
     * The default implementation traverses all elements of 
     * the collection using
     * its {@link #iterator}.  Each matching element is 
     * removed using
     * {@link Iterator#remove()}.  If the collection's 
     * iterator does not
     * @param filter a predicate which returns {@code true} 
     * for elements to be removed
     * @return {@code true} if any elements were removed
     * @since 1.8
     */
    default boolean removeIf(Predicate<? super E> filter) {
        Objects.requireNonNull(filter);
        boolean removed = false;
        final Iterator<E> each = iterator();
        while (each.hasNext()) {
            if (filter.test(each.next())) {
                each.remove();
                removed = true;
            }
        }
        return removed;
    }

    /**
     * 仅保留此 collection 中那些也包含在指定 collection 的元素
     * @param c collection containing elements to be 
     * retained in this collection
     * @return <tt>true</tt> if this collection changed as 
     * a result of the call
     * @see #remove(Object)
     * @see #contains(Object)
     */
    boolean retainAll(Collection<?> c);

    /**
     * 移除此 collection 中的所有元素(可选操作)。
     * The collection will be empty after this method 
     * returns.
     */
    void clear();

    /**
     * 比较此 collection 与指定对象是否相等
     * @param o object to be compared for equality with 
     * this collection
     * @return <tt>true</tt> if the specified object is 
     * equal to this collection
     * @see Object#equals(Object)
     * @see Set#equals(Object)
     * @see List#equals(Object)
     */
    boolean equals(Object o);

    /**
     * 返回此 collection 的哈希码值
     * @return the hash code value for this collection
     * @see Object#hashCode()
     * @see Object#equals(Object)
     */
    int hashCode();

总结:
Collection 层次结构 中的根接口。Collection 表示一组对象,这些对象也称为 collection 的元素。一些 collection 允许有重复的元素,而另一些则不允许。一些 collection 是有序的,而另一些则是无序的。JDK 不提供此接口的任何直接 实现:它提供更具体的子接口(如 Set 和 List)实现。此接口通常用来传递 collection,并在需要最大普遍性的地方操作这些 collection。

包 (bag) 或多集合 (multiset)(可能包含重复元素的无序 collection)应该直接实现此接口。

所有通用的 Collection 实现类(通常通过它的一个子接口间接实现 Collection)应该提供两个“标准”构造方法:一个是 void(无参数)构造方法,用于创建空 collection;另一个是带有 Collection 类型单参数的构造方法,用于创建一个具有与其参数相同元素新的 collection。实际上,后者允许用户复制任何 collection,以生成所需实现类型的一个等效 collection。尽管无法强制执行此约定(因为接口不能包含构造方法),但是 Java 平台库中所有通用的 Collection 实现都遵从它。

此接口中包含的“破坏性”方法,是指可修改其所操作的 collection 的那些方法,如果此 collection 不支持该操作,则指定这些方法抛出 UnsupportedOperationException。如果是这样,那么在调用对该 collection 无效时,这些方法可能,但并不一定抛出 UnsupportedOperationException。例如,如果要添加的 collection 为空且不可修改,则对该 collection 调用 addAll(Collection) 方法时,可能但并不一定抛出异常。

一些 collection 实现对它们可能包含的元素有所限制。例如,某些实现禁止 null 元素,而某些实现则对元素的类型有限制。试图添加不合格的元素将抛出一个未经检查的异常,通常是 NullPointerException 或 ClassCastException。试图查询是否存在不合格的元素可能抛出一个异常,或者只是简单地返回 false;某些实现将表现出前一种行为,而某些实现则表现后一种。较为常见的是,试图对某个不合格的元素执行操作且该操作的完成不会导致将不合格的元素插入 collection 中,将可能抛出一个异常,也可能操作成功,这取决于实现本身。这样的异常在此接口的规范中标记为“可选”。

由每个 collection 来确定其自身的同步策略。在没有实现的强烈保证的情况下,调用由另一进程正在更改的 collection 的方法可能会出现不确定行为;这包括直接调用,将 collection 传递给可能执行调用的方法,以及使用现有迭代器检查 collection。

Collections Framework 接口中的很多方法是根据 equals 方法定义的。例如,contains(Object o) 方法的规范声明:“当且仅当此 collection 包含至少一个满足 (o==null ? e==null :o.equals(e)) 的元素 e 时,返回 true。”不 应将此规范理解为它暗指调用具有非空参数 o 的 Collection.contains 方法会导致为任意的 e 元素调用 o.equals(e) 方法。可随意对各种实现执行优化,只要避免调用 equals 即可,例如,通过首先比较两个元素的哈希码。(Object.hashCode() 规范保证哈希码不相等的两个对象不会相等)。较为常见的是,各种 Collections Framework 接口的实现可随意利用底层 Object 方法的指定行为,而不管实现程序认为它是否合适。

注:参考字JDK API 手册。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值