Analysis and comparison of several common containers

Source: Internet
Author: User

ArrayList uses an array form to hold the object, which puts the object in a contiguous position, so the biggest drawback is that it is cumbersome to insert and delete.
LinkedList takes objects in separate spaces and saves the next linked index in each space but the downside is that finding is very troublesome. To start with the first index


List is the interface defines some methods but the method is not implemented ArrayList is a class implementation of the list of the interface list inside the definition of the method in the ArrayList is implemented in the same time list can also be implemented by other classes such as vectors, vectors and Arra Ylist the implementation of the methods defined by list is different.




The difference between a vector and a list
Vectors allocate a contiguous address space for stored objects, so random access to the elements in the vector is highly efficient. Inserting or deleting an element in Vecotor requires that an existing element be copied and moved. If the objects stored in the vector are large, or the constructors are complex, it is expensive to copy the existing elements because the Copy object calls the copy constructor. For simple small objects, vectors are more efficient than list. Vector expands the capacity by twice times each time it expands, which is very efficient for small objects.
The objects in the list are stored in discrete, random access to an element that needs to traverse the list. Inserting elements into the list, especially at the end of the insert, is highly efficient and requires only changing the pointer of the element.
Sum up:
Vector applies: Small number of objects change, simple objects, random access elements frequently
List applies: the number of objects varies greatly, objects are complex, insertions and deletions are frequent
The biggest difference is that the list is bidirectional, and the vector is one-way.
Therefore, in the actual use, how to choose which of the three containers, should be based on your needs, should generally follow the following
Principle of:
1. If you need efficient immediate access, without the efficiency of insert and delete, use vector
2, if you need a large number of insertions and deletions, and do not care about the immediate access, you should use the list
3, if you need to immediately access, and care about the end of data insertion and deletion, you should use Deque.
Vectors represent a contiguous area of memory where each element is stored sequentially in this memory, and the random access to vectors is highly efficient, but the insertion and deletion of non-end elements is very inefficient.
Deque
Also represents a contiguous area of memory, but unlike a vector it supports the efficient insertion and deletion of elements in its header, implemented by a two-level array structure, one level represents the actual container, the second level points to the first and the end of the container

List represents a non-contiguous area of memory and is linked in two directions through a pair of pointers to the end-to-end elements, with high insertion efficiency and low random access efficiency





2

STL provides three of the most basic containers: Vector,list,deque.

The vector is similar to the built-in array, which has a contiguous memory space and the starting address is the same, so
It can be very well supported immediately, that is, the [] operator, but because its memory space is continuous, so in the middle
Inserting and deleting will result in a copy of the memory block, and when the memory space behind the array is not enough, you need to re-
Request a piece of memory that is large enough and make a copy of the memory. These have greatly affected the efficiency of vectors.

A list is a doubly linked list in a data structure (according to the SGI STL source code), so its memory space can be discontinuous
Access to data through pointers, which makes it very inefficient to access it immediately, so it
No overloads are provided for the [] operator. But due to the characteristics of the list, it can be very efficient to support the deletion of any place
and insert.

Deque is a double-ended queue, its concrete implementation is not very clear, but know that it has the following two features:
It supports the [] operator, which is supported immediately, and is similar to the efficiency of the vector, which supports both the
Operation: Push_back,push_front,pop_back,pop_front, and the efficiency of the list on both sides of the operation
also similar.

Therefore, in the actual use, how to choose which of the three containers, should be based on your needs, should generally follow the following
Principle of:
1. If you need efficient immediate access, without the efficiency of insert and delete, use vector
2, if you need a large number of insertions and deletions, and do not care about the immediate access, you should use the list
3, if you need to immediately access, and care about the end of data insertion and deletion, you should use Deque.






The difference between a vector and a list
Vectors allocate a contiguous address space for stored objects, so random access to the elements in the vector is highly efficient. Inserting or deleting an element in Vecotor requires that an existing element be copied and moved. If the objects stored in the vector are large, or the constructors are complex, it is expensive to copy the existing elements because the Copy object calls the copy constructor. For simple small objects, vectors are more efficient than list. Vector expands the capacity by twice times each time it expands, which is very efficient for small objects.
The objects in the list are stored in discrete, random access to an element that needs to traverse the list. Inserting elements into the list, especially at the end of the insert, is highly efficient and requires only changing the pointer of the element.
Sum up:
Vector applies: Small number of objects change, simple objects, random access elements frequently
List applies: the number of objects varies greatly, objects are complex, insertions and deletions are frequent
The biggest difference is that the list is bidirectional, and the vector is one-way.


Therefore, in the actual use, how to choose which of the three containers, should be based on your needs, should generally follow the following
Principle of:


1. If you need efficient immediate access, without the efficiency of insert and delete, use vector


2, if you need a large number of insertions and deletions, and do not care about the immediate access, you should use the list


3, if you need to immediately access, and care about the end of data insertion and deletion, you should use Deque.
Vector
Represents a contiguous area of memory where each element is stored sequentially in this memory, and the random access to the vector is highly efficient, but the insertion and deletion of non-end elements is very inefficient.
Deque


Also represents a contiguous area of memory, but unlike a vector it supports the efficient insertion and deletion of elements in its header, implemented by a two-level array structure, one level represents the actual container, the second level points to the first and the end of the container


List
Represents a non-contiguous area of memory and is linked in two directions through a pair of pointers to the end-to-end elements, with high insertion efficiency and low random access efficiency


2,STL provides three most basic containers: Vector,list,deque.


The vector is similar to the built-in array, which has a contiguous memory space and the starting address is the same, so


It can be very well supported immediately, that is, the [] operator, but because its memory space is continuous, so in the middle


Inserting and deleting will result in a copy of the memory block, and when the memory space behind the array is not enough, you need to re-


Request a piece of memory that is large enough and make a copy of the memory. These have greatly affected the efficiency of vectors.


List is the doubly linked list in the data structure (according to SGI
STL source code), so its memory space can be discontinuous
Access to data through pointers, which makes it very inefficient to access it immediately, so it


No overloads are provided for the [] operator. But due to the characteristics of the list, it can be very efficient to support the deletion of any place
and insert.


Deque is a double-ended queue, its concrete implementation is not very clear, but know that it has the following two features:


It supports the [] operator, which is supported immediately, and is similar to the efficiency of the vector, which supports both the


Operation: Push_back,push_front,pop_back,pop_front, and the efficiency of the list on both sides of the operation
also similar.


Therefore, in the actual use, how to choose which of the three containers, should be based on your needs, should generally follow the following
Principle of:


1. If you need efficient immediate access, without the efficiency of insert and delete, use vector


2, if you need a large number of insertions and deletions, and do not care about the immediate access, you should use the list


3, if you need to immediately access, and care about the end of data insertion and deletion, you should use Deque.






There are three important differences between the Hashtable and the HashMap class. The first difference is mainly historical reasons. Hashtable is based on the old dictionary class, HashMap is an implementation of the map interface introduced by Java 1.2.


Perhaps the most important difference is that the Hashtable method is synchronous, and the HashMap method is not. This means that while you can use a hashtable in a multithreaded application without taking any special action, you have to provide an external synchronization for a hashmap as well. A convenient way is to use the static Synchronizedmap () method of the collections class, which creates a thread-safe map object and returns it as a encapsulated object. The method of this object allows you to synchronize access to potential hashmap. The result is that when you don't need to sync, you can't cut off synchronization in Hashtable (like in a single-threaded application), and synchronization adds a lot of processing overhead.


The 3rd difference is that only hashmap can let you use NULL as a key or value for the entry of a table. Only one record in HashMap can be an empty key, but any number of entries can be empty value. This means that if a search key is not found in the table, or if a search key is found, but it is an empty value, then get () returns NULL. If necessary, use the Containkey () method to differentiate between the two cases.


Some data suggest, when need to synchronize, use Hashtable, converse with HashMap. However, because HashMap can be synchronized when needed, HashMap functions more than Hashtable, and it is not based on a stale class, it is argued that hashmap in all cases takes precedence over Hashtable.


About properties
Sometimes, you might want to use a hashtable to map the string of key to the string of value. The environment strings in DOS, WINDOWS, and Unix have some examples, such as the string path of key that is mapped to the string C:\WINDOWS of value; C:\WINDOWS\SYSTEM. Hashtables is a simple way to represent these, but Java provides another way.


The Java.util.Properties class is a subclass of Hashtable, designed for string keys and values. The use of the properties object is similar to the use of Hashtable, but the class adds two time-saving methods, which you should know.


The store () method saves the contents of a Properties object to a file in a readable form. The Load () method is exactly the opposite, used to read the file and set the Properties object to contain keys and values.


Note that because properties extends the Hashtable, you can use the put () method of the superclass to add keys and values that are not string objects. This is not advisable. In addition, if you use the store () for a Properties object that does not contain a string object, store () will fail. As an alternative to put () and get (), you should use SetProperty () and GetProperty (), which use the string parameter.

Analysis and comparison of several common containers

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.