/**
* Demonstration of Map and Set
*
*/
//intialize Map
var employee = new Map();
//initialize Map by passing array of key value pairs
var employee2 = new Map([
["name","Raj kher"],
["designation","Lead Engineer"],
["city","Mumbai"]
]);
//set function demo
employee.set("name","San Jose");
employee.set(1, "home : Delhi");
employee.set(2, "home : Tokyo");
employee.set(3,"home : New York").set(4,"home : Dubai").set(5,"home : Vienna");
//has key demo
console.log("employee has name key ? " + employee.has("name"));
console.log("employee has isMarried key ? " + employee.has("isMarried"));
console.log("employee has home '2' ? "+employee.has('2'));
console.log("employee has home 2 ? "+employee.has(2));
//get value from Map
console.log("employee Name :: "+employee.get("name"));
console.log("employee 5 home :: "+employee.get(5));
console.log("employee2 name :: "+employee2.get("name"));
console.log("employee2 designation :: "+employee2.get("designation"));
//get function demo
//clear function to remove all key-value pairs from map
employee2.clear();
console.log("employee2 has name ? " + employee2.has("name"));
console.log("employee2 has city ? " + employee2.has("city"));
console.log("employee2 has designation ? " + employee2.has("designation"));
//delete function to remove key and associated value. It returns true if deleted.
employee2 = new Map([
["name","Raj kher"],
["designation","Lead Engineer"],
["city","Mumbai"]
]);
console.log("deleting designation :: "+employee2.delete("designation"));
console.log("deleting designation again :: "+employee2.delete("designation"));
console.log("designation exists? :: "+employee2.delete("designation"));
//entries() function to return iterator function with array of [key,value]
var itr1 = employee2.entries();
console.log("itr1 ::"+itr1.next().value);
console.log("itr1 ::"+itr1.next().value);
console.log("itr1 ::"+itr1.next().value);
//forEach(callback[,thisArg]) - execute function for each item
function print(key, value){
console.log("key ::" + key +", value ::"+value);
}
employee2.forEach(print);
//keys() - returns iterator object to refer to keys of Map
var keyItr1 = employee2.keys();
console.log("keyItr1 ::"+keyItr1.next().value);
console.log("keyItr1 ::"+keyItr1.next().value);
//values() - returns iterator object to refer to values of Map
var valueItr1 = employee.values();
console.log("valueItr1 ::"+valueItr1.next().value);
console.log("valueItr1 ::"+valueItr1.next().value);
//for .. in loop over map
for(let item of employee2)
console.log("key ::"+item);
//weakMap is a Map having key as object. Those objects can be garbage collected
var weakMap3 = new WeakMap();
var object = {};
weakMap3.set(object, "Hello World");
console.log("weakMap3.get ::"+weakMap3.get(object));
//Set is data structure which does not allow duplicates
//Set can allow primitive and non-primitive types
//Set elements can be iterated in order of their insertion
//Set constructor
var fruitSet = new Set("Mango","Banana","Chikkoo","Orange");
//Size property returns the size of set
console.log("fruitSet.size :: " + fruitSet.size);
//add() to add a value to set,duplicates are not added
fruitSet.add("Apple");
fruitSet.add("Pineapple");
fruitSet.add("Mango");
console.log("fruitSet.size ::"+fruitSet.size);
//clear() to return all values from set
fruitSet.clear();
console.log("cleared size ::"+fruitSet.size);
//delete(value) to remove value from set
fruitSet = new Set();
fruitSet.add("Apple");
fruitSet.add("Pineapple");
fruitSet.add("Mango");
fruitSet.add("Banana");
fruitSet.delete("Mango");
console.log("deleted Mango fruitSet size :: " + fruitSet.size);
//entries() to return iterator that contains array of values
fruitSet = new Set();
fruitSet.add("Apple");
fruitSet.add("Pineapple");
fruitSet.add("Mango");
fruitSet.add("Banana");
var fruitSetItr1 = fruitSet.entries();
console.log("fruitSet itr :: " + fruitSetItr1.next().value);
console.log("fruitSet itr :: " + fruitSetItr1.next().value);
console.log("fruitSet itr :: " + fruitSetItr1.next().value);
console.log("fruitSet itr :: " + fruitSetItr1.next().value);
//forEach(callback[,thisArg]) - to execute callback function on each value of set
function printSetItem(value){
console.log("set.value ::" + value);
}
fruitSet = new Set();
fruitSet.add("Apple");
fruitSet.add("Pineapple");
fruitSet.add("Mango");
fruitSet.forEach(printSetItem);
//has(value) returns true if value present
console.log("fruitSet has value 'Apple' ::"+fruitSet.has("Apple"));
//values() - returns iterator that contains array of values in insertion order
var fruitSetItr2 = fruitSet.values();
console.log("fruitSet itr :: " + fruitSetItr2.next().value);
console.log("fruitSet itr :: " + fruitSetItr2.next().value);
console.log("fruitSet itr :: " + fruitSetItr2.next().value);
//WeakSet - is a set which contains only objects as values
var fruitWeakSet = new WeakSet();
fruitWeakSet.add({name:"Mango"});
fruitWeakSet.add({name:"Apple"});
fruitWeakSet.add({name:"Pineapple"});
fruitWeakSet.add({name:"Banana"});
fruitWeakSet.add({name:"Kiwi"});
Output
employee has name key ? true employee has isMarried key ? false employee has home '2' ? false employee has home 2 ? true employee Name :: San Jose employee 5 home :: home : Vienna employee2 name :: Raj kher employee2 designation :: Lead Engineer employee2 has name ? false employee2 has city ? false employee2 has designation ? false deleting designation :: true deleting designation again :: false designation exists? :: false itr1 ::name,Raj kher itr1 ::city,Mumbai itr1 ::undefined key ::Raj kher, value ::name key ::Mumbai, value ::city keyItr1 ::name keyItr1 ::city valueItr1 ::San Jose valueItr1 ::home : Delhi key ::name,Raj kher key ::city,Mumbai weakMap3.get ::Hello World fruitSet.size :: 5 fruitSet.size ::8 cleared size ::0 deleted Mango fruitSet size :: 3 fruitSet itr :: Apple,Apple fruitSet itr :: Pineapple,Pineapple fruitSet itr :: Mango,Mango fruitSet itr :: Banana,Banana set.value ::Apple set.value ::Pineapple set.value ::Mango fruitSet has value 'Apple' ::true fruitSet itr :: Apple fruitSet itr :: Pineapple fruitSet itr :: Mango