The difference of Hashmap,hashtable,linkedhashmap,treemap __java

Source: Internet
Author: User
1. Map Introduction

The Map collection class is used to store element pairs (called "Keys" and "values"), where each key maps to a value. The map is used to store key-value pairs, resulting in values based on the key, so that key duplication is not allowed and values can be duplicated. 1.1. HashMap

HashMap is the most commonly used map, which stores data according to the key's hashcode value, and can obtain its value directly according to the key, with fast access speed. HashMap allows only one record key to be null, allowing multiple records to have a null value; HashMap does not support thread synchronization, where multiple threads can write HashMap at any one time, and may result in inconsistent data.  If you need to sync, you can use the collections Synchronizedmap method to make HashMap have the ability to synchronize. 1.1.1. HashMap example

Hashmaptest.java

Package map;

 

Import Java.util.HashMap;

Import Java.util.Set;

 

Publicclass hashmaptest {

 

    publicstaticvoid main (string[] args) {

 

       

       hashmap<string, string> HashMap = new Hashmap<string,string> ();

       

       Hashmap.put (null, NULL);

       Hashmap.put ("AA", null);

       Hashmap.put ("BB", "123");

       Hashmap.put ("DD", "456");

       Hashmap.put ("CC", "789");

       

       Set<string> keyset = Hashmap.keyset ();

       

       for (String key:keyset) {

           System.out.println (key+ ": +hashmap.get (Key));}}}

 



The results of the operation are as follows:

Null:null

dd:456

Aa:null

Bb:123

cc:789

It can be seen from the result that the value stored in the HashMap can be either Null,key or null, and the result of the output is unordered 1.2. Linkedhashmap

Linkedhashmap is also a hashmap, but the internal maintenance of a two-way linked list, you can keep the order 1.2.1. Linkedhashmap instance

Linkedhashmaptest

Package map;

 

Import Java.util.LinkedHashMap;

Import Java.util.Map;

Import Java.util.Set;

 

Publicclass linkedhashmaptest {

 

    publicstaticvoid main (string[] args) {

       //TODO auto-generated method stub

 

       map<string, string> map = new linkedhashmap<string, string> ();

 

       Map.put (null, NULL);

       Map.put ("AA", null);

       Map.put ("BB", "123");

       Map.put ("DD", "456");

       Map.put ("CC", "789");

 

       Set<string> keyset = Map.keyset ();

 

       for (String key:keyset) {

           System.out.println (key + ":" + map.get (key));}}

 



Run Result:

Null:null

Aa:null

Bb:123

dd:456

cc:789

From the results, it can be found that the LINKEDHASHMAP output is consistent with the order of the input. 1.3. TreeMap

TreeMap can sort the keys and values into the inside. 1.3.1. treemap example

Treemaptest

Package map;

 

Import Java.util.Map;

Import Java.util.Set;

Import Java.util.TreeMap;

 

Publicclass treemaptest {

 

    publicstaticvoid main (string[] args) {

 

       map<string, string> Map = new treemap< String, string> ();

 

       Map.put (null, NULL);

       Map.put ("AA", null);

       Map.put ("BB", "123");

       Map.put ("DD", "456");

       Map.put ("CC", "789");

 

       Set<string> keyset = Map.keyset ();

 

       for (String key:keyset) {

           System.out.println (key + ":" + map.get (key));}}

 



Run Result:

Aa:null

Bb:123

cc:789

dd:456

It can be seen from the result that key is not allowed in treemap, and the result of output is sorted by nature.   1.4. HashTable 1.4.1. The difference between HashMap and Hashtable

1. HashMap is not thread-safe, and the Hashtable method is synchronize and thread-safe. If you need to sync, you can use Collections.synchronizedmap (HashMap map) Method enables HashMap to have the ability to synchronize.

2. Hashtable does not allow record keys or values to be null; it supports thread synchronization, where only one thread can write hashtable at any one time, however, this also causes Hashtable to be slower when writing.

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.