Differences between hashmap and hashtable

Source: Internet
Author: User
Guidance:

1 hashmap is NOT thread-safe

Hastmap is a sub-interface of the map interface. It is an object that maps keys to values. The key and value types are all objects and cannot contain duplicate keys, but can contain duplicate values. Hashmap allows null key and null value, but hashtable does not.

2 hashtable is a collection of thread security.

Hashmap is a lightweight Implementation of hashtable (non-thread-safe implementation). They all complete the map interface. The main difference is that hashmap allows null key values ), because of non-thread security, the efficiency may be higher than that of hashtable.
Hashmap allows null as the key or value of an entry, whereas hashtable does not.
Hashmap removes the contains method of hashtable and changes it to containsvalue and containskey. The contains method is easy to misunderstand.
Hashtable inherits from the dictionary class, while hashmap is an implementation of the map interface introduced by java1.2.
The biggest difference is that the hashtable method is synchronize, but hashmap is not. When multiple threads access hashtable, they do not need to implement synchronization for their own methods, hashmap must provide external synchronization for it.
Hash/rehash used by hashtable and hashmapAlgorithmSo the performance will not be significantly different.

Public static void main (string ARGs [])
{
Hashtable H = new hashtable ();
H. Put ("user 1", new INTEGER (90 ));
H. Put ("user 2", new INTEGER (50 ));
H. Put ("user 3", new INTEGER (60 ));
H. Put ("user 4", new INTEGER (70 ));
H. Put ("user 5", new INTEGER (80 ));
Enumeration E = H. Elements ();
While (E. hasmoreelements ()){
System. Out. println (E. nextelement ());
}

Map Method:

Clear () deletes all mappings from the map.

Remove (Object key) deletes keys and associated values from map

Put (Object key, object Value) associates the specified value with the specified key

Get (Object key) returns the value associated with the specified key

Containskey (Object key) returns true if map contains the ing of the specified key.

 

 

Containsvalue (object Value) If this map maps one or more keys to a specified value, true is returned.

Isempty () If map does not contain key-value ing, true size () is returned and the number of key-value ing in map is returned.

These representJavaThe set in, here mainly from the order of its elements, whether it can be repeat for different memory, so that the appropriate use, of course, there are synchronization differences, see the previous article relatedArticle.

Ordered or not

Allow repeated Elements

Collection

No

Yes

List

Yes

Yes

Set

Abstractset

No

No

Hashset

treeset

Yes (ordered by Binary Trees)

Map

Abstractmap

NO

use key-value to map and store data, key must be unique, value repeatable

hashmap

Treemap

Yes (Binary Tree sorting)

 

 

 

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.

 

From: http://blog.csdn.net/ago52030/archive/2008/04/12/2287041.aspx

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.