網站首頁 編程語言 正文
本篇體驗除Queue<T>和Stack<T>之外的其它泛型集合。
SortedList<TKey, TValue>
SortedList<TKey, TValue>和List<T>比較相似,不同的地方在于SortedList集合元素是排過序的,往SortedList集合添加元素的時候需要添加鍵值對數據。在添加集合元素的時候,首先采用"二分查找算法"找到合適的位置,然后元素被放到該位置,該位置后面所有的集合元素整體后退一位。
static void Main(string[] args)
{
var mySotedList = new SortedList<int, string>();
mySotedList.Add(1, "張三");
mySotedList.Add(2,"李四");
mySotedList.Add(3,"趙六");
mySotedList.Add(4,"王九");
//判斷是否包含某個鍵
mySotedList.ContainsKey(1);
//判斷是否包含某個值
mySotedList.ContainsValue("張三");
//獲取某個鍵的索引
int keyIndex = mySotedList.IndexOfKey(2);
//獲取某個值的索引
int valIndex = mySotedList.IndexOfValue("李四");
//根據鍵刪除
mySotedList.Remove(3);
//根據索引刪除
mySotedList.RemoveAt(1);
//根據鍵查找元素
string myVal = mySotedList[3];
//根據索引查找元素
string myVal1 = mySotedList.Values[1];
//根據索引查找鍵
int myKey = mySotedList.Keys[3];
//獲取某個元素而不拋異常
string myWantVal;
if (mySotedList.TryGetValue(2, out myWantVal))
{
}
//遍歷鍵
foreach (int key in mySotedList.Keys)
{
}
//遍歷值
foreach (string str in mySotedList.Values)
{
}
}
使用SortedList<TKey, TValue>相對不足的地方在于:當插入數據的時候,是通過"二分查找算法"確定插入位置的,開銷相對昂貴;但,由于SortedList<TKey, TValue>集合元素是排過序的,這又讓查找變得方便。
如果需要對一個集合進行經常性的查找,而向集合插入數據相對不多,就可以考慮使用SortedList<TKey, TValue>。????
Dictionary<TKey,TValue>
Dictionary<TKey,TValue>把存儲的數據放在了一個HashTable中,每個集合元素的鍵必須是唯一的,且集合元素未經排序。
與SortedList<TKey, TValue>相似的地方在于也是以鍵值對的形式添加集合元素的,不同的地方在于:插入數據的效率Dictionary<TKey,TValue>比SortedList<TKey, TValue>要高。
var myDictionary = new Dictionary<int, string>();
myDictionary.Add(1, "darren");
myDictionary.Add(2, "jack");
myDictionary.Add(3, "sunny");
//根據鍵判斷是否存在
myDictionary.ContainsKey(1);
//根據值判斷是否存在
myDictionary.ContainsValue("darren");
//根據鍵刪除
myDictionary.Remove(3);
//根據鍵查找集合元素
string myVal = myDictionary[2];
//根據鍵查找元素不拋異常
string myVal1;
if (myDictionary.TryGetValue(2, out myVal1))
{
}
//遍歷鍵
foreach (int key in myDictionary.Keys)
{
}
//遍歷值
foreach (string value in myDictionary.Values)
{
}
SortedDictionary<TKey,TValue>
SortedDictionary<TKey,TValue>與SortedList<TKey, TValue>最大的不同在于:SortedDictionary<TKey,TValue>不允許通過索引獲取集合元素,其內部維護著一個"紅黑樹",所以,當執行插入操作的時候,相比SortedList<TKey, TValue>,SortedDictionary<TKey,TValue>有更好的執行效率,當然也占用了更多的內存。
var mySortedDictionary = new SortedDictionary<int, string>();
mySortedDictionary.Add(1,"one");
mySortedDictionary.Add(2,"two");
mySortedDictionary.Add(3,"three");
//判斷是否包含某個鍵
mySortedDictionary.ContainsKey(2);
//判斷是否包含某個值
mySortedDictionary.ContainsValue("two");
//根據鍵移除某個元素
mySortedDictionary.Remove(2);
//根據鍵查找某個元素
string myVal = mySortedDictionary[1];
//根據鍵查找元素,不拋異常
string myVal1;
if (mySortedDictionary.TryGetValue(1, out myVal1))
{
}
//遍歷所有鍵
foreach (int key in mySortedDictionary.Keys)
{
}
//遍歷所有值
foreach (string val in mySortedDictionary.Values)
{
}
LinkedList<T>
每個集合元素都有屬性指向前一個和后一個節點。第一個的前一個節點是最后一個節點,后一個節點是第二個節點。最后一個節點的前一個節點是倒數第二個節點,后一個節點是第一個節點。
var myLinkedList = new LinkedList<string>();
//創建第一個節點
myLinkedList.AddFirst("one");
//創建最后一個節點
var lastNode = myLinkedList.AddLast("three");
//在最后一個節點前添加一個節點
myLinkedList.AddBefore(lastNode, "two");
//在當前最后一個節點后面添加一個節點
myLinkedList.AddAfter(lastNode, "four");
//遍歷節點值
foreach (string value in myLinkedList)
{
}
//根據值查找節點
//找到符合條件的第一個節點
var myNode = myLinkedList.Find("two");
//根據值查找節點
//找到符合條件的最后一個節點
var myNode1 = myLinkedList.FindLast("one");
//獲取第一個節點
var firstNode = myLinkedList.First;
//獲取最后一個節點
var lastNode1 = myLinkedList.Last;
//根據值產生節點
myLinkedList.Remove("one");
//刪除第一個節點
myLinkedList.RemoveFirst();
//刪除最后一個節點
myLinkedList.RemoveLast();
//清空所有節點
myLinkedList.Clear();
如果對一個集合有很多的插入操作,或者插入批量集合元素,可以考慮使用LinkedList<T>。
SortedSet<T>
SortedSet<T>的內部其實是SortedDictionary類型,但是沒有鍵,只有值。SortedSet代表一個抽象的數據集,數據集中的元素值必須是唯一的,未經過排序的。
如果需要對一組數據進行合并等操作,可以考慮使用SortedSet<T>。
var sortedSet1 = new SortedSet<string>();
sortedSet1.Add("one");
sortedSet1.Add("two");
sortedSet1.Add("three");
var sortedSet2 = new SortedSet<string>();
sortedSet2.Add("two");
sortedSet2.Add("four");
//判斷某個值是否存在
sortedSet1.Contains("one");
//合并數據集,取出重復項
sortedSet1.ExceptWith(sortedSet2);
//判斷一個數據集是否是另一個數據集的自己
sortedSet1.IsSubsetOf(sortedSet2);
//判斷一個i額數據集是否包含另一個數據集的所有元素
sortedSet1.IsSubsetOf(sortedSet2);
//判斷兩個數據集是否有交集
sortedSet1.Overlaps(sortedSet2);
//根據值刪除某個元素
sortedSet1.Remove("two");
//判斷兩個數據集是否相等
sortedSet1.SetEquals(sortedSet2);
//只包含在一個數據集中的元素,這些元素不包含在另一個數據集
sortedSet1.SymmetricExceptWith(sortedSet2);
//取兩個數據集的并集
sortedSet1.UnionWith(sortedSet2);
//獲取包含某些元素的數據集
SortedSet<string> result = sortedSet1.GetViewBetween("one", "two");
//遍歷數據集
foreach (string val in sortedSet1)
{
}
//清空數據集
sortedSet1.Clear();
HashSet<T>
HashSet和SortedSet都實現了ISet接口。使用SortedSet的前提是:需要根據多個元素值作為查找條件;數據不能被哈希。其余情況應考慮使用HashSet。
var hashSet1 = new HashSet<string>();
hashSet1.Add("one");
hashSet1.Add("two");
hashSet1.Add("three");
var hashSet2 = new HashSet<string>();
hashSet2.Add("two");
hashSet2.Add("four");
//判斷是否包含某個值
hashSet1.Contains("four");
//去掉一個數據集中與另一個數據集重復的項
hashSet1.ExceptWith(hashSet2);
//保留一個數據集中與另一個數據集相同的項
hashSet1.IntersectWith(hashSet2);
//判斷一個數據集是否是另一個數據集的子集
hashSet1.IsSubsetOf(hashSet2);
//判斷一個數據集是否包含另一個數據集的所有元素
hashSet1.IsSupersetOf(hashSet2);
//判斷兩個數據集是否有重復的元素
hashSet1.Overlaps(hashSet2);
//根據值刪除某個元素
hashSet1.Remove("one");
//判斷兩個數據集是否相等
hashSet1.SetEquals(hashSet2);
//合并兩個數據集
hashSet1.UnionWith(hashSet1);
//查找只包含在一個數據集中,卻不包含在另一個數據集的元素
hashSet1.SymmetricExceptWith(hashSet2);
//遍歷數據集
foreach (string value in hashSet1)
{
}
//清空數據集
hashSet1.Clear();
這幾天體驗了各個泛型集合的用法。總結如下:
Stack<T>先進后出, 在這里。
Queue<T>先進先出,在這里。
SortedList<TKey, TValue>,插入數據不是很頻繁,且經常需要查找。
Dictionary<TKey,TValue>,經常插入數據,不排序,鍵值對。
SortedDictionary<TKey,TValue>,經常插入數據,排序,鍵值對。
LinkedList<T>,雙向鏈表,插入很隨意。
SortedSet<T>,數據集,根據多個元素值作為查找條件;數據不能被哈希。
HashSet<T>,數據集,大多數情況下使用HashSet處理數據集操作。
原文鏈接:https://www.cnblogs.com/darrenji/p/4525834.html
相關推薦
- 2023-11-14 樹莓派以及linux ubuntu 上,各種依賴不滿足,修復不了:E: Release file f
- 2022-02-20 uni-app checkbox全選功能
- 2023-05-10 淺談numpy廣播機制_python
- 2021-09-25 Flutter實現底部彈窗效果_Android
- 2022-12-27 教你使用Psycopg2連接openGauss的方法_python
- 2023-04-22 GO的range如何使用詳解_Golang
- 2022-10-24 Numpy?數據處理?ndarray使用詳解_python
- 2022-05-13 annot read properties of undefined (reading ‘split
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支