Tag: Map to store data in order
We know that the map is unordered when it stores data. And sometimes, we sort in our own order. For example, if you look up a collection of data and plug it into a map, you want to sort by the order of the data in your query. Then we can't manipulate the data with regular maps. Next, we use Linkedhashmap
linkedhashmap<String,Integer>Map=Newlinkedhashmap<String,Integer> ();Map. put ("D",2);Map. put ("C",1);Map. put ("B",1);Map. put ("a",3); System. out. println (Map. KeySet ());Map<String,Integer> MAP1 =Newhashmap<String,Integer> (); Map1.put ("D",2); Map1.put ("C",1); Map1.put ("B",1); Map1.put ("a",3); System. out. println (Map1.keyset ());
Output Result:
[D, C, B, a]
[D, B, C, a]
From this we can see: Linkedhashmap ordering is ordered, and map is unordered.
Key output is different, its corresponding value is naturally different
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Map to store data in order