Differences and relationships between arraylist, vector, hashmap, hashset, and hashtable

Source: Internet
Author: User

Overall architecture diagram:


Looking at the frame chart above, grasp its trunk, namely collection and map.

1 collection is an interface that is a highly abstract set that contains basic operations and attributes of the set.

Collection contains the list and set branches.
(01) list is an ordered queue. Each element has its index. The index value of the first element is 0.
The List Implementation classes include struct list, arraylist, vector, and stack.

(02) set is a set that does not allow repeated elements.
Set implementation classes include hastset and treeset. Hashset depends on hashmap, which is actually implemented through hashmap; treeset depends on treemap, which is actually implemented through treemap.

2 map is a ing interface, that is, key-value pairs. Each element in map contains "one key" and "value corresponding to the key ".

Abstractmap is an abstract class that implements most APIs in the map interface. Hashmap, treemap, and weakhashmap all inherit from abstractmap.
Although hashtable inherits from dictionary, it implements the map interface.


From the architecture of arraylist and vector, we can see that both arraylist and vector implement the list interface.
List is an ordered queue, and each element has its index. The index value of the first element is 0.
The List Implementation classes include struct list, arraylist, vector, and stack.

A two-way linked list inherited from abstractsequentiallist. It can also be operated as a stack, queue, or double-end queue.
The listlist interface can be used to operate queues.
Consumer List implements the deque interface, which can be used as a dual-end queue.
Javaslist implements the cloneable interface, which overwrites the function clone () and can be cloned.
The serialize List implements the java. Io. serializable interface, which means that the serialize List supports serialization and can be transmitted through serialization.
The synchronized list is not synchronous.
****************
Arraylist is an array queue, which is equivalent to a dynamic array. Compared with the array in Java, its capacity can dynamically increase. It inherits from abstractlist and implements the list, randomaccess, cloneable, java. Io. serializable interfaces.
Arraylist inherits abstractlist and implements list. It is an array queue that provides functions such as adding, deleting, modifying, and traversing.
Arraylist implements the randmoaccess interface, which provides the random access function. Randmoaccess is implemented by list in Java to provide quick access for list. In arraylist, we can quickly obtain element objects through the sequence number of the element. This is fast random access. Later, we will compare the efficiency of "quick Random Access" and "access via iterator" in the list.
Arraylist implements the cloneable interface, which overwrites the clone () function and can be cloned.
Arraylist implements the java. Io. serializable interface, which means that arraylist supports serialization and can be transmitted through serialization.
******************
Vector is a vector queue. It is a class added to JDK. It inherits from abstractlist and implements the list, randomaccess, and cloneable interfaces.
Vector inherits abstractlist and implements list. Therefore, it is a queue that supports related functions such as adding, deleting, modifying, and traversing.
Vector implements the randmoaccess interface, which provides the random access function. Randmoaccess is implemented by list in Java to provide quick access for list. In vector, we can quickly obtain element objects through the sequence number of the element. This is fast random access.
Vector implements the cloneable interface, that is, the clone () function. It can be cloned.
******************
Stack is a stack. Its features are: first in last out (Filo, first in last out ).
The stack in Java toolkit inherits from vector (vector Queue). Because vector is implemented through arrays, this means that stack is also implemented through arrays rather than linked lists. Of course, we can also use the consumer list as a stack! In the "Java Collection series 06 detailed introduction of vector (source code parsing) and examples of use", we have already introduced the data structure of Vector in detail. Here we will not describe the data structure of the stack.

Unlike arraylist, operations in a vector are thread-safe.
The difference between arraylist and vector is that: 1 is extended, the arraylist is changed to (150% + 1), and the vector is changed to (200% ).
2arraylist is NOT thread-safe, while vector is thread-safe;

Hashmap and hashset1hashmap implement the map interface, while hashset implements the set interface;
2hashmap stores (Key, value), and hastset stores only one key. In fact, it is more accurate to store one (Key, O ), O is a member variable of the object type in the hashset;
The essence of hashset is a set of "No repeated elements", which is implemented through hashmap. Hashset contains a "hashmap-type member variable" map. The operation functions of hashset are actually implemented through map.
3. When adding elements, hashmap uses put (Key, value) and hashset uses add (key );

Neither of them is thread-safe.
Neither of them has repeated elements. What is repetition? It is equal. You can see what is equal in the Collection class:
About hashcode and equals

Hashmap and hashtable1hashmap are non-synchronous while hashtable is synchronous;
The key and value of 2hashmap are also null, and the key and value of hashtable cannot be null;


References: http://www.cnblogs.com/wanlipeng/archive/2010/10/21/1857791.html

Http://www.cnblogs.com/skywang12345/p/3308498.html

The second blog is the directory of relevant articles in the Java Collection. This note references articles in the directory many times. Here we only list the directories. You must take a look. The authors thoroughly analyzed the collection classes.



Differences and relationships between arraylist, vector, hashmap, hashset, and hashtable

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.