An ordered map Linkedhashmap

Source: Internet
Author: User


HashMap is unordered, HashMap in put is based on the key hashcode hash and then put into the corresponding place. So in a certain order put into the HashMap, and then traverse out the order of HashMap and the order of the put is different (unless the key has been put in accordance with the Hashcode sort number, the probability is very small)
Simple HashMap is not possible to achieve the sorting, this sort refers to, we put the key value pairs in a certain order put into the hashmap, and then in the operation of the key value pair, it is to put in the order of the key values are taken out.
Java has provided linkedhashmap after JDK1.4 to help us achieve an orderly hashmap!
Linkedhashmap The key value pairs are taken in the order you put them.
    Import Java.util.HashMap;      Import Java.util.Iterator;      Import Java.util.LinkedHashMap;      Import Java.util.Map;      Import Java.util.Map.Entry; /** * @author Teana e-mail: [email protected] * @version created: 2011-1-21 pm 02:23:07 * @DO Linkedh              Ashmap and HashMap */public class Linkedmap {public static void main (string[] args) {              Linkedhashmap ordered Map maps = new Linkedhashmap ();              Maps.put ("1", "Zhang San");              Maps.put ("2", "John Doe");              Maps.put ("3", "Harry");              Maps.put ("4", "Zhao Liu");              System.out.println ("Linkedhashmap (ordered):");              Iterator it = Maps.entryset (). Iterator ();                  while (It.hasnext ()) {map.entry entity = (Entry) it.next ();              System.out.println ("[key =" + Entity.getkey () + ", value =" + entity.getvalue () + "]"); }//hasHmap unordered Map map = new HashMap ();              Map.put ("1", "Zhang San");              Map.put ("2", "John Doe");              Map.put ("3", "Harry");              Map.put ("4", "Zhao Liu");              it = null;              System.out.println ("HashMap (unordered):");              it = Map.entryset (). iterator ();                  while (It.hasnext ()) {map.entry entity = (Entry) it.next ();              System.out.println ("[key =" + Entity.getkey () + ", value =" + entity.getvalue () + "]");   }          }      }



The results of the implementation are as follows:
Linkedhashmap (ordered):
[key = 1, value = Zhang San]
[key = 2, value = John Doe]
[key = 3, value = Harry]
[key = 4, value = Zhao Liu]
HashMap (unordered):
[key = 3, value = Harry]
[key = 2, value = John Doe]
[key = 1, value = Zhang San]
[key = 4, value = Zhao Liu]

HASHMAP,LINKEDHASHMAP,TREEMAP Application Brief
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.

    Import Java.util.HashMap;      Import Java.util.Iterator;      Import Java.util.LinkedHashMap;      Import Java.util.Map;       Import Java.util.TreeMap; public class Mapapptest {/** * @Create on Nov 9, LRM */public static void main (string[] args)         {//TODO auto-generated Method Stub Mapapptest.noorder ();         Mapapptest.hasorder ();      Mapapptest.likedhashmap ();         public static void Noorder () {System.out.println ("------unordered (Random output------");         Map map = new HashMap ();         Map.put ("1", "Level 1");         Map.put ("2", "Level 2");         Map.put ("3", "Level 3");         Map.put ("4", "Level 4");         Map.put ("F", "level F");         Map.put ("Q", "Level Q");         Iterator it = Map.entryset (). Iterator ();          while (It.hasnext ()) {Map.entry E = (map.entry) it.next ();   System.out.println ("Key:" + e.getkey () + ";         Value: "+ e.getvalue ()); }}//Ordered (Default sort, cannot be specified) public static void Hasorder () {System.out.println ("------ordered (but by default shun charge, cannot be specified)------");         Map map = new TreeMap ();         Map.put ("F", "level F");         Map.put ("7", "Level 1");         Map.put ("8", "Level 2");         Map.put ("4", "Level 3");         Map.put ("4", "Level 4");         Map.put ("Q", "Level Q");         Map.put ("E", "Level E");         Iterator it = Map.entryset (). Iterator ();          while (It.hasnext ()) {Map.entry E = (map.entry) it.next ();   System.out.println ("Key:" + e.getkey () + ";         Value: "+ e.getvalue ());         }} public static void Likedhashmap () {System.out.println ("------ordered (output according to input order)------");         Map map = new Linkedhashmap ();         Map.put ("F", "level F");         Map.put ("7", "Level 1");         Map.put ("8", "Level 2");         Map.put ("4", "Level 3");         Map.put ("4", "Level 4");         Map.put ("Q", "Level Q");         Map.put ("E", "Level E"); Iterator it = mAp.entryset (). iterator ();          while (It.hasnext ()) {Map.entry E = (map.entry) it.next ();   System.out.println ("Key:" + e.getkey () + ";         Value: "+ e.getvalue ());    }      }       }



Output Result:
------unordered (Random output------
Key:3; Value:level 3
Key:f; Value:level F
Key:2; Value:level 2
Key:4; Value:level 4
Key:q; Value:level Q
Key:1; Value:level 1
------ordered (but by default shun charge, cannot be specified)------
Key:4; Value:level 4
Key:7; Value:level 1
Key:8; Value:level 2
Key:e; Value:level E
Key:f; Value:level F
Key:q; Value:level Q
------ordered (output according to the input order)------
Key:f; Value:level F
Key:7; Value:level 1
Key:8; Value:level 2
Key:4; Value:level 4
Key:q; Value:level Q
Key:e; Value:level E


J

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

An ordered map Linkedhashmap

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.