Collection of java Collection Learning (3)
This blog mainly introduces the List set and Queue set.
1. List Set
The List set is an ordered and repeatable set. Each element in the set corresponds to an index, we can access the data in the List set through the index (in fact, it can be seen as a variable-length array ).
List, as a sub-interface of Collection, supports all Collection operations. However, because List is ordered, some new operations are added, mainly insert and delete indexes, method of replacing the element. List uses the equals () method to determine whether objects are equal. The following code is used:
Package lkl; import java. util. arrayList; import java. util. collection; import java. util. hashSet; import java. util. list; public class ListTest {public static void main (String [] args) {Collection c = new HashSet (); c. add ("java"); c. add ("c"); c. add ("c ++"); List ls = new ArrayList (); 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 to the index area in the List, the index value cannot be greater than the original List length ls. add (2, 4); System. out. println (ls); // void addAll (int index, Collection c) // inserts all the elements in set c into the index of List ls. addAll (2, c); System. out. println (ls); // Object get (int index) // returns the elements at the List index. This method can be used to traverse the List for (int I = 0; I
For List, because it is ordered, List provides an additional Traversal method lsitIterator (); this method provides the reverse iteration function, you can also add elements to the set during traversal. Compared with the Iterator interface, the method is as follows:
Boolean hasPrevious (): returns whether the set associated with the iterator has the previous element.
Object previous (): returns the previous element of the iterator.
Void add (): insert an element to a 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 class ListIteratorTest {public static void main (String [] args) {List ls = new ArrayList (); ls. add (1); ls. add (2); ls. add (3); // declare a ListIterator type iterator ListIterator lt = ls. listIterator (); // Forward Iteration Process while (lt. hasNext () {int k = (int) lt. next (); System. out. print (k + ""); // you can insert an element during iteration, but the inserted element does not affect the traversal if (k = 2. add (4);} System. out. println (); System. out. println ("The following is the reverse iteration process"); // start the reverse iteration process below while (lt. hasPrevious () {System. out. print (lt. previous () + "");} System. out. println ();}}
II. Implementation class of List set: ArrayList and Vector
ArrayList and Vector are two typical implementations of List. They fully support all the operations described above. ArrayList and Vector are both List classes based on arrays. Therefore, the ArrayList and Vector classes encapsulate a dynamic array of objects [] that allow redistribution. Arraylist and Vector objects use the initialCapacity parameter to set the length of the array. After the array is full, initialCapcity automatically increases. However, if we want to add a large number of elements to ArrayList, therefore, we can use the ecsureCapacity () method to increase initialCapacity at one time, which can reduce the allocation times and improve performance. The default Object [] array length is 10.
The usage of Vector and ArrayList is almost identical, but Vector is an ancient collection.
In addition, Vector also encapsulates a Stack subclass to simulate stacks. Its usage is as follows:
Package lkl; import java. util. stack; public class VectorStackTest {public static void main (String [] args) {// declare a Stack st = new Stack (); // add elements to the Stack st. push ("C Language"); st. push ("cpp"); st. push ("java"); System. out. println (st); // returns the top element of the stack, but does not output it out of the stack System. out. println (st. peek (); System. out. println (st); // outputs the elements at the top of the stack and returns the System. out. println (st. pop (); System. out. println (st );}}
Iii. Queue set
The Queue set is used to simulate the Queue, that is, to meet the nature of first-in-first-out. The basic operations of Queue are as follows:
Package lkl; import java. util. arrayDeque; import java. util. queue; public class QueueTest {public static void main (String [] args) {Queue qu = new ArrayDeque (); // void add (Object e) boolean offer (Object e) /// both methods can add elements to the qu at the end of the queue. add ("c ++"); qu. add ("C Language"); qu. add ("java"); System. out. println (qu); // Object peek (): gets the Header element but does not delete it. // Object poll (): gets the Header element and deletes the System element. out. println (qu. peek (); System. out. println (qu); System. out. println (qu. poll (); System. out. println (qu );}}
Queue also has a PriorityQueue implementation class, that is, the priority Queue we usually use.
In addition, the Queue also has a Qeque interface, namely, "Double-end Queue", which can be used to add and delete elements from both ends. Therefore, the Deque implementation class can be used either as a Queue or as a stack. The basic operations of Dequeu are not listed in detail. The Deque interface provides a typical implementation class: ArrayDeque, an array-based dual-end queue implementation. The following demonstrates how to use the double-end queue in the original stack. We can see that the stack usage is the same as that implemented by the previous Vector:
package lkl;import java.util.ArrayDeque;public class DequeStackTest { public static void main(String[] args){ ArrayDeque st = 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); }}
IV. Implementation class of javaslist
The shortlist implementation class is the implementation class of the List interface. It uses the linked List internally to save elements in the set. Therefore, it has excellent insertion and deletion performance. The random access performance of other List Implementation classes is better. In addition to basic List operations, tranquility List also implements the Dequeu interface. Therefore, it can be used as a dual-end queue or a stack. The following code demonstrates the usage of the collections list set:
Package lkl; import java. util. shortlist; public class into listtest {public static void main (String [] args) {shortlist books = new shortlist (); // Add the String to books at the end of the queue. offer ("c ++"); // Add the string element to the top of the stack books. push ("java"); // Add the string to the queue header (equivalent to the top of the stack) books. offerFirst ("C Language"); for (int I = 0; I