java_集合体系之Collection框架相关抽象类接口详解、源码――02(二)

2014-11-24 07:42:57 · 作者: · 浏览: 1
public interface ListIterator
  
    extends Iterator
   
     { // Query Operations /** * 以正向遍历列表时,如果列表迭代器有多个元素,则返回 true(换句话说,如果 next 返回一个元素而不是抛出异常,则返回 true)。 */ boolean hasNext(); /** * 返回列表中的下一个元素。 */ E next(); /** * 如果以逆向遍历列表,列表迭代器有多个元素,则返回 true。 */ boolean hasPrevious(); /** * 返回列表中的前一个元素。 */ E previous(); /** * 返回对 next 的后续调用所返回元素的索引。 */ int nextIndex(); /** * 返回对 previous 的后续调用所返回元素的索引。 */ int previousIndex(); // Modification Operations /** * 从列表中移除由 next 或 previous 返回的最后一个元素(可选操作)。 */ void remove(); void set(E e); void add(E e); }
   
  


六:Set


1、接口简介:

Set、作为Collection的一个子类、与List相比、其不允许有重复元素出现、存放的元素没有索引一说、其API与Collection完全相同、只是对方法的定义不同

2、源码不再乱贴


七:AbstractSet


1、抽象类简介:

同AbstractList很相似、因Set继承与Collection接口、其也继承AbstractCollection抽象类、由于Set的特性、他不像AbstractList一样新增了有关索引的操作方法、他没有添加任何方法、仅仅是实现了equals、hashCode、removeAll(Collection c)方法的实现。注意:他不像AbstractList、内部提供了实现获取Iterator的方法、他要求继承他的子类去实现获取Iterator的方法。

2、源码

package com.chy.collection.core;

import java.util.Iterator;

/**
 * 此类提供 Set 接口的骨干实现,从而最大限度地减少了实现此接口所需的工作。
 * 此类并没有重写 AbstractCollection 类中的任何实现。它仅仅添加了 equals 和 hashCode 的实现。
 */
@SuppressWarnings("unchecked")
public abstract class AbstractSet
  
    extends AbstractCollection
   
     implements Set
    
      { protected AbstractSet() { } // Comparison and hashing public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof Set)) return false; Collection c = (Collection) o; if (c.size() != size()) return false; try { return containsAll(c); } catch (ClassCastException unused) { return false; } catch (NullPointerException unused) { return false; } } public int hashCode() { int h = 0; Iterator
     
       i = iterator(); while (i.hasNext()) { E obj = i.next(); if (obj != null) h += obj.hashCode(); } return h; } public boolean removeAll(Collection
       c) { boolean modified = false; if (size() > c.size()) { for (Iterator
       i = c.iterator(); i.hasNext(); ) modified |= remove(i.next()); } else { for (Iterator
       i = iterator(); i.hasNext(); ) { if (c.contains(i.next())) { i.remove(); modified = true; } } } return modified; } }
     
    
   
  


更多内容:java_集合体系之总体目录――00