标签:union fun 完整 形式 比较 属性 strong prot 取出
集合比较常见的实现方式是哈希表,这里使用JavaScript的Object类进行封装。
集合通常是由一组无序的、不能重复的元素构成。
集合是特殊的数组:
实现集合类:
在ES6中的Set类就是一个集合类,这里我们重新封装一个Set类,了解集合的底层实现。
JavaScript中的Object类中的key就是一个集合,可以使用它来封装集合类Set。
集合常见的操作:
add(value):向集合添加一个新的项;
remove(value):从集合中移除一个值;
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 = {}
  }
}标签:union fun 完整 形式 比较 属性 strong prot 取出
原文地址:https://www.cnblogs.com/AhuntSun-blog/p/12474890.html