List interface of the Collection framework, list of the Collection framework

Source: Internet
Author: User
Tags addall

List interface of the Collection framework, list of the Collection framework


An ordered collection (also called a sequence ). Users of this interface can precisely control the insert position of each element in the list. You can access the element based on the integer index (position in the list) of the element and search for the element in the list.

Unlike set, the list is usuallyRepeated elements are allowed.. To be more precise, the list usually allows e1.equals (e2) elements to pair e1 and e2, and if the List itself permits null elements, they usually allow multiple null elements.

In the List interfaceThe remove () method removes only the first element in the list., Add () and addAll () methods always append new elements to the end of the list.

The List interface provides four access methods for locating (indexing) List elements: get (), set (), add (), remove (), set (), and remove () the returned value is the overwritten or removed element value.The List (like the Java array) is based on 0.. Note that these operations may be performed within a period proportional to the index values of some implementations (such as the sort list class. Therefore, if the caller does not know the implementation, iteration on the list element is usually better than traversing the list by index.

The List interface provides a special iterator calledListIteratorIn addition to the normal operations provided by the Iterator interface, the Iterator also allows element insertion and replacement, and bidirectional access (besides having hasNext () and next, there are also hasPrevious () and previous () methods ). You can also obtain the list iterator starting from the specified position in the list.

The List interface provides two methods to search for a specified object: indexOf () and lastIndexOf (). It is used to return the subscript that appears for the first time and the subscript that appears for the last time. From the performance point of view, you should be careful to use these methods. In many implementations, they perform highly open linear search.

The List interface provides the method subList () to return to the List View ()

Let's look at the following example:

List<Integer> list = new ArrayList<Integer>();         list.add(1);         list.add(2);         list.add(3);         list.add(4);         list.add(5);         list.add(6);                 list.subList(1, 5).clear();                 Iterator<Integer>it= list.listIterator();         while(it.hasNext()){                System.out.println(it.next());         }


The list. subList (). clear () code deletes the Elements marked as greater than or equal to 1 and less than 5 in the list.

The subList () method does not return a new list. Instead, it intercepts an element from the original list as an operation object. In essence, it still performs operations on the meta list.

It is also the list above. After executing the following code

List. subList (1, 5). indexOf (4 );

The result is 2, list. subList () intercepts the elements "2, 3, 4, and 5" in the middle of the original list as the operation object. In this case, execute the indexOf (1) method, the returned element "4" is the subscript (2) in the sublist, rather than the subscript (3) in the original list)

Although the subList () method is powerful, the List returned by subList () method is only suitable for temporary objects for limited operations. Once the initial List of the subList changes, this sublist will become an undefined object.

List <Integer> list = newArrayList <Integer> (); list. add (1); list. add (2); list. add (3); list. add (4); list. add (5); list. add (6); List <Integer> list2 = list. subList (1, 5); list. remove (5); System. out. println (list2.get (2); // The initial list is changed. This Code throws an exception.

In CollectionsClass defines many static methods for processing sets, many of which are specifically for List operations, Collections. sort () can be used for sorting, Collections. swap () can be used to exchange the positions of two elements in the list, Collections. binarySearch () uses the Binary Search Method to locate the elements in the list, and so on.

The List interface's extension method (that is, the method inherited from the Collection Interface) List is as follows:

Void add (int index, E element)

Insert a specified element at a specified position in the list (optional ).

Boolean addAll (int index, Collection <? Extends E> c)

Insert all elements in the specified collection to the specified position in the list (optional ).

Void clear ()

Removes all elements from the List (optional ).

E get (int index)

Returns the element at the specified position in the list.

Int indexOf (Object o)

Returns the index of the specified element that appears for the first time in this list. If the list does not contain this element,-1 is returned.

Int lastIndexOf (Object o)

Returns the index of the last specified Element in the list. If the list does not contain this element,-1 is returned.

List Iterator <E> listIterator ()

Returns the list iterator for this list element (in the appropriate order ).

List Iterator <E> listIterator (int index)

Return the list iterator of the elements in the list (in the proper order), starting from the specified position in the list.

E remove (int index)

Removes an element at a specified position from the List (optional ).

E set (int index, E element)

Replace the specified position in the list with the specified Element (optional ).

List <E> subList (int fromIndex, inttoIndex)

Return some views between fromIndex (included) and toIndex (excluded) specified in the list.

In addition, the ListIterator () method returnsListIterator InterfaceMethod list:

Void add (E e)

Insert the specified element into the List (optional ).

Boolean hasNext ()

When traversing the list in a forward direction, if the list iterator has multiple elements, true is returned (in other words, if next returns an element instead of throwing an exception, true is returned ).

Boolean hasPrevious ()

Returns true if the list iterator contains multiple elements in reverse traversal.

E next ()

Returns the next element in the list.

Int nextIndex ()

Returns the index of the elements returned by subsequent calls to next.

E previous ()

Returns the previous element in the list.

Int previusindex ()

Returns the index of the elements returned by subsequent calls to previous.

Void remove ()

Remove the last element returned by next or previous from the List (optional ).

Void set (E e)

Replace the last element returned by next or previous with the specified Element (optional ).

List has two common implementation classes: ArrayList and rule List.In most cases, select ArrayList.Because its random access efficiency is much higher than that of the shortlist. However, when we need to add and delete elements frequently, the sorted list will be better than the ArrayList.

ArrayList

The implementation of Variable-size array of the List interface. All optional list operations are implemented, and all elements including null are allowed. In addition to the List interface, this class also provides some methods to internally store the size of the List array. (This is basically equivalent to the Vector class, except that this class is not synchronized .)

Each ArrayList instance has a capacity. This capacity refers to the size of the array used to store list elements. It is always equal to at least the size of the list. As elements are added to the ArrayList, their capacity increases automatically. The details of the growth policy are not specified, because it is not just as simple as adding an element to allocate a fixed time overhead.

Before adding a large number of elements, the application can use the ensureCapacity operation to increase the capacity of the ArrayList instance. This can reduce the number of incremental redistribution.

Extension constructor:

ArrayList (intinitialCapacity)

Creates an empty list with the specified initial capacity.

Extension Method:

Void ensureCapacity (int minCapacity)

If necessary, increase the capacity of this ArrayList instance to ensure that it can accommodate at least the number of elements specified by the minimum capacity parameter.

Protected void removeRange (int fromIndex, int toIndex)

Remove all elements in the list between fromIndex (included) and toIndex (excluded.

Void trimToSize ()

Adjust the capacity of the ArrayList instance to the current size of the list.

Shortlist

List interface. Implement all optional list operations and allow all elements (including null ). In addition to the List interface, the revoke List class also provides a unified naming method for get, remove, and insert elements at the beginning and end of the List.These operations allow the link list to be used as a stack, queue, or dual-end queue.

This type of Deque interface provides first-in-first-out queue operations for add and poll, as well as other stack and double-end queue operations.

All operations are performed according to the dual-link list.. The indexing operation in the list traverses the list from the beginning or end (from the end near the specified index ).

The inclulist implements the Deque, Queue, and List interfaces at the same time. In addition to the methods defined by the List interface, there are many other methods used to operate Queue and Deque.

List of extended methods:

Void addFirst (Ee)

Insert the specified element at the beginning of the list.

Void addLast (Ee)

Add the specified element to the end of the list.

Iterator <E> descendingIterator ()

Returns the iterator that iterates on the elements of this double-end queue in reverse order.

E element ()

Obtain but not remove the header (the first element) of this list ).

E getFirst ()

Returns the first element of the list.

E getLast ()

Returns the last element of the list.

Boolean offer (Ee)

Add the specified element to the end of the list (the last element ).

Boolean offerFirst (Ee)

Insert the specified element at the beginning of the list.

Boolean offerLast (E e)

Insert the specified element at the end of the list.

E peek ()

Obtain but not remove the header (the first element) of this list ).

E peekFirst ()

Obtain but not remove the first element from the list. If the list is empty, null is returned.

E peekLast ()

Obtain but not remove the last element from the list. If the list is empty, null is returned.

E poll ()

Get and remove the header of this list (first element)

E pollFirst ()

Gets and removes the first element from the list. If the list is empty, null is returned.

E pollLast ()

Gets and removes the last element from the list. If the list is empty, null is returned.

E pop ()

An element pops up in the stack represented in this list.

Void push (E e)

Push an element into the stack represented in this list.

E removeFirst ()

Remove and return the first element in this list.

Boolean removeFirstOccurrence (Object o)

Remove the specified element that appears for the first time from this list (when traversing the list from the header to the end ).

E removeLast ()

Remove and return the last element in the list.

Boolean removeLastOccurrence (Object o)

Remove the last specified element from the List (when traversing the list from the header to the end ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.