C#中集合总结

1 数组型(Array)

1.1 Array ArrayList List

{
    //Array:在内存上连续分配的,而且元素类型是一样的
    //可以用索引访问,读取快,增删慢 长度不变
    int[] intArray = new int[3];
    intArray[0] = 123;
    string[] strArray = new string[] { "123","456"};
}

{
    //ArrayList 不定长  连续分配的
    //元素没有类型的限制,任何元素都是当成object处理,如果是值类型的话,会有装箱操作
    //读取快,增删慢
    ArrayList arrayList = new ArrayList();
    arrayList.Add("song");
    arrayList.Add("fsd");
    arrayList.Add(31);
    arrayList[2] = 34;//只能用于元素的修改  不能用于元素的增加

    arrayList.RemoveAt(0);
    arrayList.Remove("fsd");
    arrayList.Remove("s");//元素不存在,不会抛异常
}

{
    //List 也是Array 内存上是连续 不定长;泛型:保证类型安全,避免拆箱与装箱
    //读取快,增删慢
    List<int> list = new List<int>() { 1, 2, 3, 4 };
    list.Add(123);
    list.Add(456);
    list[6] = 1213;//只能用于元素的修改  不能用于元素的增加
}

2 链表型(Linked)

{
    //基于链表实现 泛型特点;链表,元素不连续分配 每个元素都有记录前后节点
    //不能通过索引访问  找元素不方便 
    //增删比较方便
    LinkedList<int> linkedList = new LinkedList<int>();
    linkedList.AddFirst(123);
    linkedList.AddLast(456);

    bool isContain =  linkedList.Contains(123);
    LinkedListNode<int> node =  linkedList.Find(123);
    linkedList.AddBefore(node, 0);

    linkedList.AddAfter(node, 9);

    linkedList.Remove(456);
    linkedList.Remove(node);
    linkedList.RemoveFirst();
    linkedList.RemoveLast();
    linkedList.Clear();
}

{
    //Queue :队列 基于链表实现 先进先出 
    //应用场景:放任务延迟执行  A不断写任务,B不断获取任务去执行
    Queue<string> strQueue = new Queue<string>();
    strQueue.Enqueue("one");
    strQueue.Enqueue("two");
    strQueue.Enqueue("three");
    strQueue.Enqueue("four");
    strQueue.Enqueue("five");

    foreach (string num in strQueue)
    {
        Console.WriteLine(num);
    }

    Console.WriteLine($"Dequeue:{strQueue.Dequeue()}");
    Console.WriteLine($"Peek at next item to dequeue:{strQueue.Peek()}");
    Console.WriteLine($"Dequeue:{strQueue.Dequeue()}");

    Queue<string> copyQueue = new Queue<string>(strQueue);
    foreach (var item in copyQueue)
    {
        Console.WriteLine(item);
    }
    Console.WriteLine($"queueCopy.Contains():{copyQueue.Contains("three")}");
    Console.WriteLine($"copyQueue.Count = {copyQueue.Count}");

}

{
    //
    //Stack :栈 基于链表实现 先进后出 
    //应用场景:解析表达式目录树,先产生数据后使用
    Stack<string> strStack = new Stack<string>();
    strStack.Push("one");
    strStack.Push("two");
    strStack.Push("three");
    strStack.Push("four");
    strStack.Push("five");

    foreach (string num in strStack)
    {
        Console.WriteLine(num);
    }

    Console.WriteLine($"Pop:{strStack.Pop()}");//获取并移除
    Console.WriteLine($"Pop at next item to dequeue:{strStack.Peek()}");//获取不移除
    Console.WriteLine($"Pop:{strStack.Pop()}");

    Stack<string> copyStack = new Stack<string>(strStack);//猜测:从另一个栈中复制时,进行了出栈,入栈的操作
    foreach (var item in copyStack)
    {
        Console.WriteLine(item);
    }
    Console.WriteLine($"copyStack.Contains():{copyStack.Contains("three")}");
    Console.WriteLine($"copyStack.Count = {copyStack.Count}");
}

3 hash

{
    //集合:hash分布,元素间没有关系,动态增加容量  去重
    //统计IP 交、叉、并、补    
    HashSet<string> hashSet = new HashSet<string>();
    hashSet.Add("123");
    hashSet.Add("two");
    hashSet.Add("three");
    hashSet.Add("four");
    hashSet.Add("five");

    foreach (var item in hashSet)
    {
        Console.WriteLine(item);
    }
    Console.WriteLine(hashSet.Count);
    Console.WriteLine(hashSet.Contains("one"));

    {
        HashSet<string> hashSet1 = new HashSet<string>();
        hashSet1.Add("123");
        hashSet1.Add("1234");
        hashSet1.Add("1235");
        hashSet1.Add("1234");
        hashSet1.Add("1236");
        hashSet1.SymmetricExceptWith(hashSet);//补
        hashSet1.UnionWith(hashSet);//并
        hashSet1.ExceptWith(hashSet);//差
        hashSet1.IntersectWith(hashSet);//交
        hashSet1.ToList();
        hashSet1.Clear();
     }
}

{
    //排序集合:去重 并且排序
    //统计排名:
    SortedSet<string> sortedSet = new SortedSet<string>();
    //自定义对象,要排序用这个IComparer<T> comparer 指定
    sortedSet.Add("123");
    sortedSet.Add("1234");
    sortedSet.Add("1235");
    sortedSet.Add("1234");
    sortedSet.Add("1236");

    foreach (var item in sortedSet)
    {
        Console.WriteLine(item);
    }
    Console.WriteLine(sortedSet.Count);
    Console.WriteLine(sortedSet.Contains("123"));


}


{
    //hashTable key-value 体积可以动态增加 用key计算一个地址  然后放入key-value
    //object 装箱与拆箱   如果不同的key得到相同的值  第二个在前面地址上+1
    //查找的时候,如果地址对应数据的key不同,那就+1查找
    //浪费了空间 Hashtable 是基于数组实现的
    //查找数据  一次定位  增删一次定位  增删查找都很快
    //浪费空间  数据太多  重复定位  效率就下去了
    Hashtable hashTable = new Hashtable();
    hashTable.Add("123","456");
    hashTable[234] = 4556;
    hashTable[22] = 234;
    hashTable[1] = 456;
    hashTable[67] = 456;
    foreach (DictionaryEntry item in hashTable)
    {
        Console.WriteLine(item.Key+" = "+item.Value);
    }
    //线程安全
   Hashtable hs =  Hashtable.Synchronized(hashTable);//只有一个线程写,多个线程读

}

{
    //泛型 Key-Value : 增删查改  都很快 有序的
    Dictionary<int, string> dic = new Dictionary<int, string>();
    dic.Add(1,"Hello");
    dic.Add(2, "World");
    dic.Add(3, "C#");
    dic.Add(4, ".NET");
    dic.Add(5, "Big");

    dic[4] = "张三";

    foreach (var item in dic)
    {
        Console.WriteLine(item.Key+"="+item.Value);
    }
}

{
    //排序的SortedDictionary
    SortedDictionary<int, string> dic = new SortedDictionary<int, string>();
    dic.Add(1, "Hello");
    dic.Add(2, "World");
    dic.Add(3, "C#");
    dic.Add(4, ".NET");
    dic.Add(5, "Big");
    dic[4] = "张三";

    foreach (var item in dic)
    {
        Console.WriteLine(item.Key + "=" + item.Value);
    }

}

{
    SortedList sortList = new SortedList();
    sortList.Add("First", "Hello");
    sortList.Add("Second", "world");
    sortList.Add("Thrid", "1");
    sortList["Thrid"] = "---";

    IList keyList = sortList.GetKeyList();
    IList valueList =  sortList.GetValueList();

    sortList.TrimToSize();//将容量设置为 System.Collections.SortedList 对象中元素的实际数目。 最小化集合的内存开销

    sortList.Remove("Second");
    sortList.RemoveAt(0);
    sortList.Clear();
}

4 线程安全的集合

 {
                //线程安全版本的各种容器
                //ConcurrentQueue<string> ConcurrentQueue = new ConcurrentQueue<string>(); //线程安全版本的Queue
                //ConcurrentStack<string> ConcurrentStack = new ConcurrentStack<string>();//线程安全版本的Stack
                //ConcurrentBag<string> ConcurrentBag = new ConcurrentBag<string>();//线程安全版本的无序集合
                //ConcurrentDictionary<string, string> ConcurrentDictionary = new ConcurrentDictionary<string, string>();//线程安全版本的字典
                //BlockingCollection<string> BlockingCollection = new BlockingCollection<string>();//线程安全版本的集合
            }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章