The difference between Java HashMap and Linkedhashmap

Source: Internet
Author: User

HashMap and Linkedhashmap are two implementation classes of the map interface, the biggest difference is that hashmap elements are stored in order, and the elements of Linkedhashmap are stored in an orderly manner, example:

map<string, integer> hashMap = new hashmap<string, integer> (); map<string, integer> linkedhashmap = new linkedhashmap<string, integer> (); for (int i = 0; i <; i++) {have Hmap.put (String.valueof (i), i), Linkedhashmap.put (string.valueof (i), i); System.out.println ("HashMap"); for (entry<string, integer> entry:hashMap.entrySet ()) {System.out.println ( Entry.getkey () + ":" + entry.getvalue ());} System.out.println ("Linkedhashmap"); for (entry<string, integer> entry:linkedHashMap.entrySet ()) { System.out.println (Entry.getkey () + ":" + entry.getvalue ());
Output Result:

Hashmap3:32:21:10:07:76:65:54:49:98:8linkedhashmap0:01:12:23:34:45:56:67:78:89:9

Previously also published a book to achieve the order of the map elements "Java implementation map set sorting function", did not expect to be their own shot bricks, now think of no culture can be really scary ah ...

The difference between Java HashMap and Linkedhashmap

Related Article

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.