这几天项目不忙,之前就一直想着总结一下集合框架,对集合这块有一个大体了解。
下面先来看一下集合框架:
编程要针对接口编程,我们再阅读源码的时候,看懂里面的接口,对代码就可以有了一个较为清晰的了解
提到集合,必不可少的操作就是遍历和访问的操作,其余就是增删改查操作,所以提到容器,必不可少的就是迭代器。迭代器为各种容器提供公共的操作接口。
先查看一下迭代器接口的方法:
public interface Iterator<E> {
/**
* Returns true if there is at least one more element, false otherwise.
* @see #next
*/
public boolean hasNext();
/**
* Returns the next object and advances the iterator.
*
* @return the next object.
* @throws NoSuchElementException
* if there are no more elements.
* @see #hasNext
*/
public E next();
/**
* Removes the last object returned by {@code next} from the collection.
* This method can only be called once between each call to {@code next}.
*
* @throws UnsupportedOperationException
* if removing is not supported by the collection being
* iterated.
* @throws IllegalStateException
* if {@code next} has not been called, or {@code remove} has
* already been called after the last call to {@code next}.
*/
public void remove();
}
可以看出有遍历和删除操作,当然我们可以进行扩展,我们经常用到的
ListIterator就扩展了Iterator ,看一下源码
public interface ListIterator<E> extends Iterator<E> {
/**
* Inserts the specified object into the list between {@code next} and
* {@code previous}. The object inserted will be the previous object.
*
* @param object
* the object to insert.
* @throws UnsupportedOperationException
* if adding is not supported by the list being iterated.
* @throws ClassCastException
* if the class of the object is inappropriate for the list.
* @throws IllegalArgumentException
* if the object cannot be added to the list.
*/
void add(E object);
/**
* Returns whether there are more elements to iterate.
*
* @return {@code true} if there are more elements, {@code false} otherwise.
* @see #next
*/
public boolean hasNext();
/**
* Returns whether there are previous elements to iterate.
*
* @return {@code true} if there are previous elements, {@code false}
* otherwise.
* @see #previous
*/
public boolean hasPrevious();
/**
* Returns the next object in the iteration.
*
* @return the next object.
* @throws NoSuchElementException
* if there are no more elements.
* @see #hasNext
*/
public E next();
/**
* Returns the index of the next object in the iteration.
*
* @return the index of the next object, or the size of the list if the
* iterator is at the end.
* @throws NoSuchElementException
* if there are no more elements.
* @see #next
*/
public int nextIndex();
/**
* Returns the previous object in the iteration.
*
* @return the previous object.
* @throws NoSuchElementException
* if there are no previous elements.
* @see #hasPrevious
*/
public E previous();
/**
* Returns the index of the previous object in the iteration.
*
* @return the index of the previous object, or -1 if the iterator is at the
* beginning.
* @throws NoSuchElementException
* if there are no previous elements.
* @see #previous
*/
public int previousIndex();
/**
* Removes the last object returned by {@code next} or {@code previous} from
* the list.
*
* @throws UnsupportedOperationException
* if removing is not supported by the list being iterated.
* @throws IllegalStateException
* if {@code next} or {@code previous} have not been called, or
* {@code remove} or {@code add} have already been called after
* the last call to {@code next} or {@code previous}.
*/
public void remove();
/**
* Replaces the last object returned by {@code next} or {@code previous}
* with the specified object.
*
* @param object
* the object to set.
* @throws UnsupportedOperationException
* if setting is not supported by the list being iterated
* @throws ClassCastException
* if the class of the object is inappropriate for the list.
* @throws IllegalArgumentException
* if the object cannot be added to the list.
* @throws IllegalStateException
* if {@code next} or {@code previous} have not been called, or
* {@code remove} or {@code add} have already been called after
* the last call to {@code next} or {@code previous}.
*/
void set(E object);
}
多了几个方法,扩展的了previous 等方法。
上面介绍了一下迭代器,下面我们开始介绍框架:
从上面UML图中,包括了主要的接口,我们可以看出,主要包含的操作就是增删改查,其中Queue是先进先出(FIFO)的队列,而Deque则是双端队列,而增删改查又涉及到遍历。
下篇文章主要讲这些接口的具体实现,先从list开始