JS Implementation HashMap

Source: Internet
Author: User

/** * Map object, implement map function * * Interface: * SIZE () Get number of map elements * ISEMPTY () Determine if map is empty * C                 Lear () Delete all elements of map * put (key, value) add element to map (key, value) * Remove (key)                  Deletes the element of the specified key, returns true successfully, returns false * GET (key) gets the element value of the specified key, and fails to return NULL * ELEMENT (Index)   Gets the element of the specified index (using Element.key,element.value to get the key and value), and fails to return NULL * CONTAINSKEY (key) to determine whether the element in the map contains the specified key * Containsvalue (value) to determine if the map contains an element of the specified value * VALUES () gets an array of all value in the map (array) * keys   () gets an array of all keys in the map (array) * Example: * var map = new map ();   * * Map.put ("key", "value"); * var val = map.get ("key") * ... **/functionHashMap () {/** * Store data*/    This. data =NewObject (); /** * Put a key value pair * @param {String} key * @param {Object} value*/    This. put =function(key, value) { This. Data[key] =value;      }; /** * Get the value corresponding to a key * @param {String} key * @return {Object} value*/    This. get =function(key) {return  This. ContainsKey (Key)? This. Data[key]:NULL;      }; /** * Delete a key value pair * @param {String} key*/    This. remove =function(key) {Delete  This. Data[key];      }; /** * traverse Map, execute handler function * * @param {Function} callback function (Key,value,index) {:} */    This. each =function(FN) {if(typeoffn = ' function ')      {         return; }      varLen = This. Data.length;  for(vari=0;i<len;i++)      {         varK = This. Data[i]; fn (k, This. Data[k],i);      }   }; /** * Get an array of key values (Java-like EntrySet ()) * An array of @return key-value object {Key,value}*/    This. Entrys =function()    {      varLen = This. Data.length; varEntrys =NewArray (len);  for(vari = 0; i < Len; i++) {Entrys[i]={key:i, value: This. Data[i]}; }      returnEntrys;      }; /** * To determine if the map is empty*/    This. IsEmpty =function()    {      return  This. Data.length = = 0;      }; /** * Get the number of key-value pairs*/    This. Size =function()   {      return  This. Data.length;      }; /** * Rewrite tostring to format JSON*/    This. toString =function()   {      vars = "[";  for(vari=0;i< This. data.length;i++,s+= ', '){         varK = This. Data[i]; S+ = "{' id ': '" + K + "', ' value ': '" + This. data[k]+ "'}"; } s=s.substring (0, S.length-1); if(s!= "") {s+="]"; }      returns;      }; /** * Output value*/    This. Values =function (){      var_values=NewArray ();  for(varKeyinch  This. Data) {_values.push ( This. Data[key]); }          return_values;      }; /** * Get keys*/    This. KeySet =function (){      var_keys =NewArray ();  for(varKeyinch  This. Data)          {_keys.push (key); }          return_keys;      }; /** * Determine if the map contains the specified key element*/     This. ContainsKey =function(_key) {return(_keyinch  This. Data);        }; /** * Clear Map*/       This. Clear =function(){          This. data.length = 0;  This. data =NewObject (); }; }

JS Implementation HashMap

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.