Java Collection Four family stories (iv) The LINKEDHASHMAP of the--map family

Source: Internet
Author: User

Each family has one of the most basic and most commonly used data types: For example, the HashMap of the Arraylist,map series of the list series, and their essence is the array storage structure. Similarly, each family has a class that begins with a linked, such as the list family's LinkedList and the map family's linkedhashmap, which are stored in two-way linked lists (in fact, a continuous array of pointers in two directions, so they are compatible with the array , and can have the nature of the doubly linked list). The purpose of introducing the function of doubly linked list is to be able to traverse sequentially.

Today, we are going to introduce Linkedhashmap.

Extended HASHMAP increases the implementation of the doubly linked list, claiming to be the most memory-occupying data structure. Support for iterator () is sorted by entry in the insertion order (but not the update, if the setting Accessorder property is true, all read and write access is counted).

The implementation is on the entry to add the property before/after pointer, insert the add yourself to the front of the header entry. If all read and write access to order, but also to the front and back entry before/after stitching up to delete themselves in the list.

(This article originates from: http://my.oschina.net/happyBKs/blog/493260)

In the following example, I did not compare two groups of Linkedhashmap and HashMap respectively, the data of the two groups respectively one by one corresponding.

We can see that linkedhashmap is consistent with the order in which we insert the put, the order in which it is inserted, and the order in which it is returned. And HashMap no matter what order we insert, the order of return is unique and independent of the insertion Order, and HashMap's return key-value order is only related to Hashcode.

@Testpublic  void testlinkedhashmap () {System.out.println ("linkedhashmap: n-m-s"); Linkedhashmap<string,integer> lhm=new linkedhashmap<string,integer> (); Lhm.put ("NOKIA",  4000); Lhm.put ("MOTO",  3000), Lhm.put ("Samgsung",  3500); for (Entry e:lhm.entryset ()) { System.out.println (E.getkey () + ": " +e.getvalue ());} System.out.println ("Linkedhashmap: s-n-m"); Linkedhashmap<string,integer> lhm2=new linkedhashmap<string,integer> (); Lhm2.put (" Samgsung ",  3500); Lhm2.put (" NOKIA ",  4000); Lhm2.put (" MOTO ",  3000); for (Entry e:lhm2.entryset ( ) {System.out.println (E.getkey () + ": " +e.getvalue ());} System.out.println ("Hashmap: n-m-s"); Hashmap<string,integer> hm=new hashmap<string,integer> (); Hm.put ("NOKIA",  4000); Hm.put ("MOTO",  3000); Hm.put ("Samgsung",  3500); for (Entry e:hm.entryset ()) {System.out.println ( E.getkey () + ": " +e.getvalue ());} System.out.println ("hashmap:&nbsp S-n-m "); Hashmap<string,integer> hm2=new hashmap<string,integer> (); Hm2.put ("SAMGSUNG",  3500) ; Hm2.put ("NOKIA",  4000), Hm2.put ("MOTO",  3000); for (Entry e:hm2.entryset ()) {System.out.println ( E.getkey () + ": " +e.getvalue ());} /*linkedhashmap: n-m-snokia: 4000moto: 3000samgsung: 3500linkedhashmap: s-n-msamgsung : 3500nokia: 4000moto: 3000hashmap: n-m-smoto: 3000nokia: 4000samgsung:  3500hashmap: s-n-mmoto: 3000nokia: 4000samgsung: 3500*///hashmap inserted key-value pairs are unordered and the position is only associated with key Linkedhashmap is related to the insertion Order//actually LINKEDHASHMAP is to add the property Before/after pointer on the entry,//Add yourself to the front of the header entry when inserting. If all read and write access to order, but also to the front and back entry before/after stitching up to delete themselves in the list. }


Java Collection Four family stories (iv) The LINKEDHASHMAP of the--map family

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.