"List of Java collection frameworks"

Source: Internet
Author: User

Five, List interface.

A great feature of the list is the ability to manipulate the angle mark.

The following is an introduction to the more specific method in the list interface relative to the collection interface. The methods already described in the collection interface are not covered here.

1. Add

 void add(int index, E element)
Inserts the specified element (optional action) at the specified position in the list.
 boolean addAll(int index, Collection<? extends E> c)
Inserts all the elements in the specified collection into the specified position in the list (optional action).

These two methods, relative to the collection interface, can insert new elements or collections directly in a specific place.

2. Delete.

 E remove(int index)
Removes the element from the specified position in the list (optional action).

This method can be used to directly delete elements in a particular location relative to the collection interface.

3. Modifications

 E set(int index, E element)
Replaces the element in the specified position in the list with the specified element (optional action).

This method can replace the element at the specified position with another element (object).

4. Find

 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 first appears in this list, or 1 if the list does not contain the element.
 int lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this list, or 1 if the list does not contain this element.
 List<E> subList(int fromIndex, int toIndex)
Returns a partial view between the specified FromIndex(inclusive) and Toindex(not included) in the list.

As you can see, all the unique methods of the list above are related to the operation of the corner label, which is caused by the "orderly" character of the list.

Unique method for 5.List interface: Listiterator

 ListIterator<E> listIterator()
Returns the list iterator for this list element, in the appropriate order.
 ListIterator<E> listIterator(int index)
Returns the list iterator (in the appropriate order) of the elements in the list, starting at the specified position in the list.

Case:

1  PackageP01. Basecollectiondemo;2 3 Importjava.util.ArrayList;4 ImportJava.util.Iterator;5 Importjava.util.List;6 7  Public classListiteratordemo {8 9      Public Static voidMain (string[] args)Ten     { OneList al=NewArrayList (); AAl.add ("ABC1"); -Al.add ("ABC2"); -Al.add ("ABC3"); theAl.add ("ABC4"); - Demo1 (AL); -     } -  +     Private Static voidDemo1 (List al) { -          for(Iterator it=al.iterator (); It.hasnext ();) +         { AString str=(String) It.next (); at System.out.println (str); -             if(str== "ABC1") -             { -Al.add ("Abc5"); -             } -         } in     } -      to  +}
View Code

An exception occurred in the running result:

Analysis: Viewing the API, you can get an API description of the Concurrentmodificationexception exception: This exception is thrown when the method detects concurrent modifications to an object, but does not allow the modification. The program is a single-threaded, how to make a concurrent modification of the object? The real reason is that the iterator has determined the number of objects in the container before traversing, and if the object is incremented within the container, the iterator will not be able to determine the number of objects within the container. Iterators iterate over an object, and the Add method adds an object, which results in concurrent modifications to the object. The API's description of such exceptions is that if a single thread issues a sequence of method calls that violate an object contract, the object might throw this exception. For example, an iterator throws this exception if the thread uses a fast-failed iterator to directly modify the collection when iterating over the collection. In short, iterations are not allowed to modify the elements at the time of the collection.

Workaround: Use the Listiterator iterator.

A listiterator iterator is a list-specific iterator that does not apply to iterations of other collections.

1  PackageP01. Basecollectiondemo;2 3 Importjava.util.ArrayList;4 ImportJava.util.Iterator;5 Importjava.util.List;6 ImportJava.util.ListIterator;7 8  Public classListiteratordemo {9 Ten      Public Static voidMain (string[] args) One     { AList al=NewArrayList (); -Al.add ("ABC1"); -Al.add ("ABC2"); theAl.add ("ABC3"); -Al.add ("ABC4"); - //Demo1 (AL); - Demo2 (AL); +     } -  +     Private Static voidDemo2 (List al) { A          for(Listiterator li=al.listiterator (); Li.hasnext ();) at         { -String str=(String) Li.next (); -             if(Str.equals ("ABC1")) -             { -Li.add ("Abc5"); -             } in         } - System.out.println (AL); to          +     } -  the     Private Static voidDemo1 (List al) { *          for(Iterator it=al.iterator (); It.hasnext ();) $         {Panax NotoginsengString str=(String) It.next (); - System.out.println (str); the             if(str== "ABC1") +             { AAl.add ("Abc5"); the             } +         } -     } $      $  -}
View Code

Operation Result:

[ABC1, Abc5, ABC2, ABC3, ABC4]

The key to solving the problem is to add elements using the Add method in Listiterator. In other words, whether it is an iteration or a modified element, it is given to Listiterator to complete. It is still not permissible for the collection to modify the elements when iterating.

Listiterator listiterator (int index), the method specifies the position of the iteration, which can be self-made, and the default (no parameter) is 0.

In addition, Listiterator offers two more interesting ways to do this:

 boolean hasPrevious()
Returns trueif the list iterator has more than one element in the reverse loop.
 E previous()
Returns the previous element in the list.

These two methods allow for reverse traversal.

6.List interface commonly used "sub-class" characteristics.

There are three categories under list: Vector, ArrayList, LinkedList.

6.1Vector class.

The vector class has appeared since the JDK1.0, it can be said to be the collective framework of the elder cadres. Since the JDK2.0 appeared, the Vector class has joined the team of the set frame and has been grouped into list. The greatest feature of the vector class is thread safety, which is uncommon throughout the set framework. It is because of thread safety that execution is inefficient and is now almost obsolete. Even though it is not used in multithreaded programming, it is replaced with a ArrayList lock. The inside of the vector is an array structure and is extensible, with a step size of 100%. Vector additions and deletions are very slow.

6.2ArrayList class.

The ArrayList class is intended to replace the vector class, so its function is almost identical to the vector class, but it is not synchronous. ArrayList also maintains a variable-length array, with a growth step of 50% and a fast query.

7.LinkedList class.

LinkedList is the list interface link listing implementation. That means we're talking about the linked list. The biggest feature of LinkedList is the fast and easy to delete.

8.LinkedList and ArrayList all have get methods, what's the difference?

Although both are sub-categories of the list, but the bottom of the ArrayList is the array data structure, in memory is continuous, so use the Get method is not used to find from scratch, but LinkedList is a linked list data structure, even if you use the Get method, but still need to find from scratch.

"List of Java collection frameworks"

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.