"Java Collection series three" Vector-stack parsing

Source: Internet
Author: User

2017-07-29 12:59:14

I. INTRODUCTION 1, Vector inheritance relationship

2, Vector class expansion

Vector classes are very similar in implementation to ArrayList, using array storage elements, but the scaling strategy is different, ArrayList is basically scaled up to 1.5 times times, vectors are incremented according to the Capacityincrement value set when the Vector object was created, and if the value is not set or 0, then the direct double, as follows:

3. Vector Thread Safety

The vector is called thread-safe, and its security comes from the addition of the Synchronized keyword to almost every method, because the lock object is the current object, as long as there is a thread in the execution of a lock method, that is, to obtain the current object this lock object, No other thread can use this vector object anymore ... This is a very brutal design, it is estimated that this may be the first time synchronized out of the JDK team of the excitement of it ~

For a few examples:

Even if there are some methods that do not directly add the Synchronized keyword, at least the method that it calls is added.

4. Stack

Stack inherits from the vector, provides the pop, push, peek, search and other stacks of special operations, the source code is as follows:

1  PackageJava.util;2 3 /**4 * The <code>Stack</code> class represents a Last-in-first-out5 * (LIFO) stack of objects. It extends class <tt>Vector</tt> with five6 * Operations-a vector to be treated as a stack. The usual7 * <tt>push</tt> and <tt>pop</tt> operations is provided, as well as a8 * method to <tt>peek</tt> in the top item on the stack, a method to test9 * For whether the stack was <tt>empty</tt>, and a method to <tt>search</tt>Ten * The stack for a item and discover how far it was from the top. One * <p> A * When a stacks is first created, it contains no items. -  * - * <p>a more complete and consistent set of LIFO stacks operations is the * provided by the {@linkDeque} interface and its implementations, which - * should is used in preference to this class. For example: - * <pre> {@code - * deque<integer> stack = new arraydeque<integer> ();} </pre> +  * -  * @authorJonathan Payne +  * @sinceJDK1.0 A  */ at  Public - classStack<e>extendsVector<e> { -     /** - * Creates an empty Stack. -      */ -      PublicStack () { in     } -  to     /** + * Pushes an item onto the top of this stack. this have exactly - * The same effect as: the * <blockquote><pre> * * AddElement (item) </pre></blockquote> $      *Panax Notoginseng      * @paramitem The item to is pushed onto this stack. -      * @returnThe <code>item</code> argument. the      * @seejava.util.vector#addelement +      */ A      Publice push (E item) { the addelement (item); +  -         returnitem; $     } $  -     /** - * Removes the object at the top of this stack and returns that the * Object as the value of this function. -      *Wuyi      * @returnThe object at the top of this stack ( the last item the * of the <tt>Vector</tt> object). -      * @throwsemptystackexception If this stack is empty. Wu      */ -      Public synchronizedE Pop () { About E obj; $         intLen =size (); -  -obj =Peek (); -Removeelementat (len-1); A  +         returnobj; the     } -  $     /** the * Looks at the object on the top of this stack without removing it the * from the stack. the      * the      * @returnThe object at the top of this stack ( the last item - * of the <tt>Vector</tt> object). in      * @throwsemptystackexception If this stack is empty. the      */ the      Public synchronizedE Peek () { About         intLen =size (); the  the         if(len = = 0) the             Throw Newemptystackexception (); +         returnElementAt (len-1); -     } the Bayi     /** the * Tests If this stack is empty. the      * -      * @return<code>true</code> if and if this stack contains - * no items; <code>false</code> otherwise. the      */ the      Public Booleanempty () { the         returnSize () = = 0; the     } -  the     /** the * Returns The 1-based position where an object is on this stack. the * If The object <tt>o</tt> occurs as an item in this stack, this94 * method returns the distance from the top of the stack of the the * Occurrence nearest the top of the stack; the topmost item on the the * Stack is considered to being at distance <tt>1</tt>. The <tt>equals</tt> the * method is used-compare <tt>o</tt> to the98 * items in this stack. About      * -      * @paramo the desired object.101      * @returnThe 1-based position from the top of the stack where102 * The object is located; The return value <code>-1</code>103 * Indicates that the object isn't on the stack.104      */ the      Public synchronized intsearch (Object o) {106         inti =lastIndexOf (o);107 108         if(I >= 0) {109             returnSize ()-i; the         }111         return-1; the     }113  the     /**Use serialversionuid from JDK 1.0.2 for interoperability*/ the     Private Static Final LongSerialversionuid = 1224463164541339165L; the}
View Code5. End

The similarities and differences between Vector and ArrayList:

1, the expansion strategy is different;

2, thread-safe;

3, to provide some unique methods such as removeelement ();

4, the realization of other methods and ArrayList is almost the same, after all, is an array of operations;

5, ignore this class can be;

"Java Collection series three" Vector-stack parsing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.