[JAVA set] Vector and Stack, javavectorstack
The following content is based on the jdk1.7.0 _ 79 source code;
What is Vector and Stack?
Vector: thread-safe Dynamic Array
Stack: inherits Vector, a thread-safe Stack based on dynamic arrays;
Vector and Stack features
The Vector and ArrayList are basically the same. The difference is that the Vector is thread-safe, and the synchronized keyword is added before a thread-safe method;
Vector: Fast random access, poor insertion and removal performance (array features); Support for null elements; sequential; repeated elements; thread safety;
Stack, not a stack );
Vector and Stack inherited classes and Implemented interfaces
The Iterable interface, Collection interface, List interface, RandomAccess, Cloneable, java. io. for details about the Serializable interface, AbstractCollection class, And AbstractList class, refer to the previous article: ArrayList in JAVA, which is basically the same. The remaining Vector class and Stack class are as follows:
Vector
Similar to ArrayList, see ArrayList in JAVA. The main differences are as follows:
1. Vector is thread-safe.
2. The growth of ArrayList is 0.5 times by default, and the growth of Vector capacityIncrement can be configured. If not specified (<= 0), the size doubles. The source code is as follows:
private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; int newCapacity = oldCapacity + ((capacityIncrement > 0) ? capacityIncrement : oldCapacity); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); elementData = Arrays.copyOf(elementData, newCapacity); }
3. Others. If the constructor methods are inconsistent, the Vector can initialize capacityIncrement through the constructor. In addition, there are other methods, such as the indexOf method. The Vector supports searching from the specified position. In addition, vector also has some redundant methods with repeated functions, such as addElement and setElementAt. This is due to historical reasons. For example, the addElement method is legacy. When the collection framework is introduced, vector is added to the Collection family and changed to implement the List interface. Some methods defined in the List interface need to be implemented, so there are some functional redundancy methods;
Stack
The basic stack operations are implemented. The method is as follows:
Public Stack ();
Create an empty Stack
Public synchronized E peek ();
Returns the value at the top of the stack;
Public E push (E item );
Inbound stack operation;
Public synchronized E pop ();
Out-of-stack operations;
Public boolean empty ();
Determines whether the stack is empty;
Public synchronized int search (Object o );
Returns the position of the object in the stack;
Examples of using Vector and Stack
Some methods in the Vector are used as follows. In addition, the Traversal method of the Vector is the same as that of the ArrayList. You can use foreach, iterator, and for loop traversal;
Package com. pickers. basis. col; import java. util. arrays; import java. util. iterator; import java. util. list; import java. util. listIterator; import java. util. vector; public class Test {public static void main (String [] args) {Vector <Integer> vector = new Vector <Integer> (); for (int I = 0; I <10; I ++) {vector. add (I);} // print System directly. out. println (vector. toString (); // size () System. out. println (vector. size (); // contains System. out. println (vector. contains (2); // iterator Iterator <Integer> iterator = vector. iterator (); while (iterator. hasNext () {System. out. print (iterator. next () + "");} // toArray Object [] objArr = vector. toArray (); System. out. println ("\ nobjArr:" + Arrays. asList (objArr); Integer [] intArr = vector. toArray (new Integer [vector. size ()]); System. out. println ("intArr:" + Arrays. asList (intArr); // add vector. add (5); // remove vector. remove (5); System. out. println (vector); // containsAll System. out. println (vector. containsAll (Arrays. asList (5, 6); // addAll vector. addAll (Arrays. asList (555,666); System. out. println (vector); // removeAll vector. removeAll (Arrays. asList (555,666); System. out. println (vector); // addAll method vector. addAll (5, Arrays. asList (666,666, 6); System. out. println (vector); // get method System. out. println (vector. get (5); // set method vector. set (5, 55); System. out. println (vector. get (5); // add method vector. add (0,555); System. out. println (vector); // remove Method vector. remove (0); System. out. println (vector); // indexof method System. out. println (vector. indexOf (6); // lastIndexOf method System. out. println (vector. lastIndexOf (6); // listIterator method ListIterator <Integer> listIterator = vector. listIterator (); System. out. println (listIterator. hasPrevious (); // listIterator (index) method ListIterator <Integer> iListIterator = vector. listIterator (5); System. out. println (iListIterator. previous (); // subList method System. out. println (vector. subList (5, 7); // clear vector. clear (); System. out. println (vector );}}
Some of the methods in Stack are used as follows. Because Stack inherits Vector, the methods that can be used by Vector can also be used by Stack. The following lists some examples of Stack-specific methods, which are very simple, it is some basic operations of the stack. In addition to several traversal methods of Vector, the stack also has its own unique method of traversing elements:
Package com. pickers. basis. col; import java. util. stack; public class Test {public static void main (String [] args) {Stack <Integer> stack = new Stack <Integer> (); for (int I = 0; I <10; I ++) {stack. add (I);} System. out. println (stack); System. out. println (stack. peek (); stack. push (555); System. out. println (stack); System. out. println (stack. pop (); System. out. println (stack); System. out. println (stack. empty ()) ; System. out. println (stack. search (6); System. out. println ("stack traversal:"); while (! Stack. empty () {System. out. print (stack. pop () + "");}}}
Suggestions
Vector is thread-safe, but has poor performance. Generally, ArrayList is used unless otherwise specified;
If you plan to use the Stack as the stack, you will be able to strictly follow several Stack operations. Otherwise, the meaning of using the stack is better than using the Vector;