A collection of Java (iv) vectors and stacks

Source: Internet
Author: User

Reprint please indicate source: http://www.cnblogs.com/lighten/p/7296023.html

1. Preface

This chapter describes the vector and its subclass stack in the list of Java collections. The vector class is the Java 1.0 provides a collection class, its implementation and the ArrayList explained in the last chapter almost the same, in the following will be a brief introduction (otherwise there is nothing to say), its sub-class stack is a programmer is familiar with the stack structure, characterized by the first into the back, The stack is actually simpler and will be described briefly.

2.Vector

In the preface, it is also said that vector and ArrayList are almost the same, why there are two repeating function of the class? The reason is simple, the biggest difference between vectors and ArrayList is that vectors are thread-safe, and ArrayList are thread insecure. Vector also says that it is a class provided by Java 1.0, and ArrayList is a class provided later in Java version 1.2. The reason must be clear, that is efficiency. In many cases, what we need is not a thread-safe class, such as a single-threaded or guaranteed only one thread can access the object, thread safety will not make much sense, but will cause the program to run inefficient. So, it should be up to the developer to decide whether or not thread safety is required, which is a mistake made by Java early on, and most of the provided are thread-safe classes, similar classes have StringBuffer and StringBuilder.

In addition to the above-mentioned thread-safety differences, the other is almost the same, the method definition and name are slightly different, the only difference is that the size of the expansion is not the same.

ArrayList is three times times the original, vector does not specify is twice times the original. Nothing else can be explained, iterators are also rewritten, and thread safety is implemented.

3.Stack

The stack inherits the child vector which is also thread-safe, and its implementation also draws on the vector's data structure and method. The simple one is a push operation: The last element is added to the array, and the pop operation: takes out the last element. This achieves first-in and out-of-the-way results, and a peek operation is only visible.

  

Look at the past push is not synchronized, actually called the parent class of the addelement operation is synchronized, there is no need to synchronize. The other one is a search operation, using the LastIndexOf method, the reverse traversal only.

4. Figure

The vector diagram is no longer given, and here is the operation diagram for the stack.

A collection of Java (iv) vectors and stacks

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.