集合比較常見的實現方式是哈希表,這裏使用JavaScript的Object類進行封裝。javascript
集合一般是由一組無序的、不能重複的元素構成。java
集合是特殊的數組:編程
實現集合類:數組
在ES6中的Set類就是一個集合類,這裏咱們從新封裝一個Set類,瞭解集合的底層實現。編程語言
JavaScript中的Object類中的key就是一個集合,可使用它來封裝集合類Set。測試
集合常見的操做:this
add(value)
:向集合添加一個新的項;prototype
remove(value)
:從集合中移除一個值;code
has(value)
:若是值在集合中,返回true
,不然返回false
;對象
clear()
:移除集合中的全部項;
size()
:返回集合所包含元素的數量,與數組的length
屬性類似;
values()
:返回一個包含集合中全部值的數組;
還有其餘的方法,用的很少這裏不作封裝;
//封裝集合類 function Set() { //屬性 this.items = {} //方法 //一.has方法 Set.prototype.has = value => { return this.items.hasOwnProperty(value) } //二.add方法 Set.prototype.add = value => { //判斷集合中是否已經包含該元素 if (this.has(value)) { return false } //將元素添加到集合中 this.items[value] = value//表示該屬性鍵和值都爲value return true//表示添加成功 } //三.remove方法 Set.prototype.remove = (value) => { //1.判斷集合中是否包含該元素 if (!this.has(value)) { return false } //2.將元素從屬性中刪除 delete this.items[value] return true } //四.clear方法 Set.prototype.clear = () => { //原來的對象沒有引用指向,會被自動回收 this.items = {} } //五.size方法 Set.prototype.size = () => { return Object.keys(this.items).length } //獲取集合中全部的值 //六.values方法 Set.prototype.values = function() { return Object.keys(this.items) } }
測試代碼:
//測試集合類 //1.建立Set類對象 let set = new Set() //添加元素 //2.測試add方法 console.log(set.add('a')); //67 console.log(set.add('a')); //68 console.log(set.add('b')); //69 console.log(set.add('c')); //70 console.log(set.add('d')); //71 //3.測試values方法 console.log(set.values()); //74 //刪除元素 //4.測試remove方法 console.log(set.remove('a')); //78 console.log(set.remove('a')); //79 console.log(set.values()); //80 //5.測試has方法 console.log(set.has('b')); //83 //6.測試size方法和clear方法 console.log(set.size()); //86 set.clear() // 因爲clear方法的實現原理爲指向另一個空對象,因此不影響原來的對象 console.log(set.size()); //89 console.log(set.values()); //90
測試結果:
集合間操做:
並集的實現:
實現思路:建立集合C表明集合A和集合B的並集,先將集合A
中的全部元素添加到集合C
中,再遍歷集合B
,若是是集合C
所沒有的元素就把它添加到集合C
中。
Set.prototype.union = otherSet => { // this:集合對象A // otherSet:集合對象B //1.建立一個新的集合 let unionSet = new Set() //2.將A集合中的全部元素添加到新集合中 let values = this.values() // for(let i of values){ // unionSet.add(i) // } for(let i = 0;i < values.length;i++){ unionSet.add(values[i]) } //3.取出B集合中的元素,判斷是否須要加到新集合中 values = otherSet.values() // for(let i of values){ // //因爲集合的add方法已經對重複的元素進行了判斷,因此這裏能夠直接添加 // unionSet.add(i) // } for(let i = 0;i < values.length;i++){ unionSet.add(values[i]) } return unionSet }
交集的實現:
實現思路:遍歷集合A,當取得的元素也存在於集合B時,就把該元素添加到另外一個集合C中。
Set.prototype.intersection = otherSet => { // this:集合A // otherSet:集合B //1.建立新的集合 let intersectionSet = new Set() //2.從A中取出一個元素,判斷是否同時存在於集合B中,是則放入新集合中 let values = this.values() for(let i =0 ; i < values.length; i++){ let item = values[i] if (otherSet.has(item)) { intersectionSet.add(item) } } return intersectionSet }
差集的實現:
實現思路:遍歷集合A,當取得的元素不存在於集合B時,就把該元素添加到另外一個集合C中。
Set.prototype.diffrence = otherSet => { //this:集合A //otherSet:集合B //1.建立新的集合 var diffrenceSet = new Set() //2.取出A集合中的每個元素,判斷是否同時存在於B中,不存在則添加到新集合中 var values = this.values() for(var i = 0;i < values.length; i++){ var item = values[i] if (!otherSet.has(item)) { diffrenceSet.add(item) } } return diffrenceSet }
子集的實現:
實現思路:遍歷集合A,當取得的元素中有一個不存在於集合B時,就說明集合A不是集合B的子集,返回false。
Set.prototype.subset = otherSet => { //this:集合A //otherSet:集合B //遍歷集合A中的全部元素,若是發現,集合A中的元素,在集合B中不存在,那麼放回false,若是遍歷完整個集合A沒有返回false,就返回true let values = this.values() for(let i = 0; i < values.length; i++){ let item = values[i] if(!otherSet.has(item)){ return false } } return true }
字典的特色:
字典和映射的關係:
字典類常見的操做:
true
,反之則返回false
。length
屬性相似。字典類能夠基於JavaScript中的對象結構來實現,比較簡單,這裏直接實現字典類中的經常使用方法。
//封裝字典類 function Dictionary(){ //字典屬性 this.items = {} //字典操做方法 //一.在字典中添加鍵值對 Dictionary.prototype.set = function(key, value){ this.items[key] = value } //二.判斷字典中是否有某個key Dictionary.prototype.has = function(key){ return this.items.hasOwnProperty(key) } //三.從字典中移除元素 Dictionary.prototype.remove = function(key){ //1.判斷字典中是否有這個key if(!this.has(key)) return false //2.從字典中刪除key delete this.items[key] return true } //四.根據key獲取value Dictionary.prototype.get = function(key){ return this.has(key) ? this.items[key] : undefined } //五.獲取全部keys Dictionary.prototype.keys = function(){ return Object.keys(this.items) } //六.size方法 Dictionary.prototype.keys = function(){ return this.keys().length } //七.clear方法 Dictionary.prototype.clear = function(){ this.items = {} } }