【Unity3D数据集合】(六)散列集合HashSet和排序集合SortedSet学习

推荐阅读

大家好,我是佛系工程师☆恬静的小魔龙☆,不定时更新Unity开发技巧,觉得有用记得一键三连哦。

一、前言

在日常开发中,常常会用到数据集合,那么数据集合是什么呢,数据集合也没有想象中那么复杂。

数据集合就是专门用来存储数据、检索数据,以及对数据一系列操作的类。

这些类有:ArrayList数组、List列表、Queue队列、Dictionary字典、Hashtable哈希表、Stack堆栈。

在开发中,每种数据集合都有优缺点,今天就将这些数据集合进行归纳总结。

一是方便自己捋顺思路,二是可以帮助到对此理解不清晰的开发者。

这是本系列文章的第六篇:
【Unity3D数据集合】(一)数组集合Array学习
【Unity3D数据集合】(二)列表集合List及ListArray学习
【Unity3D数据集合】(三)字典Dictionary和哈希表Hashtable学习
【Unity3D数据集合】(四)堆栈Stack和队列Queue学习
【Unity3D数据集合】(五)链表LinkedList数据集合学习
【Unity3D数据集合】(六)散列集合HashSet和排序集合SortedSet学习
【Unity3D数据集合】(七)排序列表SortedList和排序字典SortedDictionary学习
【Unity3D数据集合】(八)点阵列BitArray学习

二、散列集合HashSet

散列集合HashSet介绍

散列集合HashSet是一种数据结构,支持泛型。

HashSet包含一组不重复出现且无特性顺序的元素,不支持虫谷的对象

HashSet的值不重复且没有顺序,容量会按需自动增加。

这个类主要是设计用来做高性能集运算的,例如对两个集合求交集、差集、并集、补集等。

接下来就演示一下,这个HashSet的交、叉、并、补功能:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Demo5 : MonoBehaviour
{
    //HashSet<string> table;
    void Start()
    {
        HashSet<string> hashSet = new HashSet<string>();
        hashSet.Add("1111");
        hashSet.Add("2222");
        hashSet.Add("3333");
        hashSet.Add("A12435");
        hashSet.Add("B12435");
        hashSet.Add("C12435");

        HashSet<string> hashSet1 = new HashSet<string>();
        hashSet1.Add("1111");
        hashSet1.Add("1111");
        hashSet1.Add("1111");
        hashSet1.Add("2222");
        hashSet1.Add("3333");
        hashSet1.Add("a12435");
        hashSet1.Add("b12435");
        hashSet1.Add("c12435");

        HashSet<string> hashSet2 = new HashSet<string>();
        hashSet2.Add("1111");
        hashSet2.Add("1111");
        hashSet2.Add("1111");
        hashSet2.Add("2222");
        hashSet2.Add("3333");
        hashSet2.Add("a12435");
        hashSet2.Add("b12435");
        hashSet2.Add("c12435");

        HashSet<string> hashSet3 = new HashSet<string>();
        hashSet3.Add("1111");
        hashSet3.Add("1111");
        hashSet3.Add("1111");
        hashSet3.Add("2222");
        hashSet3.Add("3333");
        hashSet3.Add("a12435");
        hashSet3.Add("b12435");
        hashSet3.Add("c12435");

        HashSet<string> hashSet4 = new HashSet<string>();
        hashSet4.Add("1111");
        hashSet4.Add("1111");
        hashSet4.Add("1111");
        hashSet4.Add("2222");
        hashSet4.Add("3333");
        hashSet4.Add("a12435");
        hashSet4.Add("b12435");
        hashSet4.Add("c12435");

        Debug.Log("计算交集开始");
        hashSet1.IntersectWith(hashSet);//交集(hashSet1与hashSet共有的元素集合,并赋值给hashSet1)
        foreach (var item in hashSet1)
        {
            Debug.Log(item);
        }
        Debug.Log("计算交集结束");

        Debug.Log("计算补集开始");
        hashSet2.SymmetricExceptWith(hashSet);//补集(除共有意外的所有元素集合,并赋值给hashSet2)
        foreach (var item in hashSet2)
        {
            Debug.Log(item);
        }
        Debug.Log("计算补集结束");

        Debug.Log("计算并集开始");
        hashSet3.UnionWith(hashSet);//并集(两个集合含有的所有元素,并赋值给hashSet3)
        foreach (var item in hashSet3)
        {
            Debug.Log(item);
        }
        Debug.Log("计算并集结束");

        Debug.Log("计算差集开始");
        hashSet4.ExceptWith(hashSet);//差集(hashSet1有而hashSet没有的元素集合,并赋值给hashSet4)
        foreach (var item in hashSet4)
        {
            Debug.Log(item);
        }
        Debug.Log("计算差集结束");
    }
}

三、排序集合SortedSet

有序集合SortedSet,也具有交、叉、并、补功能,并且可以自动排序。

SortedSet对象在插入和删除元素时会维护排序顺序,而不会影响性能。

SortedSet的优点就是更新排序时没有性能损耗,缺点就是不允许重复元素,不支持更改现有项的排序。

下面这个例子演示了自动排序:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Demo5 : MonoBehaviour
{
    void Start()
    {
        SortedSet<string> sortedSet = new SortedSet<string>();
        sortedSet.Add("a123456");
        sortedSet.Add("b123456");
        sortedSet.Add("c123456");
        sortedSet.Add("12435");
        sortedSet.Add("12435");
        sortedSet.Add("12435");

        foreach (var item in sortedSet)
        {
            Debug.Log(item);
        }
    }
}
  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

恬静的小魔龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值