Java HashMap, Linkedhashmap

Source: Internet
Author: User

One. HashMap

1.HashMap Constructors

 PublicHashMap (intInitialcapacity,floatloadfactor) {    if(Initialcapacity < 0)        Throw NewIllegalArgumentException ("Illegal initial capacity:" +initialcapacity); if(Initialcapacity >maximum_capacity) initialcapacity=maximum_capacity; if(loadfactor <= 0 | |Float.isnan (loadfactor))Throw NewIllegalArgumentException ("Illegal load factor:" +loadfactor);  This. Loadfactor =Loadfactor; Threshold=initialcapacity;   Init (); //Note this template function, which is used in Linkhashmap }

2. Default parameters

// The capacity must be 2 index (default is 16), think about the reason?  staticfinalint default_initial_capacity = 1 << 4static  Finalint maximum_capacity = 1 <<; // The default balance factor is 0.75, which is the tradeoff between time complexity and spatial complexity . // too high a factor can increase storage space utilization but the time to find will increase.  staticfinalfloat default_load_factor = 0.75f;

3. Resetting the Index

    voidResizeintnewcapacity) {entry[] oldtable=table; intOldcapacity =oldtable.length; if(Oldcapacity = =maximum_capacity) {Threshold=Integer.max_value; return; } entry[] NewTable=NewEntry[newcapacity];        Transfer (newtable, inithashseedasneeded (newcapacity)); Table=newtable; Threshold= (int) math.min (newcapacity * loadfactor, maximum_capacity + 1); }    /*** Transfers all entries from the current table to newtable. */    voidTransfer (entry[] newtable,Booleanrehash) {        intNewcapacity =newtable.length;  for(entry<k,v>e:table) {             while(NULL!=e) {Entry<K,V> next =E.next; if(Rehash) {E.hash=NULL= = E.key? 0: Hash (E.key); }                inti =indexfor (E.hash, newcapacity); //which slot is occupied E.next=Newtable[i]; Newtable[i]=e; E=Next; }        }    }

4.

    Static int indexfor (intint  length) {        //  assert Integer.bitcount (length) = = 1: "Length must be a non-zero power of 2";        return H & (Length-1);    }

Two. Linkedhashmap

1.Entry

    Private Static class extends Hashmap.entry<k,v> {        //  These fields comprise the doubly linked list used for iteration.< /c9>        entry<k,v> before, after;        Entry (int hash, K key, V value, hashmap.entry<k,v> next) {            super (hash, key, value, next);}        }

2. Delete

   // When you delete a node, you need     to // 1. The successor of the forward node points to the subsequent node    to delete the node //    privatevoid  remove () {         = after ;         = before;    }

3. Increase

        Private void Addbefore (entry<k,v> existingentry)  {after = existingentry;            The successor node of the current node points             to the new node = Existingentry.before;             this ;             this ;               //        }

4. The rewritten init

    @Override    void  init () {        newnullNULL null );         = Header.after = header;    }

5. Rewrite transfer

    /*** Transfers all entries to new table array.  This method was called * by superclass resize.     It's overridden for performance, as it's * faster to iterate using our linked list. */@OverridevoidTransfer (hashmap.entry[] newtable,Booleanrehash) {        intNewcapacity =newtable.length;  for(entry<k,v> e = Header.after; E! = header; e =e.after) { //reorder elements in the  listif(rehash) E.hash= (E.key = =NULL) ? 0: Hash (E.key); intindex =indexfor (E.hash, newcapacity); E.next=Newtable[index]; Newtable[index]=e; }    }

Java HashMap, 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.