JavaScript实现集合与字典
<h2 id="javascript实现集合与字典">JavaScript实现集合与字典</h2><h3 id="一集合结构">一、集合结构</h3>
<h4 id="11简介">1.1.简介</h4>
<p>集合比较常见的实现方式是<strong>哈希表</strong>,这里使用JavaScript的Object类进行封装。</p>
<p>集合通常是由一组<strong>无序的</strong>、<strong>不能重复</strong>的元素构成。</p>
<ul>
<li>数学中常指的集合中的元素是可以重复的,但是计算机中集合的元素不能重复。</li>
</ul>
<p><strong>集合是特殊的数组:</strong></p>
<ul>
<li>特殊之处在于里面的元素<strong>没有顺序</strong>,<strong>也不能重复</strong>。</li>
<li>没有顺序意味着<strong>不能通过下标值进行访问</strong>,不能重复意味着<strong>相同的对象</strong>在集合中只会<strong>存在一份</strong>。</li>
</ul>
<p><strong>实现集合类</strong>:</p>
<ul>
<li>
<p>在ES6中的<strong>Set</strong>类就是一个集合类,这里我们重新封装一个Set类,了解集合的底层实现。</p>
</li>
<li>
<p>JavaScript中的<strong>Object</strong>类中的<strong>key</strong>就是一个集合,可以使用它来封装集合类Set。</p>
</li>
</ul>
<p><strong>集合常见的操作</strong>:</p>
<ul>
<li>
<p><code>add(value)</code>:向集合添加一个新的项;</p>
</li>
<li>
<p><code>remove(value)</code>:从集合中移除一个值;</p>
</li>
<li>
<p><code>has(value)</code>:如果值在集合中,返回<code>true</code>,否则返回<code>false</code>;</p>
</li>
<li>
<p><code>clear()</code>:移除集合中的所有项;</p>
</li>
<li>
<p><code>size()</code>:返回集合所包含元素的数量,与数组的<code>length</code>属性相似;</p>
</li>
<li>
<p><code>values()</code>:返回一个包含集合中所有值的数组;</p>
</li>
</ul>
<p>还有其他的方法,用的不多这里不做封装;</p>
<h4 id="12代码实现">1.2.代码实现</h4>
<pre><code> //封装集合类
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
return true//表示添加成功
}
//三.remove方法
Set.prototype.remove = (value) => {
//1.判断集合中是否包含该元素
if (!this.has(value)) {
return false
}
//2.将元素从属性中删除
delete this.items
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)
}
}
</code></pre>
<p><strong>测试代码:</strong></p>
<pre><code> //测试集合类
//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
</code></pre>
<p><strong>测试结果:</strong></p>
<p><img src="https://gitee.com/ahuntsun/BlogImgs/raw/master/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/%E9%9B%86%E5%90%88%E5%92%8C%E5%AD%97%E5%85%B8/1.png" alt="image-20200228183431635" loading="lazy"></p>
<h4 id="13集合间的操作">1.3.集合间的操作</h4>
<p><strong>集合间操作:</strong></p>
<ul>
<li><strong>并集</strong>:对于给定的两个集合,返回一个包含两个集合中所有元素的新集合;</li>
<li><strong>交集</strong>:对于给定的两个集合,返回一个包含两个集合中共有元素的新集合;</li>
<li><strong>差集</strong>:对于给定的两个集合,返回一个包含所有存在于第一个集合且不存在于第二个集合的元素的新集合;</li>
<li><strong>子集</strong>:验证一个给定集合是否是另一个集合的子集;</li>
</ul>
<p><img src="https://gitee.com/ahuntsun/BlogImgs/raw/master/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/%E9%9B%86%E5%90%88%E5%92%8C%E5%AD%97%E5%85%B8/2.png" alt="image-20200228210239984" loading="lazy"></p>
<p><strong>并集的实现:</strong></p>
<p>实现思路:创建集合C代表集合A和集合B的并集,先将集合<code>A</code>中的所有元素添加到集合<code>C</code>中,再遍历集合<code>B</code>,如果是集合<code>C</code>所没有的元素就把它添加到集合<code>C</code>中。</p>
<pre><code> 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)
}
//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)
}
return unionSet
}
</code></pre>
<p><strong>交集的实现:</strong></p>
<p>实现思路:遍历集合A,当取得的元素也存在于集合B时,就把该元素添加到另一个集合C中。</p>
<pre><code> 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
if (otherSet.has(item)) {
intersectionSet.add(item)
}
}
return intersectionSet
}
</code></pre>
<p><strong>差集的实现:</strong></p>
<p>实现思路:遍历集合A,当取得的元素不存在于集合B时,就把该元素添加到另一个集合C中。</p>
<pre><code>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
if (!otherSet.has(item)) {
diffrenceSet.add(item)
}
}
return diffrenceSet
}
</code></pre>
<p><strong>子集的实现:</strong></p>
<p>实现思路:遍历集合A,当取得的元素中有一个不存在于集合B时,就说明集合A不是集合B的子集,返回false。</p>
<pre><code> 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
if(!otherSet.has(item)){
return false
}
}
return true
}
</code></pre>
<h3 id="二字典结构">二、字典结构</h3>
<h4 id="21简介">2.1.简介</h4>
<p><strong>字典的特点</strong>:</p>
<ul>
<li>字典存储的是键值对,主要特点是一一对应;</li>
<li>比如保存一个人的信息:数组形式:,可通过下标值取出信息;字典形式:{"age":19,"name":"Tom","height":165},可以通过key取出value。</li>
<li>此外,在字典中<strong>key</strong>是<strong>不能重复</strong>且<strong>无序</strong>的,而<strong>Value</strong>可以<strong>重复</strong>。</li>
</ul>
<p><strong>字典和映射的关系</strong>:</p>
<ul>
<li>有些编程语言中称这种<strong>映射关系</strong>为<strong>字典</strong>,如Swift中的Dictonary,Python中的dict;</li>
<li>有些编程语言中称这种<strong>映射关系</strong>为<strong>Map</strong>,比如Java中的HashMap&TreeMap等;</li>
</ul>
<p><strong>字典类常见的操作</strong>:</p>
<ul>
<li>set(key,value):向字典中添加新元素。</li>
<li>remove(key):通过使用键值来从字典中移除键值对应的数据值。</li>
<li>has(key):如果某个键值存在于这个字典中,则返回<code>true</code>,反之则返回<code>false</code>。</li>
<li>get(key):通过键值查找特定的数值并返回。</li>
<li>clear():将这个字典中的所有元素全部删除。</li>
<li>size():返回字典所包含元素的数量。与数组的<code>length</code>属性类似。</li>
<li>keys():将字典所包含的所有键名以数组形式返回。</li>
<li>values():将字典所包含的所有数值以数组形式返回。</li>
</ul>
<h4 id="22封装字典">2.2.封装字典</h4>
<p>字典类可以基于JavaScript中的对象结构来实现,比较简单,这里直接实现字典类中的常用方法。</p>
<pre><code>//封装字典类
function Dictionary(){
//字典属性
this.items = {}
//字典操作方法
//一.在字典中添加键值对
Dictionary.prototype.set = function(key, value){
this.items = 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
return true
}
//四.根据key获取value
Dictionary.prototype.get = function(key){
return this.has(key) ? this.items : 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 = {}
}
}
</code></pre>
<blockquote>
<p>参考资料:JavaScript数据结构与算法</p>
</blockquote>
</div>
<div id="MySignature" role="contentinfo">
多抽出1分钟来学习,让你的生命更加精彩!<br><br>
来源:https://www.cnblogs.com/AhuntSun-blog/p/12474890.html
頁:
[1]