1. What is a Lsit collection?
A list collection is an ordered, repeatable collection of elements in which each element has its own element index. The list collection allows repeatable elements that can be indexed to access the elements at the specified location.
2.JAVA8 improved list interface and Listiterator interface
List as a sub-interface of the collection collection, you can use all the methods of the collection interface. In addition, because the list is an ordered set, some methods are added to manipulate the list collection by index.
void Add (int index, Object Element): elements element is added to the List collection index at index.
boolean addall (int index, Collection c): inserts all the elements from the Collection collection to index at the list collection.
Object Get (int index): returns the element at index.
int indexOf (Object o): Returns the position index of the first occurrence of the element o in the list collection.
int lastIndexOf (Object o): Returns the position index of the last occurrence of element o in the list collection.
object Set (int index, Object Element): An element that indexes the position as an index, and replaces it with elements.
list sublist (int fromIndex, int toindex): returns all elements from the index FromIndex to the index Toindex in the list collection.
Java8 adds the following two default methods to the list collection:
void ReplaceAll (unaryoperator operator): re-sets all elements of the list collection according to the calculation rules specified by operator.
void Sort (Comparator c): Sorts the elements of the list collection according to the Comparator parameter.
Public classListtest { Public Static voidMain (string[] args) {List s=NewArrayList (); S.add (NewString ("Spring")); S.add (NewString ("Autumn")); S.add (NewString ("Winter")); //output: [Spring, Autumn, winter]System.out.println (s); S.add (1,NewString ("Summer")); S.remove (2); //output: [Spring, Summer, winter]System.out.println (s); //Output: 1System.out.println (S.indexof (NewString ("Summer"))); S.set (1,NewString ("Autumn")); //output: [Spring, Autumn, winter]System.out.println (s); //output: [Autumn]//including Fromindex itself, but not including Tofrom itselfSystem.out.println (S.sublist (1, 2)); }}
Public classListTest0 { Public Static voidMain (string[] args) {List books=NewArrayList (); Books.add (NewString ("Lightweight Java EE Enterprise application Development")); Books.add (NewString ("Crazy Java Handout")); Books.add (NewString ("Crazy Android Handout")); Books.add (NewString ("Crazy iOS Handout")); //sort the list collection using a lambda expression of the target type comparatorBooks.sort ((O1,o2) ((String) O1). Length ()-(String) O2). Length ()); //output: [Crazy iOS handouts, crazy Java handouts, crazy Android handouts, lightweight Java EE Enterprise application development]System.out.println (books); Books.replaceall (Ele-(String) ele). Length ()); //output: [7, 9, one, +]System.out.println (books); }}
3.ArrayList and vector implementation classes
both the ArrayList and the vector classes are list classes based on an array implementation, so the ArrayList and vector classes encapsulate a dynamic, object[] array that is allowed to be allocated. The ArrayList and Vector objects use the initialcapacity parameter to set the length of the array, and their initialcapacity automatically increases when the elements added to the ArrayList or vector exceed the length of the array .
ArrayList and vectors provide two ways to reassign the object[] array:
void ensurecapacity (int mincapacity): increases the length of the object[] array of the ArrayList or vector collection by greater than or equal to the mincapacity value.
void TrimToSize (): adjusts the length of the objec[] array of the ArrayList or vector collection to the current number of elements. Calling this method reduces the storage space used by ArrayList or vector collection objects.
ArrayList and Vecto are almost identical in usage, but since object[] is an ancient set (JDK1.0 has it), vectors have earlier provided many ways to name long methods, and the method named in vectors is later added.
A notable difference between ArrayList and vectors is that ArrayList is thread insecure, and when multiple threads access the same ArrayList collection, if more than one thread modifies the ArrayList collection, the program must manually guarantee the synchronization of the collection. However, vectors are thread-safe and do not need to guarantee synchronization of the set. Because vectors are thread-safe, the performance of vectors is lower than the performance of ArrayList collections. In fact, it is not recommended to use vectors even if you need to ensure thread safety for the list collection.
4. Fixed-length list
There is a tool class arrays that manipulates arrays, which provides a aslist (object ... a) method that converts an array or a specified number of objects into a list collection, which is not an instance of the ArrayList implementation class. Nor is it an instance class of vectors, but an instance of arrays's inner class ArrayList.
Arrays.arraylist is a fixed-length list collection that the program can only traverse to access the elements in the collection, not add, and delete the elements in the collection.
Public classFixedsizelist { Public Static voidMain (string[] args) {List fixedlist= Arrays.aslist ("Crazy Java Handout", "Crazy iOS Handout"); //get the implementation class for Fixedlist//output: Class Java.util.arrays$arraylistSystem.out.println (Fixedlist.getclass ()); //output: Crazy java Handout//Crazy iOS HandoutsFixedlist.foreach (system.out::p rintln); //attempting to add, deleting an element throws an Unsupportedoperationexception exception//Fixedlist.add ("Crazy Android Handout"); }}
List Collection of Java collections