List interface
The list interface represents an ordered collection, and each element in the collection has its corresponding sequential index. The list allows you to use a repeating element to access a collection element at a specified location through an index. The default is to add element order as index.
The list can be indexed to insert, replace, and delete elements relative to the set set.
The standard that list determines the equality of two objects is that the Equals method returns True.
Examples of Use:
Import java.util.ArrayList;
Import java.util.List;
public class Testlist {
/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
List books = new ArrayList ();
Books.add ("Book1");
Books.add ("Book2");
Books.add ("Book3");
SYSTEM.OUT.PRINTLN (books);
Books.add (1, "dog");
for (int i = 0; i < books.size (); i++) {
System.out.println (Books.get (i));
}
Books.remove (2);
SYSTEM.OUT.PRINTLN (books);
Determine the position of an element in a collection
System.out.println (Books.indexof (New String ("BOOK3"));
Replace element
Books.set (0, New String ("Duck"));
SYSTEM.OUT.PRINTLN (books);
Output subset
System.out.println (Books.sublist (1, 2));
}
}
The set (int index,object Element) method in the above example does not change the length of the list, that is, index must be less than the length of the collection
The list provides a Listiterator () method that returns an Listiterator object with the interface implemented by the class that inherits the iterator interface, providing a way to specifically manipulate the list.
Example:
Import java.util.ArrayList;
Import java.util.List;
Import Java.util.ListIterator;
public class Testlistiterator {
/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
string[] Books = {"Book1", "Book2", "BOOK3"};
List Booklist = new ArrayList ();
for (int i = 0; i < books.length; i++) {
Booklist.add (Books[i]);
}
Listiterator li = Booklist.listiterator ();
System.out.println ("============= forward Iteration ==============");
while (Li.hasnext ()) {
System.out.println (Li.next ());
}
SYSTEM.OUT.PRINTLN ("============ reverse Iteration =============");
while (Li.hasprevious ()) {
System.out.println (Li.previous ());
}
}
}
Both ArrayList and vectors implement the list interface
The difference between ArrayList and vectors
Vector has many ways to repeat, there are many shortcomings
Vector thread safety, ArrayList is not safe
Vector provides a stack subclass that simulates the stack function
Arrays.arraylist is a fixed-length list collection, and unlike the ArrayList collection, a program can only traverse the Arrays.arraylist collection, not add, delete, or element in the collection.
The queue interface simulates the queuing data structure, with linkelist and priorityqueue two implementation classes.
LinkedList also implements the list interface and implements the queue interface, all of which are exceptionally powerful, can be used as queues, and can be used as a list
Example:
Import java.util.LinkedList;
public class Testlinkedlist {
/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
LinkedList books = new LinkedList ();
Joins a string to the end of a queue
Books.offer ("Book1");
SYSTEM.OUT.PRINTLN (books);
Books.offer ("Book2");
SYSTEM.OUT.PRINTLN (books);
for (int i = 0; i< books.size (); i++) {
System.out.println (Books.get (i));
}
Access, do not delete the first element in a queue
System.out.println (Books.peek ());
SYSTEM.OUT.PRINTLN (books);
Access, and delete the first element in a queue
System.out.println (Books.poll ());
SYSTEM.OUT.PRINTLN (books);
}
}
ArrayList and vectors are stored as arrays, so the random access to the collection element performs well, the LinkedList is stored as a list, and the random access performance is poor when the insert and delete elements perform well. Vector realizes the thread synchronization function, the performance of each aspect is lower than ArrayList.
Priorityqueue is a queue that is reordered in element size
Inserting null is not allowed, and the default is natural ordering.
List of Java collections