Javascript implementation of the HashMap class code
- 2020-03-30 03:28:53
- OfStack
<script language = "javascript" >
function HashMap() {
var size = 0;
var entry = new Object();
this.put = function(key, value) {
if (!this.containsKey(key)) {
size++;
entry[key] = value;
}
}
this.get = function(key) {
return this.containsKey(key) ? entry[key] : null;
}
this.remove = function(key) {
if (this.containsKey(key) && (delete entry[key])) {
size--;
}
}
this.containsKey = function(key) {
return (key in entry);
}
this.containsValue = function(value) {
for (var prop in entry) {
if (entry[prop] == value) {
return true;
}
}
return false;
}
this.values = function() {
var values = new Array();
for (var prop in entry) {
values.push(entry[prop]);
}
return values;
}
this.keys = function() {
var keys = new Array();
for (var prop in entry) {
keys.push(prop);
}
return keys;
}
this.size = function() {
return size;
}
this.clear = function() {
size = 0;
entry = new Object();
}
}
//Create a HashMap object
var hashMap = new HashMap();
hashMap.put("A", "1");
hashMap.put("B", "2");
hashMap.put("A", "5");
hashMap.put("C", "3");
hashMap.put("A", "4");
alert(hashMap.size());
</script>