• C#中HashSet<T>、SortedSet<T>和Hashtable的使用以及所有集合类型的概述


    本文主要介绍.NET(C#)中,HashSet<T>、SortedSet<T>和Hashtable的使用,以及相关的示例代码。

    1、HashSet<T>

    HashSet<T>类提供高性能的设置操作。 集是不包含重复元素的集合,其元素无特定顺序。泛型的使用保证类型安全,可以避免装箱拆箱。对象的容量 HashSet<T> 是对象可以容纳的元素数。 HashSet<T>当向对象添加元素时,对象的容量会自动增加。两个集合求交集、并集、差集和补集等操作。

    例如:

    Console.WriteLine("***************HashSet<string>******************");
    HashSet<string> hashSet = new HashSet<string>();
    hashSet.Add("C#");
    hashSet.Add("C/C++");
    hashSet.Add("Java");
    hashSet.Add("Python");
    hashSet.Add("Python");
    hashSet.Add("Python");
    //hashSet[0];
    foreach (var item in hashSet)
    {
        Console.WriteLine(item);
    }
    Console.WriteLine(hashSet.Count);
    Console.WriteLine(hashSet.Contains("Python"));
    HashSet<string> hashSet1 = new HashSet<string>();
    hashSet1.Add("C#");
    hashSet1.Add("C/C++");
    hashSet1.Add("Java");
    hashSet1.Add("Python");
    hashSet1.Add("Python");
    hashSet1.Add("Python");
    hashSet1.SymmetricExceptWith(hashSet);//补
    hashSet1.UnionWith(hashSet);//并
    hashSet1.ExceptWith(hashSet);//差
    hashSet1.IntersectWith(hashSet);//交
    hashSet.ToList();
    hashSet.Clear();

    2、SortedSet<T>

    SortedSet<T>表示按排序顺序维护的对象的集合。SortedSet<T> 对象在插入和删除元素时维护排序顺序,而不会影响性能。

    例如:

    Console.WriteLine("***************SortedSet<string>******************");
    SortedSet<string> sortedSet = new SortedSet<string>();
    //IComparer<T> comparer  自定义对象要排序,就用这个指定
    sortedSet.Add("C#");
    sortedSet.Add("C/C++");
    sortedSet.Add("Java");
    sortedSet.Add("Python");
    sortedSet.Add("Python");
    sortedSet.Add("Python");
    foreach (var item in sortedSet)
    {
        Console.WriteLine(item);
    }
    Console.WriteLine(sortedSet.Count);
    Console.WriteLine(sortedSet.Contains("Python"));
    {
        SortedSet<string> sortedSet1 = new SortedSet<string>();
        sortedSet1.Add("C#");
        sortedSet1.Add("C/C++");
        sortedSet1.Add("Java");
        sortedSet1.Add("Python");
        sortedSet1.Add("Python");
        sortedSet1.Add("Python");
        sortedSet1.SymmetricExceptWith(sortedSet);//补
        sortedSet1.UnionWith(sortedSet);//并
        sortedSet1.ExceptWith(sortedSet);//差
        sortedSet1.IntersectWith(sortedSet);//交
    }
    sortedSet.ToList();
    sortedSet.Clear();

    3、Hashtable

    Hashtable表示根据键的哈希代码进行组织的键/值对的集合。任何元素都是当成object处理,如果是值类型,会有装箱操作。不推荐使用 Hashtable 类进行新的开发。 推荐使用泛型 Dictionary<TKey,TValue> 类。

    例如:

    Console.WriteLine("***************Hashtable******************");
    Hashtable table = new Hashtable();
    table.Add("code", "C#");
    table[1011] = "Java";
    table[1012] = "Python";
    table[1014] = 3333;
    table[1015] = 4444;
    table["cjavapy"] = 5457;
    foreach (DictionaryEntry objDE in table)
    {
        Console.WriteLine(objDE.Key.ToString());
        Console.WriteLine(objDE.Value.ToString());
    }
    //线程安全,Hashtable.Synchronized(table)返回 Hashtable 的同步(线程安全)包装。
    var shash = Hashtable.Synchronized(table);//只有一个线程写  多个线程读
    //显示两个哈希表的同步状态
    Console.WriteLine("table: {0}", table.IsSynchronized ? "synchronized" : "not synchronized" );
    Console.WriteLine("shash: {0}", shash.IsSynchronized ? "synchronized" : "not synchronized");

    原文地址https://www.cjavapy.com/article/2510/

    C#集合类型概述
    集合是.NET FCL(Framework Class Library)中很重要的一部分。所有的集合类都继承自IEnumerable。集合类总体可分为一下几类:关联/非关联型集合,顺序/随机访问集合,顺序/无序集合,泛型/非泛型集合,线程安全集合。

    各集合类底层接口关系图

    原文地址https://www.cnblogs.com/shizheng0909/p/14451698.html

  • 相关阅读:
    android如何隐藏通知栏和禁止横屏竖屏切换
    android webview加载网页,文字listview和image混合listview的实现
    PHP加密3DES报错 Call to undefined function: mcrypt_module_open() 的解决方法
    http和HTTPS的区别及SSL介绍
    android SQLite数据库 一次性存储多条数据
    两个android程序间的相互调用(apk互调)
    android在学习——activity关闭和dialog.dismiss冲突的解决(Activity has leaked window com.android.internal.policy.impl.PhoneWindow)
    【android】解决因为图片太大引起的内存不足问题,Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
    android数据库操作 sqlite returned: error code = 14, msg = cannot open file at source line 25502
    android实现对SQLite数据库的增、删、改、查
  • 原文地址:https://www.cnblogs.com/east115/p/16116042.html
Copyright © 2020-2023  润新知