Similarities and differences of arrays, vectors, ArrayList and HashMap in Java

Source: Internet
Author: User
Tags array arrays functions integer interface object object access
Array

Array (array) and vector are very similar Java artifacts (constructs), which are completely different and should be determined according to their functions when choosing to use them.

1, array: Java arrays elements can not be subscript out of bounds, to a large extent, to ensure the security of Java programs, and other languages appear this problem often lead to disastrous consequences.
Array can hold object and basic data types, but you must specify the size of the array when you create it, and you cannot change it. It is worth noting that when one element of an array is in OBJRCT reference, Java does not invoke the default constructor, but instead sets its initial value to NULL, which is the same as the rule that Java assigns default values to various types of data, as well as to basic data types.

2, Vector: Compared to array, when more elements are joined to exceed their capacity, the size of the vector will grow dynamically, and array capacity is dead. At the same time, when vector deletes some elements, all the elements whose subscript is greater than the deleted element are moved forward in turn, and the new subscript is smaller than the original. Note: When the size () method of the vector is invoked, the number of actual elements in the vector is returned.
The vector is actually implemented as an array, and the element is accessed by an integer index of the element, but it only holds the Java.lang.Object object and cannot be used to hold the basic type data, such as to hold an integer 10, with the new Integer (10) Construct an integer wrapper class object and put it in again. When the number of elements in the vector changes, the internal array must be reassigned and copied, so this is a bit of an efficiency issue to consider.
Vetor also implements the list interface, so it can also count as colletion, but it is also special in: The Vector is synchronized. That is, the Vetor object itself realizes the synchronization mechanism.

3, ArrayList: The realization of the list interface, functions and vetor, just as there is no synchronization mechanism, of course, the access mode of elements to inherit from the list, can store any type of object.

4, HashMap: Inherited map interface, the implementation of the keys to store and access Values,keys and values can be null, it and the Hashtable class is different from the Hashtable class keys can not be null, And Hashtable class has synchronization mechanism control, and HashMap class does not.
In the Struts class library implements a Lablevaluebean, uses the lable (Key) to store and accesses the value, is very convenient.




Related Article

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.