[Java language] HashMap, HashSet, hashtable, vector, ArrayList

These commonly used but easily confused concepts are derived from Java Util package. This paper only makes a shallow analysis of several classes. (this article is based on jdk1.7, and the source code is from openjdk1.7.)

Collection and map

It has nothing special to do with the two. However, the two are still mixed together in this article:

First, HashSet, HashMap and hashtable look so alike that there is no reason not to write them together. Second, HashSet is a set. In fact, it is a map. List

When the generic type of the class inheriting list is integer, note that it is different from other types. (because index is integer)

Thread safety

Simply put, list is a one-dimensional array. The difference between vector and ArrayList is:

Vector is thread safe, but ArrayList is not. From the source code, we can see that all kinds of synchronized in vector, even the size attribute, are madly synchronized.

Similarly, the iterator specifies the vector and ArrayList. If there is a change in the middle, a concurrentmodificationexception will be thrown. Not only the list, but the iterator in many places has this exception,

Self increasing size

ArrayList cannot set self increment size, but it can still self increment. Vector can set the self increasing size.

Autoincrement function of ArrayList:

Self increasing function of vector:

ArrayList auto increment function:

Therefore, it can be simply considered that the length of vector is doubled by default, and the length of ArrayList is half by default.

Similarly, the analysis of their source code: the vector can set the self increasing size, while the ArrayList cannot (without this construction method).

Finally, you can list list = new ArrayList < > ();, Then list = new vector < > ();, Using the base class list, you can quickly assign ArrayList and vector.

Map

Map can be regarded as a simple "key value" database, such as memcached and redis.

Hashtable is based on directory. There is a saying that directory is outdated, so map is more recommended.

Thread safety

Hashtable is also thread safe. Like vector, even the size function is madly synchronized.

HashMap is not thread safe. Of course, it can also be synchronized with other black technologies, such as synchronized map and concurrent HashMap. Later

Null value

HashMap allows null values in both key and value. For example:

Hashtable does not allow null values in any k / V, but allows empty strings.

Direct error reporting: Java lang.NullPointerException。 This is related to the internal implementation. Hashtable needs to use hashcode, so you must ensure that the K / V is not null.

The relevant code is written in the source code:

Set

HashSet is essentially a collection, similar to list. It is a list / collection, not a K-V map, but it is a HashMap

In this way, it may be easier to understand: the HashSet is a collection of "classes" externally. In fact, a HashMap is maintained internally for implementation.

In fact, two are stored: hashcode and the class itself (string / custom class, etc.).

When adding a HashSet, the hashcode will be verified first: (adding a HashSet is actually a put operation on a map)

An operation on the custom class of HashSet:

The code comes from the detailed explanation of HashSet in Java

It looks like it will return true, but it actually returns false. Because the HashSet standard for judging the equality of two objects requires that the hashcode () return values of the two objects are equal, in addition to returning true through the equals () method comparison. The above program does not override the hashcode () method of the name class. The hashcode () return values of the two name objects are not the same, so HashSet will treat them as two objects, so the program returns false. If you want to return true, you need to override the equals method and hashcode method.

As for hashcode, it is another article that explains it clearly.

Collection operation

Set is actually easier to do collection operations. For example:

Output:

Reference documents

Equals method and hashcode method in HashSet

《 http://bbs.it-home.org/thread-53825-1-1.html?ref=myread 》

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>