Java Study Notes: go deep into Java HashMap

Source: Internet
Author: User

What is HashMap?
HashMap is a non-synchronous implementation based on the Map interface of the hash table. This implementation provides all optional ing operations and allows the use of null values and null keys. This class does not guarantee the order of mappings, especially it does not guarantee that the order remains unchanged.
Java HashMap
Java's HashMap consists of two data structures: An array and a linked list.


When a HashMap is created, an array structure will be created, but the length of the array structure is limited. For example, if it is a 1024 length, can we only place 1024 pieces of data? No. The length of 1024 is only a basic hash data value for the value passed into the hashmap. No matter how long the length is, there will always be data that will overlap with the previous data on the same Array key. How can this problem be solved?

The first method is the open address method. The method of the open address method is to find an empty unit data grid based on a certain value in case of a conflict. What should I do if the entire data link is searched once and occupied? At this time, the local method will store data in the previously opened buffer area.

The second method is the linked list method. The advantage of the linked list is that the previous data records the memory address of the following data. In this way, after a conflict exists, you can add a data record to the linked list, when querying, you only need to traverse the table until the query finds the location.

Therefore, the greater the key value of the array, the smaller the probability of hash conflict.

Sina Weibo uses the open address method and memcache uses the linked list method.
I used nodejs to write a hashmap instance.
Java has two hashmaps: hashMap and TreeMap. The difference between the two is that HashMap uses hashcode to quickly search its content, while all elements in TreeMap maintain a fixed order, if you need to get an ordered result, you should use TreeMap (the arrangement order of elements in HashMap is not fixed ).

Java hashmap usage:

@ RequestMapping (value = "/test ")
@ ResponseBody
Public String test (){
Map map = new HashMap (); // instantiate a hashmap
Map. put ("username", "initphp"); // Add a new data
Map. put ("password", "test ");
Map. put ("age", "100 ");
Map. put ("age1", "age1 ");
Map. put ("age2", "age2 ");
If (map. containsKey ("username") = true) {// determines whether the key exists.
System. out. println ("key exists ");
}
System. out. println ("username:" + map. get ("username"); // get a data
System. out. println ("password:" + map. get ("password "));
Map. remove ("age"); // delete an age data
System. out. println ("age:" + map. get ("age "));
System. out. println ("Length:" + map. size (); // The length of the entire hashmap data

// Cyclic output:
Iterator iterator_1 = map. keySet (). iterator ();
System. out. println ("list:"); // The length of the entire hashmap data
While (iterator_1.hasNext ()){
Object key = iterator_1.next ();
System. out. println ("tab. get (key) is:" + map. get (key ));
}
Map. clear (); // clear the entire hash map
Return "test ";
}

Result:

 

Hashmap and list seem to have similar usage, but there is a big difference. One is the hash mode and the other is the list mode.
List is suitable for some list and ordered data. Hashmap is more suitable for the data structure of k => v.
Hashmap can be found to be unordered.

Java TreeMap usage

@ RequestMapping (value = "/test ")
@ ResponseBody
Public String test (){
TreeMap map = new TreeMap (); // instantiate a hashmap
Map. put ("username", "initphp"); // Add a new data
Map. put ("password", "test ");
Map. put ("age", "100 ");
Map. put ("age1", "age1 ");
Map. put ("age2", "age2 ");
If (map. containsKey ("username") = true) {// determines whether the key exists.
System. out. println ("key exists ");
}
System. out. println ("username:" + map. get ("username"); // get a data
System. out. println ("password:" + map. get ("password "));
Map. remove ("age"); // delete an age data
System. out. println ("age:" + map. get ("age "));
System. out. println ("Length:" + map. size (); // The length of the entire hashmap data

// Cyclic output:
Iterator iterator_1 = map. keySet (). iterator ();
System. out. println ("list:"); // The length of the entire hashmap data
While (iterator_1.hasNext ()){
Object key = iterator_1.next ();
System. out. println ("tab. get (key) is:" + map. get (key ));
}
Map. clear (); // clear the entire hash map
Return "test ";
}

Result:

 

The results except the list are the same as those of HashMap.
TreeMap is an ordered list, sorted by the letters a, B, and c of the key.

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.