This blog mainly introduces the list collection and the queue collection
A. List collection
The list collection is an ordered, repeatable collection of each element in the collection that corresponds to an index, and we can access the data in the list collection by means of an index (which can actually be seen as a variable-length array).
List as a sub-interface of collection, supports all operations of collection, but because the list is ordered, new additions are made to the method of inserting, deleting, and replacing elements based on the index. List determines whether an object is equal by using the Equals () method. As shown in the following code:
Package Lkl;import Java.util.arraylist;import java.util.collection;import java.util.hashset;import java.util.List; Public classlisttest { Public Static void Main(string[] args) {Collection c =NewHashSet (); C.add ("Java"); C.add ("C"); C.add ("C + +"); List ls =NewArrayList (); Ls.add (1); Ls.add (1); Ls.add (-1); Ls.add (3); Ls.add (3); System. out. println (LS); //void Add (int index,object e) //// insert element e into List and subscript to index, the index value cannot be greater than the original length of the listLs.add (2,4); System. out. println (LS); ///void AddAll (int index,collection c) /// Insert all elements from collection C into index of listLs.addall (2, c); System. out. println (LS); //Object get (int index) /// Returns the element at List index, which can be used in conjunction with a for loop to iterate through the list for(intI=0; I<ls.size (); i++) System. out. print (LS.Get(i) +" "); System. out. println (); //int indexOf (Object o): Returns the position of element o first occurrence in the collection //int lastIndexOf (Object o): Returns the position of the last occurrence of element o in the collectionSystem. out. println (Ls.indexof (3)); System. out. println (Ls.lastindexof (3)); //Object Remove (int index): deletes and returns the element at indexSystem. out. println (Ls.remove (2)); System. out. println (LS); ///Object set (int index,object e) /// Replace the object at index with E and return the original objectSystem. out. println (LS.Set(1,3)); System. out. println (LS); //List sublist (int index1,int index2) ///Returns a subset of elements in Index1 and Index2System. out. println (Ls.sublist (2,5)); }}
For list, because it is ordered, the list provides an additional traversal method Lsititerator (), which provides the ability to reverse iterate, and also adds elements to the collection during traversal. The method is increased compared to the iterator interface as follows:
Boolean hasprevious (): Returns whether the collection associated with the iterator has a previous element.
Object Previous (): Returns the previous element of the iterator
void Add (): Inserts an element into the specified position
The following code demonstrates the effects of these operations:
Package Lkl;import Java.util.arraylist;import Java.util.list;import java.util.iterator;import Java.util.ListIterator; Public classlistiteratortest { Public Static void Main(string[] args) {List ls =NewArrayList (); Ls.add (1); Ls.add (2); Ls.add (3);/// Declare an iterator of type ListiteratorListiterator lt = Ls.listiterator ();/// forward iterative process while(Lt.hasnext ()) {intK= (int) Lt.next (); System. out. print (k +" ");///// You can also insert an element during the iteration, but the inserted element does not affect the traversal if(k==2) Lt.add (4); } System. out. println (); System. out. println ("Here is the reverse iteration process");/// below begins the reverse iteration process while(Lt.hasprevious ()) {System. out. Print (lt.previous () +" "); } System. out. println (); }}
Two. Implementation classes for the list collection: ArrayList and vectors
ArrayList and vectors are the two typical implementations of the list, fully supporting all the operations described earlier. Both ArrayList and vectors are list classes based on array implementations, so the ArrayList and vector classes encapsulate a dynamic object[] array that allows redistribution. The ArrayList and Vector objects use the initialcapacity parameter to set the length of the array, and then the initialcapcity increases automatically when the array is full, but if we want to add a large number of elements to ArrayList, Then we can use the Ecsurecapacity () method to increase the initialcapacity at once, which reduces the number of allocations and improves performance. Object[] Array default length is 10.
The vector is almost identical to the ArrayList usage, except that the vector is an ancient collection.
And the vector also encapsulates a stack subclass that simulates the stack using the following:
Package LKL;Import Java. Util. Stack;public class Vectorstacktest {public static void main (string[] args) {//Declaration of a stack stackSt= new Stack ();Put elements into the stackSt. Push("C Language"); St. Push("CPP"); St. Push("Java");System. out. println(St);Returns the top element of the stack, but does not send it out of the stack System. out. println(St. Peek());System. out. println(St);Stacks the top element of the stack and returns to the System. out. println(St. Pop());System. out. println(St);}}
Three. Queue Collection
The queue collection is used to simulate queues, which are in the nature of FIFO. The basic operation of the queue is shown in the following code:
Package Lkl;import Java.util.arraydeque;import Java.util.Queue; Public classqueuetest { Public Static void Main(string[] args) {Queue Qu =NewArraydeque ();// void Add (Object e) Boolean offer (Object e) Both methods can add elements to the tail of the queueQu.add ("C + +"); Qu.add ("C Language"); Qu.add ("Java"); System. out. println (Qu); //Object peek (): Gets the team header element but does not delete the element //Object poll (): Gets the team header element and deletes the elementSystem. out. println (Qu.peek ()); System. out. println (Qu); System. out. println (Qu.poll ()); System. out. println (Qu); }}
The queue also has a Priorityqueue implementation class, which is the priority queue we usually use.
In addition, the queue has a qeque interface, the "double-ended queue", which can be added and removed from both ends, so that the Deque implementation class can be used either as a queue or as a stack. The basic operation of the Dequeu is not listed. The Deque interface provides a typical implementation class: Arraydeque, an array-based, double-ended queue implementation. The following example shows how to use a double-ended queue for the original stack, as shown in the stack usage of the previous vector implementations:
Package LKL;Import Java. Util. Arraydeque;public class Dequestacktest {public static void main (string[] args) {ArraydequeSt= new Arraydeque (); St. Push(1); St. Push(2); St. Push(3);System. out. println(St);System. out. println(St. Peek());System. out. println(St);System. out. println(St. Pop());System. out. println(St);}}
Four. LinkedList implementation class
The LinkedList implementation class is the implementation class of the list interface, which internally uses a linked list to hold the elements in the collection, so there is very good insertion and deletion performance. and the other list implementation class random access performance is better. In addition to the basic operation of the list, LinkedList also implements the Dequeu interface, so it can be used as a double-ended queue or as a stack. The following code simply demonstrates the use of the LinkedList collection:
Package Lkl;import java.util.LinkedList; Public classlinkedlisttest { Public Static void Main(string[] args) {LinkedList books =NewLinkedList ();/// Add a string to the end of the queueBooks.offer ("C + +");/// Add a string element to the top of the stackBooks.push ("Java"); ///Add string to the head of the queue (equivalent to the top of the stack)Books.offerfirst ("C Language"); for(intI=0; I<books.size (); i++) System. out. print (books.Get(i) +" "); System. out. println ();/// Access but do not delete the top element of the stackSystem. out. println (Books.peekfirst ());/// Access but not delete the last element of the queueSystem. out. println (Books.peeklast ());/// Eject the element at the top of the stackSystem. out. println (Books.pop ()); System. out. println (books);/// Access and delete the last element of the queueSystem. out. println (Books.polllast ()); System. out. println (books); }}
Java Collection Learning Collection (3)