java 集合框架
阿新 • • 發佈:2017-09-24
ins only ast 表示 class a collect class lastindex dal
基於 JDK 1.7.0_80
Iterator 是集合中的叠代器,用來遍歷這個集合
Collection 類
collection 表示一組對象,定義了一組對象最基本的操作
// 大小 int size(); //時候為空 boolean isEmpty(); //時候包含了這個對象 boolean contains(Object var1); // 這組對象的叠代器 Iterator<E> iterator(); //將這組對象轉為對象數組 Object[] toArray(); //將這組對象轉為特定類型數組<T> T[] toArray(T[] var1); //添加 對象 boolean add(E var1); // 移除對象 boolean remove(Object var1); //是否包含這組對象 boolean containsAll(Collection<?> var1); // 將這組對象添加進來 boolean addAll(Collection<? extends E> var1); // 移除這組對象 boolean removeAll(Collection<?> var1);// 移除此 collection 中未包含在指定 collection 中的所有元素。 boolean retainAll(Collection<?> var1); //移除此 collection 中的所有元素 void clear(); // boolean equals(Object var1); // int hashCode();
AbstractCollection接口
此類提供 Collection 接口的骨幹實現,以最大限度地減少了實現此接口所需的工作。 還有兩個抽象方法,具體的叠代器,具體的Collection 的大小
public abstract Iterator<E> iterator(); public abstract int size(); //這裏add 是實現的了,而不是已抽象方法的方式呢
/*
To implement an unmodifiable collection, the programmer needs only to*
extend this class and provide implementations
for the iterator and size methods
To implement a modifiable collection, the programmer must additionally override this class‘s add method
為了實現一個不可修改的集合,程序員只需要擴展這個類並提供iterator和size方法的實現。 ( iterator方法返回的叠代器必須實現hasNext和next )
要實現可修改的集合,程序員必須另外覆蓋此類的add方法(否則將拋出UnsupportedOperationException ),並且由iterator方法返回的叠代器必須另外實現其remove方法。
*/
public boolean add(E e) {
throw new UnsupportedOperationException(); }
List 接口
有序的 collection(也稱為序列)。定義了一些針對List的方法
boolean addAll(int var1, Collection<? extends E> var2); E get(int var1); E set(int var1, E var2); void add(int var1, E var2); E remove(int var1); int indexOf(Object var1); int lastIndexOf(Object var1); ListIterator<E> listIterator(); ListIterator<E> listIterator(int var1); List<E> subList(int var1, int var2)
AbstractList接口
此類提供 List
接口的骨幹實現,以最大限度地減少實現“隨機訪問”數據存儲(如數組)支持的該接口所需的工作。和AbstractCollection實現了
Collection的骨幹實現一樣
java 集合框架