LinkedList in Java

Source: Internet
Author: User
Tags addall concurrentmodificationexception

First, class structure:
Java.lang.Object  java.util.abstractcollection<e>      java.util.abstractlist<e>          java.util.abstractsequentiallist<e>              java.util.linkedlist<e>
Ii. Declaration of class:
Extends abstractsequentiallist<e> implements List<e>, Deque<e>, Cloneable, Serializable

Three, overview:

The list interface is implemented as a link listing. Implements all optional list operations, and allows all elements (including null). In addition to implementing the list interface, theLinkedList class is also the get,remove , and insert at the beginning and end of the list Element provides a uniform naming method. These operations allow the link list to be used as a stack, queue, or double-ended queue.

This class implements the Deque interface for add,poll , and other stack and double-ended queue operations.

All actions are performed as required by the list of double links. Indexing in the list iterates through the list from the beginning or end (from one end near the specified index).

Note that this implementation is not synchronous. If multiple threads access a linked list at the same time, and at least one of the threads modifies the list from the fabric, it must remain externally synchronized. (A structural modification is any action that adds or removes one or more elements; The value of the set element is not a structural modification.) This is typically done by synchronizing the objects that naturally encapsulate the list. If such an object does not exist, you should use the Collections.synchronizedList method to "wrap" the list. It is a good idea to do this at creation time to prevent accidental, out-of-sync access to the list, as follows:

   List List = Collections.synchronizedlist (new LinkedList (...));

The iterator returned by the iterator and listiterator methods of this class is a quick failure : After the iterator is created, if the list is modified from the structure, except through the remove of the iterator itself or the add method, any other modification of any time, the iterator will be thrown ConcurrentModificationException . Therefore, in the face of concurrent modifications, the iterator will soon fail completely without risking any uncertain behavior at any time in the uncertain future.

Note that the fast failure behavior of iterators is not guaranteed and, in general, there is no guarantee that any hard guarantees can be made when there are concurrent changes that are out of sync. The fast-failing iterator does its best to throw concurrentmodificationexception. Therefore, the way to write a program that relies on this exception is wrong, and the correct approach is that the fast failure behavior of the iterator should only be used to detect program errors.

This class is a member of the Java collections Framework.

Four, the construction method:

LinkedList () constructs an empty list.

LinkedList (collection<? extends e> c) Constructs a list that contains the elements in the specified Collection, arranged in the order that they are returned by their Collection iterator.

V. Details of the method:

1, public E getfirst() returns the first element of this list.

specified by: Deque<E> in the interfacegetFirst

return: The first element of this list

thrown: NoSuchElementException -If this list is empty

2, public E getlast() returns the last element of this list.

3, public E Removefirst() removes and returns the first element of this list.

4. Public E removelast() removes and returns the last element of this list.

5. public void AddFirst(e e) Inserts the specified element at the beginning of this list.

6. public void AddLast(e E) Adds the specified element to the end of this list.

7, public boolean contains(Object o) returns trueif this list contains the specified element. More precisely, returns truewhen and only if this list contains at least one element e that satisfies (o==null? E==null:o.equals (e)) .

8, public int size() returns the number of elements in this list.

9, public boolean Add (E) adds the specified element to the end of this list. This method is equivalent to addLast(E) .

10. The public boolean remove (Object O) removes the first occurrence of the specified element (if present) from this list. If the list does not contain the element, no changes are made. Rather, remove the element that has the lowest index i (if such an element exists) that satisfies (o==null. Get (i) ==null:o.equals (get (i )). Returns trueif the list already contains the specified element (or if the list has changed because of a call).

11, public boolean AddAll(collection<? extends e> c) adds all the elements in the specified Collection to the end of this list, in order to specify the Collection of the iterator returned The order of these elements. If the specified collection is modified during the operation, the behavior of this operation is indeterminate. (Note that if the specified collection is this list and is not empty, the behavior of this operation is indeterminate.) )

12. Public Boolean addall(int index, COLLECTION<? extends e> c) inserts all the elements in the specified Collection from the specified position to insert this list. Moves the element that is currently at that position (if any), and all subsequent elements move to the right (increasing their index). The new element is displayed in the list in the order returned by the iterator that specifies collection.

13, public void Clear() removes all elements from this list.

14, public E get(int index) returns the element at the specified position in this list.

15, the public E set(int index,e Element) replaces the element of the specified position in this list with the specified element.

16, public void add(int index, E Element) inserts the specified element in the location specified in this list. Moves the element that is currently at that position (if any), and all subsequent elements move to the right (add 1 in their index).

17, public E Remove (int index) removes the element at the specified position in this list. Move any subsequent elements to the left (minus 1 from the index). Returns the element that is removed from the list.

18, public int indexOf(Object o) returns the index of the specified element that first appears in this list, or 1 if the element is not included in this list. Rather, returns the lowest index isatisfies (o==null. Get (i) ==null:o.equals (get (i )); Returns 1 if there is no this index.

19, public int lastIndexOf(Object o) returns the index of the last occurrence of the specified element in this list, or 1 if the element is not included in this list. To be more exact, return the highest index isatisfies (o==null. Get (i) ==null:o.equals (get (i )); Returns 1 if there is no this index.

20. Public E Peek() gets but does not remove the header (the first element) of this list.

21, public E element() gets but does not remove the header (the first element) of this list.

22. Public E Poll() gets and removes the header of this list (first element)

23, public E remove() Gets and removes the header of this list (the first element).

24, public a Boolean offer (e) adds the specified element to the end of this list (the last element).

25. Public Boolean Offerfirst(e) Inserts the specified element at the beginning of this list.

26. Public Boolean offerlast(e e) Inserts the specified element at the end of this list.

27, public E Peekfirst() gets but does not remove the first element of this list, or nullif the list is empty.

28, public E peeklast() gets but does not remove the last element of this list, or nullif the list is empty.

29, public E Pollfirst() gets and removes the first element of this list, or nullif the list is empty.

30, public E polllast() gets and removes the last element of this list, or nullif the list is empty.

31, public void push(e e) pushes elements into the stack represented by this list. In other words, insert the element at the beginning of this list. This method is equivalent to addFirst(E) .

32. Public E pop() pops an element at the stack represented by this list. In other words, remove and return the first element of this list. This method is equivalent to removeFirst() .

33. Public Boolean removefirstoccurrence(Object o) removes the first occurrence of the specified element from the list (from head to tail when traversing the list). If the list does not contain the element, no changes are made.

34. Public Boolean removelastoccurrence(Object o) removes the last occurrence of the specified element from this list (when traversing the list from head to tail). If the list does not contain the element, no changes are made.

35, public listiterator<e> listiterator(int index) returns a list iterator (in the appropriate order) of the elements in this list, starting at the specified position in the list. Abide by the general contract of List.listiterator (int) .

A list iterator is a quick failure : After the iterator is created, if the list is modified from the structure, the list iterator will be thrown unless it is modified by the remove or add method of the list iterator itself at any other time. concurrentmodificationexception. Therefore, in the face of concurrent modifications, the iterator will soon fail completely without risking any uncertain behavior at any time in the uncertain future.

36. Public iterator<e> descendingiterator() returns an iterator that iterates over the elements of this double-ended queue in reverse order. Elements are returned in the order from the last (tail) to the first (head).

37, public Object clone() returns a shallow copy of this LinkedList . (The elements themselves are not duplicated.) )

38, public object[] toArray() returns an array that contains all the elements in this list in the appropriate order (from the first element to the last element).

Because this list does not maintain any references to the returned array, it will be "safe." (In other words, this method must be assigned a new array). Therefore, the caller can arbitrarily modify the returned array.

This method serves as a bridge between an array-based API and an collection-based API.

39, public <T> t[] toArray(t[] a) returns an array containing all the elements in this list in the appropriate order (from the first element to the last element); Returns the type of the array whose run-time type is the specified array. If the specified array can hold the list, the list is returned in it. Otherwise, the run-time type with the specified array and a new array of this list size are assigned.

If the specified array can hold the list and there is space left (that is, the array is more than the list element), the array element immediately following the end of the list is set to null. (This method can be used to determine the length of a list only if the caller knows that the list does not contain any null elements.) )

LinkedList in Java

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.