[Convert] the difference between hashmap and hashtable, and the relationship between hashmap and hashset

Source: Internet
Author: User
Tags comparable

Transferred from:

Http://blog.csdn.net/wl_ldy/article/details/5941770

 

Hashtable is widely used. hashmap is a class used to replace hashtable in the new framework. In other words, hashmap is recommended instead of hashtable. Maybe you think hashtable is very useful. Why not? Here we will briefly analyze their differences.

I. Differences between hashmap and hashtable
1. The hashtable method is synchronous. Synchronized is used before the method, and hashmap is not synchronized. Therefore, manual synchronization is required for multithreading.

The difference between hashmap is like that between vector and arraylist.

 

[XHTML]View plaincopy
  1. You can use
  2. Map M = collections. synchronizedmap (New hashmap (...));
  3. Synchronize hashmap.


2. hashtable does not allow null values (neither key nor value). hashmap allows null values (both key and value ).

3. hashtable has the same contains (object Value) function as containsvalue (object value.

4. hashtable uses enumeration for traversal, and hashmap uses iterator for traversal.

 
  1. Hashtable traversal:
  2. Hashtable ht = new hashtable ();
  3. Ht. Put ("1", "111 ");
  4. Ht. Put ("2", "333 ");
  5. Ht. Put ("1", "222 ");
  6. Enumeration en = Ht. Keys ();
  7. While (EN. hasmoreelements ()){
  8. Object key = en. nextelement ();
  9. Object value = Ht. Get (key );
  10. System. Out. println ("Key =" + key + ", value =" + value );
  11. }

 

Reference for hashmap traversal: http://blog.csdn.net/wl_ldy/archive/2010/10/13/5939552.aspx
The above is just a difference on the surface, and their implementation is also very different.

5. The default size of the hash array in hashtable is 11, and the increase is in the old * 2 + 1 mode. The default size of the hash array in hashmap is 16, and it must be an index of 2.

6. For different hash values, hashtable directly uses the hashcode of the object. The Code is as follows:
Int hash = key. hashcode ();
Int Index = (hash & 0x7fffffff) % tab. length;
Hashmap recalculates the hash value and replaces the modulo:
Int hash = hash (k );
Int I = indexfor (hash, table. Length );

Static int Hash (Object X ){

H ^ = (H >>> 20) ^ (H >>>> 12 );
Return H ^ (H >>> 7) ^ (H >>> 4 );
}
Static int indexfor (int h, int length ){
Return H & (length-1 );
}
The above are just some of the outstanding differences.

Ii. Relationship between hashmap and hashset

1. hashset is implemented using hashmap at the underlying layer:

 
  1. Public hashset (){
  2. Map = new hashmap <E, Object> ();
  3. }

 

2,

When the hashset add method is called, a key-value pair is added to the hashmap. The key of the row is the object added to the hashset, the value of this row is a constant of the object type.

 

[XHTML]View plaincopy
  1. Private Static final object present = new object ();
  2. Public Boolean add (E ){
  3. Return map. Put (E, present) = NULL;
  4. }
  5. Public Boolean remove (Object O ){
  6. Return map. Remove (o) = present;
  7. }

 

Iii. Are the properties classes thread-safe?

 

Java. util. properties inherits from Java. util. hashtable, so properties is thread-safe.
The properties class indicates a persistent property set. properties can be saved in a stream or loaded from a stream. Each key in the attribute list and its corresponding value are a string.

 

The list interface is used to expand the collection. Its specific implementation classes are commonly used arraylist and javaslist. You can put everything in a list container and retrieve it as needed. Arraylist can be seen from its name that it is stored in an array-like form, so its random access speed is extremely fast, and the internal implementation of the arraylist is a linked list, it is suitable for frequent insert and delete operations in the center of the linked list. You can select an application as needed. The iterator mentioned above can only traverse the container forward, while the listiterator inherits the iterator idea and provides a bidirectional Traversal method for the list.

The Set interface is also an extension of the collection, but different from the list, the object elements in the set cannot be repeated, that is, you cannot put the same thing twice into the same set container. Its common implementations include the hashset and treeset classes. Hashset can quickly locate an element, but you need to implement the hashcode () method for the objects you put into the hashset. It uses the hash code algorithm mentioned above. Treeset stores the elements in the Set in order, which requires that the objects in the set are sortable. This uses the other two aggregation classes comparable and comparator provided by the set framework. A class can be sorted, and it should implement the comparable interface. If multiple classes have the same sorting algorithm, you do not need to define the same Sorting Algorithm for each class. You only need to implement the comparator interface. There are two useful public classes in the Collection framework: collections and arrays. Collections provides some useful methods for sorting, copying, searching, and filling a collection container. arrays performs similar operations on an array.


Map is a container that associates key objects and value objects, and a value object can be a map, and so on. In this way, a multi-level ing can be formed. For key objects, similar to set, key objects in a map container are not allowed to be repeated to maintain consistency of the search results. If there are two key objects, the problem occurs when you want to get the value object corresponding to the key object. Maybe what you get is not the value object you want, and the result will be messy, therefore, the uniqueness of keys is very important and also conforms to the set nature. Of course, during use, the value object corresponding to a key may change. At this time, the modified value object will correspond to the key. There is no uniqueness requirement for value objects. You can map any number of keys to a value object without any problems (however, it may cause inconvenience to your use, you don't know whether you get the value object corresponding to that key ). MAP has two common implementations: hashmap and treemap. Hashmap also uses the hash algorithm to quickly find a key. treemap stores the key in order, so it has some extended methods, such as firstkey (), lastkey (), etc. You can also specify a range from the treemap to obtain its submap. The association between keys and values is very simple. You can associate a key with a value object using the pub (Object key, object Value) method. You can use get (Object key) to obtain the value object corresponding to this key object.

[Convert] the difference between hashmap and hashtable, and the relationship between hashmap and hashset

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.