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>();//線程安全版本的集合
            }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章