Javascript JS simulates Java map class to implement Java map class methods such as put get

Source: Internet
Author: User

Note: Please indicate the source for reprinting!
/** ------------------------ Public method -------------------------*/
/**
* Add the Remove Method for Array
*
* @ Param s
* Removed values in the array
* @ Return
*/
Array. Prototype. Remove = function (s ){
For (VAR I = 0; I <this. length; I ++ ){
If (S = This [I])
This. splice (I, 1 );
}
}
/** ------------------------ Public method end -------------------------*/
/** ------------------------ Bytemap start -------------------------*/
/**
* Simulate Java map class, put (Key, value), get (key), remove (key), isempty ,..........
*/
Function bytemap (){
/** Store key-value data in keys */
VaR keys = new array ();
VaR DATA = new object ();

/**
* Put a key-Value Pair
*
* @ Param key
* Value: String
* @ Param Value
* Key value object
*/
This. Put = function (Key, value ){
If (data [Key] = NULL ){
Keys. Push (key );
}
Data [Key] = value;
};
/**
* Obtain the key value of a key position
*
* @ Param key
* Key string
*/
This. Get = function (key ){
Return date [Key];
};
/**
* If a key does not exist, "-1" is returned"
*
* @ Param key
* Key string
*/
This. Remove = function (key ){
// When the key does not exist
If (data [Key] = NULL ){
Return '-1 ';
}
Keys. Remove (key );
Data [Key] = NULL;
};
/**
* When the FN (Key, value, index) parameter of the callback function is not a function, ruturn "-1"
*
* @ Param FN
* Callback function name the function parameter should be (Key, value, index)
*/
This. Each = function (FN ){
// Check whether this function exists
If (typeof FN! = 'Function '){
Return '-1 ';
}
For (VAR I = 0; I <keys. length; I ++ ){
FN (Keys [I], data [Keys [I], I );
}
};
/**
* The array corresponding to the key value is similar to the Java entryset ()
*
* @ Return: array of {key, value} key value object
*/
This. entryarray = function (){
VaR Len = keys. length;
VaR temparr = new array (LEN );
For (VAR I = 0; I <Len; I ++ ){
Temparr [I] = {
Key: Keys [I],
Value: Data [Keys [I]
};
}
Return temparr;
};
/**
* The returned key array is similar to the Java ketset () function.
*
* @ Return Array
*/
This. keyset = function (){
Return keys;
}
/**
* Determines whether the map is empty.
*
* @ Return NULL return true not null return false
*/
This. isempty = function (){
Return keys. Length = 0;
};
/**
* Returns the map size.
*/
This. size = function (){
Return keys. length;
};
/**
* Rewrite tostring format to {key1 = value1, key2 = value2 ,....}
*/
This. tostring = function (){
VaR Len = keys. length;
VaR temp = "{";
For (VAR I = 0; I <Len; I ++ ){
Temp + = (Keys [I] + "=" + data [Keys [I] + ",");
}
Temp + = "}";
Return temp;
};
/**
* Clear map
*
* @ Return
*/
This. Clear = function (){
For (VAR I = keys. Length-1; I> = 0; I --){
Delete data [Keys [I];
Keys. Pop (I );
}
};
}
/** ------------------------ Bytemap test method start -------------------------*/

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.