Skip to content

Instantly share code, notes, and snippets.

@codyburleson
Last active April 12, 2022 14:32
Show Gist options
  • Save codyburleson/089157ed8eca114da8c4 to your computer and use it in GitHub Desktop.
Save codyburleson/089157ed8eca114da8c4 to your computer and use it in GitHub Desktop.
Java Map in JavaScript - An object that maps keys to values.
/**
* An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
* For those familiar with the Java programming language, this is similar to a HashMap; it implements most of the
* methods defined by Java's java.util.Map interface.
*
* @constructor
* @version 1.1.0
* @author cody@base22.com Burleson, Cody
*/
function Map() {
this.dict = {};
/**
* Returns the number of key-value mappings in this map.
* @method
*/
this.size = function() {
return Object.keys(this.dict).length;
};
/**
* Returns true if this map contains no key-value mappings.
* @method
*/
this.isEmpty = function() {
return Object.keys(this.dict).length == 0;
};
/**
* Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
* @method
*/
this.get = function(key){
return this.dict[key];
};
/**
* Returns true if this map contains a mapping for the specified key.
* @method
*/
this.containsKey = function(key){
if( this.get(key) !== undefined) {
return true;
} else {
return false;
}
};
/**
* Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.
* @method
*/
this.put = function(key,value) {
this.dict[key] = value;
};
/**
* Removes the mapping for the specified key from this map if present.
* @method
*/
this.remove = function(key) {
'use strict';
delete this.dict[key];
};
/**
* Removes all of the mappings from this map. The map will be empty after this call returns.
* @method
*/
this.clear = function(){
this.dict = {};
};
/**
* Executes the given callback for each entry in this map until all entries have been processed.
* The given callback will be passed a map entry as parameter. So, for example...
*
* function myCallback(mapEntryItem) {
* console.log('I will process this item: ' + mapEntryItem.text);
* }
*
* myMap.forEach(myCallback);
*
* @method
*/
this.forEach = function(callback) {
var len = this.size();
for (i = 0; i < len; i++) {
var item = this.get( Object.keys(this.dict)[i] );
callback(item);
}
}
}
/**
* =============================
* Map Test Cases
* =============================
*/
/**
* Useful for unit testing JavaScript, this method asserts that the given condition is true.
* If the given condition is not true, an optionally provided message is logged or 'Assertion failed.'
* @method
*/
function assert(condition, message) {
if (!condition) {
console.error(message) || console.error('Assertion failed');
} else {
console.log("-- PASS");
}
}
// Instantiate a new Dictionary object...
var myMap = new Map();
// Test that isEmpty returns true at this point
assert( myMap.isEmpty() );
// Test that size is zero at this point
assert( myMap.size() == 0 , "size should be zero before any entries have been put into the lookup table.");
// Put a sim0le string value in the dictionary and then test for it
myMap.put('key1','http://value1?dt=2014_06_01&dummy=true');
// Now let's test adding a different kind of object...
var testObj = {prop1:"Hello", prop2:" World!"};
myMap.put('key2',testObj);
// Test that size now equals one
assert( myMap.size() == 2, "size should be 2 after adding 2 entries.");
var ref = myMap.get('key2');
// Test that ref was fetched from the dictionary
assert( ref !== undefined , "ref should not be undefined; it should be a reference to testObj");
// Test a property of the fetched object; just to show there is support for more than just string types
assert( ref.prop1 == 'Hello' , "ref.prop1 should equal 'hello' ");
// Show that containsKey is false when there is no matching key
assert( myMap.containsKey('notReal') == false, "containsKey('notReal') should return false; no such object is in the lookup table.");
// Show that containsKey is true when there is a matching object found
assert( myMap.containsKey('key2') == true, "containsKey('key2') should return true; that object is in the lookup table.");
// Now, we remove an item to test the remove() method
myMap.remove('key1');
assert( myMap.size() == 1 , "size should be 1 after removing item under 'key1'.");
// Finally, test the clear()
myMap.clear();
assert( myMap.size() == 0 , "size should be 0 after clearing lookup table.");
@kariuki-joseph
Copy link

Awesome Explanation!

@codyburleson
Copy link
Author

codyburleson commented Apr 12, 2022

Awesome Explanation!

Thank you! But, please be advised that maps are now available as a part of the ECMAScript 2015 (ES6) standard. See: Map - JavaScrip, MDN

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment