Java中的集合(详解)

1.1 集合接口

1.1.1 集合的接口与实现分离

与现代的数据结构类库的常见情况一样,java集合类库也将接口(interface)与实现(implemention)分离。以队列为例进行讲解,队列接口指出可以再队列的尾部添加元素,在队列的头部删除元素,并且可以查找队列中元素的个数。一个队列的最小形式可能类似于下面这样:

interface Queue<E>
{
	void add(E element);
	E remove();
	int size();
}
而这个接口并没有说明队列是如何实现的,队列通常有两种实现方式:一种是使用循环数组;一种是使用链表。以Java类库设计者开发好的类来举例,如果我们需要一个循环数组队列,就可以使用ArrayDeque类。如果需要一个链表队列,就直接使用LinkedList类,这个类已经实现了Queue接口

1.1.2 java类库库中的集合接口和迭代器接口

在java类库中,集合类的基本接口是Collection接口。这个接口有两个基本方法:

public interface Collection<E> extends Iterable<E> {
    /**
     * Returns an iterator over the elements in this collection.  There are no
     * guarantees concerning the order in which the elements are returned
     * (unless this collection is an instance of some class that provides a
     * guarantee).
     *
     * @return an <tt>Iterator</tt> over the elements in this collection
     */
    <span style="color:#FF0000;">Iterator<E> iterator();</span>
/**
     * Ensures that this collection contains the specified element (optional
     * operation).  Returns <tt>true</tt> if this collection changed as a
     * result of the call.  (Returns <tt>false</tt> if this collection does
     * not permit duplicates and already contains the specified element.)<p>
     *
     * Collections that support this operation may place limitations on what
     * elements may be added to this collection.  In particular, some
     * collections will refuse to add <tt>null</tt> elements, and others will
     * impose restrictions on the type of elements that may be added.
     * Collection classes should clearly specify in their documentation any
     * restrictions on what elements may be added.<p>
     *
     * If a collection refuses to add a particular element for any reason
     * other than that it already contains the element, it <i>must</i> throw
     * an exception (rather than returning <tt>false</tt>).  This preserves
     * the invariant that a collection always contains the specified element
     * after this call returns.
     *
     * @param e element whose presence in this collection is to be ensured
     * @return <tt>true</tt> if this collection changed as a result of the
     *         call
     * @throws UnsupportedOperationException if the <tt>add</tt> operation
     *         is not supported by this collection
     * @throws ClassCastException if the class of the specified element
     *         prevents it from being added to this collection
     * @throws NullPointerException if the specified element is null and this
     *         collection does not permit null elements
     * @throws IllegalArgumentException if some property of the element
     *         prevents it from being added to this collection
     * @throws IllegalStateException if the element cannot be added at this
     *         time due to insertion restrictions
     */
    <span style="color:#FF0000;">boolean add(E e);</span>
 
add方法用于向集合中添加元素。如果添加元素确实改变了集合则返回true;如果集合中已经存在该元素(集合中不允许存在重复的对象)或者不允许添加该元素,那么add方法将会返回false。

iterator方法用于返回一个实现了Iterator接口的对象。可以使用这个迭代器对象依次访问集合中的元素。

1.迭代器

Iterator接口包含了4个方法(jdk8)

public interface Iterator<E> {
    boolean hasNext();
    E next();
    default void remove() {
        throw new UnsupportedOperationException("remove");
    }
    default void forEachRemaining(Consumer<? super E> action) {
        Objects.requireNonNull(action);
        while (hasNext())
            action.accept(next());
    }
}
通过反复调用next方法,可以访问集合中的每个元素。如果想要查看集合中的所有元素,就请求一个迭代器,并在hasnext方法返回true时反复调用next方法。
        Collection<String> c = ...;
	Iterator<String> iter = c.iterator();
	
	while(iter.hasnext())
	{
		dosomething with element;
	}

或者也可用for each循环:
        for(String element : c)
	{
		dosomething with element;
	}

for each 循环可以与任何实现了Iterable接口的对象一起工作,Collection接口扩展了Iterable接口。因此,对于标准类库中的任何集合都可以使用“for each”循环。其中元素被访问的顺序取决于集合类型。如果对ArrayList进行迭代,迭代器将从索引0开始,每迭代一次,索引值加1.然而,如果访问HashSet中的元素,每个元素将会按照某种随机的次序出现。虽然可以确定在迭代过程中能够遍历到集合中的所有元素,但无法预知元素被访问的次序。

Java集合类库中的迭代器与其他类库中的迭代器在概念上有着重要的区别。在Java中,查找操作与位置变更是紧密相连的 。查找一个元素的唯一方法是调用next,而在执行查找 操作的同时,迭代器的位置随之向前移动。因此,应该将Java迭代器认为是位于两个元素之间。当调用next方法时,迭代器就越过下一个元素,并返回刚刚越过元素的引用。

2.删除元素

next方法和remove方法具有相互依懒性,如果再调用remove方法之前没有调用next方法将是不合法的,会抛出一个IllegalStateException异常。如果想删除2个相邻的元素不能这样直接调用:

it.remove();
it.remove();
而应该是:
it.remove();
it.next();
it.remove();
3.泛型实用方法

......

将在下一遍博客中讲具体的集合。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值