Java Study Notes 4-Comparison of HashMap, LinkedHashMap, and TreeMap

Source: Internet
Author: User

Commonalities: HashMap, LinkedHashMap, and TreeMap all belong to Map. Map is mainly used to store key-value pairs and obtain values based on keys. Therefore, duplicate keys are not allowed, the allowed values are repeated. Differences: 1. the key-value pairs stored in HashMap are random during retrieval, which is also our most commonly used Map. it stores data based on the HashCode value of the key, and can directly obtain its value based on the key, with fast access speed. HashMap is the best choice for inserting, deleting, and locating elements in a Map. 2. TreeMap obtains the sorted key-value pairs. However, if you want to traverse keys in the natural or custom order, it is better to use TreeMap. 3. linkedHashMap is a subclass of HashMap. If the output sequence is the same as that of the input, you can use LinkedHashMap. (Application Scenario: shopping cart, etc.) code example: [java] package com. alibaba. sample. petstore. web. store. module. screen; import java. util. hashMap; import java. util. iterator; import java. util. linkedHashMap; import java. util. map; import java. util. map. entry; import java. util. treeMap; import javax. servlet. http. httpServletResponse; import org. springframework. beans. factory. annotation. autowired; public class ViewCart {@ Autowired private HttpServletResponse response; public void execute () throws Exception {this. useHashMap (); this. useTreeMap (); this. useLikedHashMap ();} public void useHashMap () throws Exception {response. getWriter (). println ("------ unordered (random output) ------"); Map <String, String> map = new HashMap <String, String> (); map. put ("1", "Level 1"); map. put ("2", "Level 2"); map. put ("3", "Level 3"); map. put ("a", "Level a"); map. put ("B", "Level B"); map. put ("c", "Level c"); Iterator <Entry <String, String> it = map. entrySet (). iterator (); while (it. hasNext () {Entry <String, String> e = it. next (); response. getWriter (). println ("Key:" + e. getKey () + "; Value:" + e. getValue () ;}// ordered (default sorting, cannot be specified) public void useTreeMap () throws Exception {response. getWriter (). println ("------ ordered (but not specified by default) ------"); Map <String, String> map = new TreeMap <String, String> (); map. put ("1", "Level 1"); map. put ("2", "Level 2"); map. put ("3", "Level 3"); map. put ("a", "Level a"); map. put ("B", "Level B"); map. put ("c", "Level c"); Iterator <Entry <String, String> it = map. entrySet (). iterator (); while (it. hasNext () {Entry <String, String> e = it. next (); response. getWriter (). println ("Key:" + e. getKey () + "; Value:" + e. getValue () ;}} public void useLikedHashMap () throws Exception {response. getWriter (). println ("------ ordered (output according to the input order) ------"); Map <String, String> map = new LinkedHashMap <String, String> (); map. put ("1", "Level 1"); map. put ("2", "Level 2"); map. put ("3", "Level 3"); map. put ("a", "Level a"); map. put ("B", "Level B"); map. put ("c", "Level c"); Iterator <Entry <String, String> it = map. entrySet (). iterator (); while (it. hasNext () {Entry <String, String> e = it. next (); response. getWriter (). println ("Key:" + e. getKey () + "; Value:" + e. getValue () ;}} returned result: [html] ------ unordered (random output) ------ Key: 3; Value: Level 3 Key: 2; Value: Level 2 Key: 1; Value: Level 1 Key: B; Value: Level B Key: c; Value: Level c Key: a; Value: Level a ------ ordered (but by default, cannot be specified) ------ Key: 1; Value: Level 1 Key: 2; Value: Level 2 Key: 3; Value: Level 3 Key: a; Value: Level a Key: B; value: Level B Key: c; Value: Level c ------ ordered (output according to the input order) ------ Key: 1; Value: Level 1 Key: 2; Value: Level 2 Key: 3; Value: Level 3 Key: a; Value: Level a Key: B; Value: Level B Key: c; Value: Level c 4. summary: php arrays are implemented using hashmap. Therefore, it is easy to understand hashmap after learning php. linkedhashmap is similar to php arrays.

Related Article

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.