Java Improvement Tutorial (14)-Understanding Map Linkedhashmap

Source: Internet
Author: User

Java Basics and Improved tutorials directory

A hash table of the Map interface and a list of links are implemented with predictable iteration order. The difference between this implementation and HASHMAP is that the latter maintains a doubly linked list running on all entries. This list of links defines the order of iterations, which is usually the order in which the keys are inserted into the map (insert order). Note that if you reinsert the key in the map, the insertion order is unaffected. (If M.containskey (k) returns true before the call to M.put (K, v), the call will reinsert the key K into the map m. )

This implementation allows customers to avoid unspecified, often disorganized sorting work provided by HASHMAP (and Hashtable) without increasing the cost associated with TREEMAP. Use it to generate a mapped copy that is the same as the original order, regardless of the implementation of the original mapping:

 1.package Collection;
2.3.import Java.util.HashMap;
4.import Java.util.Iterator;
5.import Java.util.LinkedHashMap;
6.import Java.util.Map; 7.8./** 9.
* Lao Zi Zhu Java Improvement Tutorial (14)-Understand map's linkedhashmap. 10. * 11.
* This class inherits from HashMap and has all of its attributes.
12. * The internal maintenance of a double link for all entries, with predictable iteration sequence.
13. * The default is displayed in the order in which they are inserted.
14. * Insert a key data repeatedly, will not affect the use of the order.
15. * This class avoids the messy order of use of hashmap while avoiding the additional cost of treemap. 16. * 17. * @author old Zi zhu Java century Net (java2000.net) 18. * 19.  */20.public class Lession14linkedhashmap {21.    public static void Main (string[] args) {22.    Use 2 classes respectively to test the same data 23.    As you can see, the order of the HashMap is 24 unpredictable.
The order of Linkedhashmap is strictly in the insertion Order 25. Num//102=>102; 100=>100; 101=>101; 98=>98;
99=>99;
TestMap (New HashMap ()); //98=>98; 99=>99; 100=>100; 101=>101;
102=>102;
TestMap (New Linkedhashmap ());    30.31.    After you delete the data, Linkedhashmap places the data at the end of 32.  HashMap may use the position of the previous vacancy 33.
} 34.    public static void TestMap (map map) {36. for (int i = <= i)102;      i++) {37.
Map.put (i, I);    38.} 39.
Showmap (map);    40.//try to insert data 41 repeatedly.
Map.put (100, 100);
Showmap (map);    43.44.    Delete the data and insert 45 again.
Map.Remove (100);
Map.put (100, 100);
Showmap (map);
48.} 49.    public static void Showmap (map map) {51.    Operation of the iteration key 52.
Iterator it = Map.keyset (). iterator ();
An. Object key;      while (It.hasnext ()) {55.
Key = It.next ();
System.out.print (key + "=>" + map.get (key) + ";");    57.} 58.
System.out.println (); .} 

Operation Effect:

102=>102; 100=>100; 101=>101; 98=>98; 99=>99;
102=>102; 100=>100; 101=>101; 98=>98; 99=>99;
102=>102; 100=>100; 101=>101; 98=>98; 99=>99;
98=>98; 99=>99; 100=>100; 101=>101; 102=>102;
98=>98; 99=>99; 100=>100; 101=>101; 102=>102;
98=>98; 99=>99; 101=>101; 102=>102; 100=>100;

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.