C# Collections
1. Generic
1.1 List<T>
No need to say this is the most commonly used data structure in C# collections. Where this collection maintains the order of the added items, therefore indexable. Also easy to query and add/update/remove items. Though it is not thread-safe and requires lock when manipulated across different threads.
1.2 Dictionary<TKey, TValue>
Another commonly used data structure. Where gives an unordered set of KeyValuePair. Allow the user to access values through specific keys. Also provides ValueCollection on Keys and Values which both are enumerable.
1.3 HashSet<T>
Without the need to enumerate or filter. Working with specific methods of sets to union or intersect values. Locate a value more efficient.
2. Sorted
2.1 SortedSet<T>
This data structure gives a collection the ability to maintain in sorted order based on an IComparer. Also provides an additional set of features for the user to play with, such as subsets and intersect/unions.
2.2 OrderedDictionary<TKey, TValue>
This data structure keeps the KeyValuePair items of the dictionary in the order they were added. Which is similar to List\<T\> when keeping the order of the added items, and indexable.
2.3 SortedDictionary<TKey, TValue>
While SortedList and SortedList<TKey, TValue> is similar, this data structure is sorted by TKey's value in general.
3. Concurrent
3.1 ConcurrentQueue<T>
Ordered set of items that is thread-safe. Provides the basic ability to add/update/remove/enumerate, put can only add/remove in a first-in first-out fashion. Which gives a certain degree of limitation for users whom are used to Lists and Dictionaries.
3.2 ConcurrentStack<T>
ConcurrentQueue but in first-in last-out pattern.
3.3 ConcurrentDictionary<TKey, TValue>
Thread-safe dictionaries, hands off one of the best data structures. Also provides an additional set of features for the user to play with, such as AddOrUpdate/GetOrAdd in database style, and TryUpdate/TryRemove which really is trying since they can be called over multiple threads. But to be aware that the data structure itself as an KeyPair Enumerable does not provide enumeration thread-safety, i.e. extension method `Where` would not be thread-safe. Whereas with applying locks, `ToArray()` and `Values` are safe due to their custom implementations within ConcurrentDictionary. Therefore when calling `ToArray().Where` or `Values.Where`, snapshots are provided before enumeration, so while enumerating through the collection, new item adds or removes does not apply to the current enumeration.
Take a look at: https://github.com/dotnet/runtime/blob/main/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentDictionary.cs
3.4 BlockingCollection<T>
The magic behind IProducerConsumerCollection which is used through all concurrent collections. GetConsumingEnumerable blocks the enumeration if there are no items in the collection, and continues to consume, or we can call it take(get and remove an item from the queue first-in first-out), from the enumeration once a new item is added(published) to the collection. Only when a cancellation token is requested or CompleteAdding is called, does the enumeration breaks and ends consuming.
C# Collections的更多相关文章
- Java基础Map接口+Collections工具类
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
- Java基础Map接口+Collections
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
- 计算机程序的思维逻辑 (54) - 剖析Collections - 设计模式
上节我们提到,类Collections中大概有两类功能,第一类是对容器接口对象进行操作,第二类是返回一个容器接口对象,上节我们介绍了第一类,本节我们介绍第二类. 第二类方法大概可以分为两组: 接受其他 ...
- 2DToolkit官方文档中文版打地鼠教程(三):Sprite Collections 精灵集合
这是2DToolkit官方文档中 Whack a Mole 打地鼠教程的译文,为了减少文中过多重复操作的翻译,以及一些无必要的句子,这里我假设你有Unity的基础知识(例如了解如何新建Sprite等) ...
- 计算机程序的思维逻辑 (53) - 剖析Collections - 算法
之前几节介绍了各种具体容器类和抽象容器类,上节我们提到,Java中有一个类Collections,提供了很多针对容器接口的通用功能,这些功能都是以静态方法的方式提供的. 都有哪些功能呢?大概可以分为两 ...
- Collection和Collections的区别?
Collection 是接口(Interface),是集合类的上层接口. Collections是类(Class),集合操作的工具类,服务于Collection框架.它是一个算法类,提供一系列静态方法 ...
- Collections.shuffle
1.Collections.shuffler 最近有个需求是生成十万级至百万级的所有随机数,最简单的思路是一个个生成,生成新的时候排重,但是这样时间复杂度是o(n^2),网上看了几个博客的解决方法都不 ...
- 集合工具类:collections
collection与collections的关系? public class Collectionsextends Object collection与collections没有直接的关系,但是与集 ...
- Map集合及与Collection的区别、HashMap和HashTable的区别、Collections、
特点:将键映射到值的对象,一个映射不能包含重复的键,每个键最多只能映射到一个值. Map集合和Collection集合的区别 Map集合:成对出现 (情侣) ...
- python collections模块
collections模块基本介绍 collections在通用的容器dict,list,set和tuple之上提供了几个可选的数据类型 namedtuple() factory function f ...
随机推荐
- Springfox与SpringDoc——swagger如何选择(SpringDoc入门)
本文分享自天翼云开发者社区@<Springfox与SpringDoc--swagger如何选择(SpringDoc入门)>,作者: 才开始学技术的小白 0.引言 之前写过一篇关于swagg ...
- Atcoder Regular Contest 093 C - Bichrome Spanning Tree
给定一张图,对图上边黑白染色,使得同时选择了两种颜色边的最小生成树边权和为X,求染色方案数. 先求出图的\(mst\)大小,然后分三类讨论: 1.\(X<mst\) 无解 2.\(X==mst\ ...
- PHP开发者交流群
PHP开发者交流群 欢迎大家加入学习讨论 QQ群(493834732)
- 云原生2.0网关API标准发展趋势
摘要:Gateway API希望取代Ingress API. 本文分享自华为云社区<云原生2.0网关API标准发展趋势>,作者:华为云云原生团队 . 云原生网关API标准背景及发展现状 G ...
- pytorch在有限的资源下部署大语言模型(以ChatGLM-6B为例)
pytorch在有限的资源下部署大语言模型(以ChatGLM-6B为例) Part1知识准备 在PyTorch中加载预训练的模型时,通常的工作流程是这样的: my_model = ModelClass ...
- .net使用nacos配置,手把手教你分布式配置中心
.net使用nacos配置,手把手教你分布式配置中心 Nacos是一个更易于构建云原生应用的动态服务发现.配置管理和服务管理平台. 这么优秀的分布式服务管理平台,怎么能不接入呢? nacos的安装和使 ...
- Django笔记三十二之session登录验证操作
本文首发于公众号:Hunter后端 原文链接:Django笔记三十二之session登录验证操作 这一篇笔记将介绍 session 相关的内容,包括如何在系统中使用 session,以及利用 sess ...
- [C++基础入门] 8、结构体
文章目录 8 结构体 8.1 结构体基本概念 8.2 结构体定义和使用 8.3 结构体数组 8.4 结构体指针 8.5 结构体嵌套结构体 8.6 结构体做函数参数 8.7 结构体中 const使用场景 ...
- [OpenCV-Python] 4 图像读取
文章目录 OpenCV-Python: II OpenCV 中的 Gui 特性 4 图片 4.1 读入图像 4.2 显示图像 4.3 保存图像 4.4 总结一下 OpenCV-Python: II O ...
- 【11个适合毕设的Python可视化大屏】用pyecharts开发拖拽式可视化数据大屏
你好,我是@马哥python说,一枚10年程序猿. 一.效果演示 以下是我近期用Python开发的原创可视化数据分析大屏,非常适合毕设用,下面逐一展示:(以下是截图,实际上有动态交互效果哦) 以下大屏 ...