集合比较常见的实现方式是哈希表,这里使用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 = {} } }