JavaScript JS simulation Java Map class implements the Java map class method __python

Source: Internet
Author: User
Tags key string map class

NOTE: Reprint please indicate the source.
/**------------------------Public Method-------------------------* *
/**
* Add Remove method to array
*
* @param s
* The values in the removed 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 () {
/** keys store key value data storage/
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;
};
/**
* Get a key value
*
* @param key
* Key String
*/
This.get = function (key) {
return Date[key];
};
/**
* return "1" When a key key does not exist
*
* @param key
* Key String
*/
This.remove = function (key) {
When key does not exist
if (data[key] = = null) {
Return '-1 ';
}
Keys.remove (key);
Data[key] = null;
};
/**
* Traversal function performs the callback function fn (key,value,index) parameter is not a function Ruturn "-1"
*
* @param fn
* Callback function name function parameter should be (Key,value,index)
*/
This.each = function (fn) {
Check if this function exists
if (typeof fn!= ' function ') {
Return '-1 ';
}
for (var i = 0; i < keys.length; i++) {
FN (Keys[i], data[keys[i]], i);
}
};
/**
* Get the corresponding array of key values similar to Java EntrySet ()
*
* @return An array of key value objects {Key,value}
*/
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;
};
/**
* Return key array similar to Java Ketset () function
*
* @return Array
*/
This.keyset = function () {
return keys;
}
/**
* Determine if the map is empty
*
* @return NULL return true NOT NULL return FALSE
*/
This.isempty = function () {
return keys.length = = 0;
};
/**
* Returns the size of the map
*/
This.size = function () {
return keys.length;
};
/**
* Rewrite the tostring format as {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;
};
/**
* Empty 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-------------------------* *

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.