java.util List and set

来源:互联网 发布:js中保留字的单词 编辑:程序博客网 时间:2024/05/29 21:28

List and Set are two interfaces which extends from the interface Collection.


1.List

List接口提供的适合于自身的常用方法均与索引有关,这是因为List集合为列表类型,以线性方式存储对象,可以通过对象的索引操作对象。

List接口的常用实现类有ArrayList和LinkedList,在使用List集合时,通常情况下声明为List类型,实例化时根据实际情况的需要,实例化为ArrayList或LinkedList

/***interface List is extends from the Collection, and add some functions which are related to the index. *and that's the List's most advantage.  */public interface List<E> extends Collection<E>{// return the number of elements in this collection contains no elementsint size();// return true if this collection contains no elementsboolean isEmpty();//return true if this collection contains the specified elementsboolean contains(Object o);// return an Iterator over the elements in this collectionIterator<E> iterator();//return an array containing all of the elements in this collectionObject[] toArray();//<T> T[] toArray(T[] a);//add an elementsboolean add(E e);//remove an elementboolean remove(Object o);//Returns true if this collection contains all of the elements    // in the specified collection.boolean containsAll(Collection<?> c);//adds all of the elements in the specified collection to this collectionboolean addAll(Collection<? extends E> c);//Removes all of this collection's elements that are also contained in the    //specified collectionboolean removeAll(Collection<?> c);//Retains only the elements in this collection that are contained in the specified collectionboolean retainAll(Collection<?> c);//clear the collection remove all the elements from this collectionvoid clear();//Compares the specified object with this collection for equality.boolean equals(Object o);//returns the hash code value for this collection.//while the collection interface adds no stipulations to the general contract//for the Object.hashCode methos, programmers should take note that any class that override//the Object.equals method must also override the Object.hashCode method in //order to satisfy the general contract for the Object.hashCode method .//In particular, c1.equals(c2) implies that c1.hashCode == c2.hashCodeint hashCode();//addational//return the element at the specified position in this ListE get(int index);//replaces the element at the specified position in this List with// the specified element// and return the element previously at this positionE set(int index, E element);//inserts the specified element at the specified position in this List//Shifts the element currently at the position and any subsequent elements  to right//adds one to their indicesvoid add(int index, E element);//remove the element at the specified position in the list//return the element previously at this positionE remove(int index);//search operation//Returns the index of the first occurrence of the specified element    //in this list, or -1 if this list does not contain the element.int indexOf(Object o);    /**     * Returns the index of the last occurrence of the specified element     * in this list, or -1 if this list does not contain the element.     */    int lastIndexOf(Object o);    // List Iterators    /**     * Returns a list iterator over the elements in this list (in proper     * sequence).     */    ListIterator<E> listIterator();    /**     * Returns a list iterator over the elements in this list (in proper     * sequence), starting at the specified position in the list.     */    ListIterator<E> listIterator(int index);    // View    /**     * Returns a view of the portion of this list between the specified     * <tt>fromIndex</tt>, inclusive, and <tt>toIndex</tt>, exclusive.  (If     * <tt>fromIndex</tt> and <tt>toIndex</tt> are equal, the returned list is     * empty.)  The returned list is backed by this list, so non-structural     * changes in the returned list are reflected in this list, and vice-versa.     * The returned list supports all of the optional list operations supported     * by this list.<p>     *     * This method eliminates the need for explicit range operations (of     * the sort that commonly exist for arrays).  Any operation that expects     * a list can be used as a range operation by passing a subList view     * instead of a whole list.  For example, the following idiom     * removes a range of elements from a list:     * <pre>     *      list.subList(from, to).clear();     * </pre>     * Similar idioms may be constructed for <tt>indexOf</tt> and     * <tt>lastIndexOf</tt>, and all of the algorithms in the     * <tt>Collections</tt> class can be applied to a subList.<p>     *     * The semantics of the list returned by this method become undefined if     * the backing list (i.e., this list) is <i>structurally modified</i> in     * any way other than via the returned list.  (Structural modifications are     * those that change the size of this list, or otherwise perturb it in such     * a fashion that iterations in progress may yield incorrect results.)     *     * @param fromIndex low endpoint (inclusive) of the subList     * @param toIndex high endpoint (exclusive) of the subList     * @return a view of the specified range within this list     * @throws IndexOutOfBoundsException for an illegal endpoint index value     *         (<tt>fromIndex < 0 || toIndex > size ||     *         fromIndex > toIndex</tt>)     */    List<E> subList(int fromIndex, int toIndex);}

Here we have a ListIterator which is an interface extends from the Iterator.

and the ListIterator is:



0 0
原创粉丝点击