Java HashMap and Hashtable (RPM)

Source: Internet
Author: User

Source: http://www.cnblogs.com/devinzhang/archive/2012/01/13/2321481.html

1. HashMap

1) data structure of HashMap

HashMap is a combination of arrays and linked lists (in the data structure called "chain-Table hashing"), as shown in:

When we put the element in the HashMap, it is worthwhile to place the element in the array (i.e. subscript) according to the hash of the key, and then we can put the element in the corresponding position. If there are other elements in the seat of this element, then the elements in the same seat will be stored in the form of a list, and the newly added ones are placed in the chain head, and the first ones are placed at the end of the chain.

2) Use

Map map = new HashMap ();
Map.put ("Rajib Sarma", "100");
Map.put ("Rajib Sarma", "Max")//the value "is replaced by" 200 ".
Map.put ("Sazid Ahmed", "200");

Iterator iter = Map.entryset (). Iterator ();
while (Iter.hasnext ()) {
Map.entry Entry = (map.entry) iter.next ();
Object key = Entry.getkey ();
Object val = Entry.getvalue ();
}

2. Hashtable and HashMap differences

First, inheritance is different.

public class Hashtable extends Dictionary implements Map
public class HashMap extends Abstractmap implements Map

Second

The methods in Hashtable are synchronous, and the methods in HashMap are not synchronized by default. In the context of multi-threaded concurrency, you can use Hashtable directly, but to use hashmap, you need to increase the synchronization process.

Third

In Hashtable, both key and value do not allow null values.

In HashMap, NULL can be used as a key with only one key, and one or more keys can have a value of NULL. When the Get () method returns a null value, it can indicate that the key is not in the HashMap, or that the value corresponding to the key is null. Therefore, the get () method cannot be used in HashMap to determine whether a key exists in HashMap and should be judged by the ContainsKey () method.

Four, the internal implementation of the two traversal methods is different.

Hashtable and HashMap all use the Iterator. For historical reasons, Hashtable also used the enumeration approach.

Fifth

Hash values are used differently, Hashtable directly using the object's hashcode. The hash value is recalculated by the HashMap.

Sixth

Hashtable and hashmap their two internal implementations of the array's initial size and the way the capacity is expanded. The default size of the hash array in Hashtable is 11, and the increment is old*2+1. The default size of the hash array in HashMap is 16, and must be a 2 index.

Java HashMap and Hashtable (ext.)

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.