Map to store data in order

Source: Internet
Author: User

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

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.