"Java" "collection"

Source: Internet
Author: User

"1. "Hashmap,linkedhashmap,treemap Contrast

Common:

Hashmap,linkedhashmap,treemap are map;map primarily used to store key (key) value pairs, and the keys do not allow keys to repeat, but allow values to be duplicated.

Different points:

The key values stored in the 1.HashMap are random at the time of extraction and are our most commonly used map. It stores the data according to the hashcode value of the key, which can be accessed directly by the key, with fast access speed. HashMap is the best choice for inserting, deleting, and locating elements in a map.

2.TreeMap takes the sorted key-value pairs. But if you want to traverse the key in natural order or in a custom order, TreeMap is better.

3. Linkedhashmap is a subclass of HashMap, and if the order of output needs to be the same as the input, then the LINKEDHASHMAP can be implemented. (Application scenario: Shopping cart etc need order)

code example:

[Java]View PlainCopy
  1. Package com.alibaba.sample.petstore.web.store.module.screen;
  2. Import Java.util.HashMap;
  3. Import Java.util.Iterator;
  4. Import Java.util.LinkedHashMap;
  5. Import Java.util.Map;
  6. Import Java.util.Map.Entry;
  7. Import Java.util.TreeMap;
  8. Import Javax.servlet.http.HttpServletResponse;
  9. Import org.springframework.beans.factory.annotation.Autowired;
  10. Public class Viewcart {
  11. @Autowired
  12. private HttpServletResponse response;
  13. public Void execute () throws Exception {
  14. This.usehashmap ();
  15. This.usetreemap ();
  16. This.uselikedhashmap ();
  17. }
  18. public void Usehashmap () throws Exception {
  19. Response.getwriter (). println ("------unordered (random output)------");
  20. map<string, string> map = new hashmap<string, string> ();
  21. Map.put ("1", "Level 1");
  22. Map.put ("2", "Level 2");
  23. Map.put ("3", "Level 3");
  24. Map.put ("A", "Level A");
  25. Map.put ("B", "Level B");
  26. Map.put ("C", "Level C");
  27. Iterator<entry<string, string>> it = Map.entryset (). Iterator ();
  28. While (It.hasnext ()) {
  29. entry<string, string> e = It.next ();
  30. Response.getwriter (). println ("Key:" + e.getkey () + ";      Value: "+ e.getvalue ());
  31. }
  32. }
  33. //Ordered (default sort, cannot be specified)
  34. public void Usetreemap () throws Exception {
  35. Response.getwriter (). println ("------ordered (but by default shun charge, cannot be specified)------");
  36. map<string, string> map = new treemap<string, string> ();
  37. Map.put ("1", "Level 1");
  38. Map.put ("2", "Level 2");
  39. Map.put ("3", "Level 3");
  40. Map.put ("A", "Level A");
  41. Map.put ("B", "Level B");
  42. Map.put ("C", "Level C");
  43. Iterator<entry<string, string>> it = Map.entryset (). Iterator ();
  44. While (It.hasnext ()) {
  45. entry<string, string> e = It.next ();
  46. Response.getwriter (). println ("Key:" + e.getkey () + ";      Value: "+ e.getvalue ());
  47. }
  48. }
  49. public void Uselikedhashmap () throws Exception {
  50. Response.getwriter (). println ("------ordered (output according to input order)------");
  51. map<string, string> map = new linkedhashmap<string, string> ();
  52. Map.put ("1", "Level 1");
  53. Map.put ("2", "Level 2");
  54. Map.put ("3", "Level 3");
  55. Map.put ("A", "Level A");
  56. Map.put ("B", "Level B");
  57. Map.put ("C", "Level C");
  58. Iterator<entry<string, string>> it = Map.entryset (). Iterator ();
  59. While (It.hasnext ()) {
  60. entry<string, string> e = It.next ();
  61. Response.getwriter (). println ("Key:" + e.getkey () + ";      Value: "+ e.getvalue ());
  62. }
  63. }
  64. }


return Result:

[HTML]View PlainCopy
  1. ------unordered (random output)------
  2. Key:3; Value:level 3
  3. Key:2; Value:level 2
  4. Key:1; Value:level 1
  5. Key:b; Value:level b
  6. KEY:C; Value:level C
  7. key:a; Value:level A
  8. ------ordered (but by default shun charge, cannot be specified)------
  9. Key:1; Value:level 1
  10. Key:2; Value:level 2
  11. Key:3; Value:level 3
  12. key:a; Value:level A
  13. Key:b; Value:level b
  14. KEY:C; Value:level C
  15. ------ordered (output according to the input order)------
  16. Key:1; Value:level 1
  17. Key:2; Value:level 2
  18. Key:3; Value:level 3
  19. key:a; Value:level A
  20. Key:b; Value:level b
  21. KEY:C; Value:level C


4. Summary: The PHP array is implemented with HashMap, so learning PHP for HashMap is easy to understand, linkedhashmap with the PHP array implementation of the function is very similar.

"2. 】

"Java" "collection"

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.