java collections读书笔记(8)collection框架总览(1)
2013-06-17 21:55 很大很老实 阅读(386) 评论(0) 编辑 收藏 举报上述是接口的继承关系。
我们可以这么理解collection接口,就是一组数据,每个数据都称之为element(也就是元素);这些element可以是重复的,也可以不是重复的;可是有序的,也可以是无序的。
list接口可以理解为collection接口的一个特例,里面的元素是有序的,可是,依然可能是有重复的;但是因为是有序的,重复也就无关大雅。
而Set接口,就是没有重复的数据的collection,如果还需要排序,则可以考虑SortedSet接口。
Map接口,完全可以理解为,用key-value替代了单个的element。因此,有自己的接口体系。
一般情况下,java不会直接从collection继承,而是继承自其子接口,比如set,list等。
下面我们看看源代码:
public interface Collection<E> extends Iterable<E>
继承自Iterable接口,而Interable接口,完全就是collection框架用来替代enumation的。
int size();元素个数;
boolean isEmpty():没有元素,则返回值为true。
boolean contains(Object o);判断是否包含某个特定的object。
Iterator<E> iterator();
Object[] toArray();把所有的元素换成一个数组,哈。
boolean add(E e);
* @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
boolean remove(Object o);
* @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
* @throws ClassCastException if the type of the specified element
* is incompatible with this collection
* (<a href="#optional-restrictions">optional</a>)
* @throws NullPointerException if the specified element is null and this
* collection does not permit null elements
* (<a href="#optional-restrictions">optional</a>)
* @throws UnsupportedOperationException if the <tt>remove</tt> operation
* is not supported by this collection
boolean containsAll(Collection<?> c);
* @return <tt>true</tt> if this collection contains all of the elements
* in the specified collection
* @throws ClassCastException if the types of one or more elements
* in the specified collection are incompatible with this
* collection
* (<a href="#optional-restrictions">optional</a>)
* @throws NullPointerException if the specified collection contains one
* or more null elements and this collection does not permit null
* elements
* (<a href="#optional-restrictions">optional</a>),
* or if the specified collection is null.
* @see #contains(Object)
boolean addAll(Collection<? extends E> c);
void clear();
boolean retainAll(Collection<?> c);
boolean removeAll(Collection<?> c);
boolean equals(Object o);
int hashCode();
下面,来看一个图:
对于这个图的使用,首先,我们在最左面一列,找到合适的接口,然后,往左找,去找合适的类。
在学习前,我们首先明白以下内容:
1)所有的接口实现都是unsynchronized(http://www.cnblogs.com/devinzhang/archive/2011/12/14/2287675.html)
2)实行Iterator的fast-fail特性(http://www.cnblogs.com/xinglongbing/archive/2012/04/04/2432247.html)
3)允许拥有null元素。
4)The implementations rely on a concept of optional methods in interfaces. If an implementation doesn't
support an operation, such as adding or removing elements, the implementation throws an
UnsupportedOperationException when you call a method that hasn't been fully implemented.