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 ...
随机推荐
- CentOS 9 开局配置
CentOS 9 开局配置 CentOS 9 发布有几年了,一直没有尝试使用,CentOS 9 有一些变动. 查看系统基础信息 # 查看系统基础信息 [root@chenby ~]# neofetch ...
- python之多线程操作
线程模块 Python3 通过两个标准库 _thread 和 threading 提供对线程的支持. _thread 提供了低级别的.原始的线程以及一个简单的锁,它相比于 threading 模块的功 ...
- Vue启用报错 RangeError: Invalid typed array length: -4095
近期开发的前端项目项目启用失败,记录下修复过程 RangeError: Invalid typed array length: -4095 错误原因:node版本问题,安装10.x.x 即可 重新安装 ...
- MySQL MHA信息的收集【Filebeat+logstash+MySQL】
一.项目背景 随着集团MHA集群的日渐增长,MHA管理平台话越来越迫切.而MHA平台的建设第一步就是将这些成百上千套的MHA集群信息收集起来,便于查询和管理. MHA主要信息如下: (1)基础配置信息 ...
- 迁移学习(COAL)《Generalized Domain Adaptation with Covariate and Label Shift CO-ALignment》
论文信息 论文标题:Generalized Domain Adaptation with Covariate and Label Shift CO-ALignment论文作者:Shuhan Tan, ...
- React 富文本编辑 braft-editor
推荐一种react-富文本编辑器,braft-editor braft-editor的github:https://github.com/margox/braft-editor braft-edito ...
- 2022-12-18:给定一个长度为n的二维数组graph,代表一张图, graph[i] = {a,b,c,d} 表示i讨厌(a,b,c,d),讨厌关系为双向的, 一共有n个人,编号0~n-1, 讨
2022-12-18:给定一个长度为n的二维数组graph,代表一张图, graph[i] = {a,b,c,d} 表示i讨厌(a,b,c,d),讨厌关系为双向的, 一共有n个人,编号0~n-1, 讨 ...
- 2020-09-13:判断一个正整数是a的b次方,a和b是整数,并且大于等于2,如何求解?
福哥答案2020-09-13: 首先确定b的范围,b的范围一定在[2,logN]里.然后遍历b,求a的范围,如果范围长度等于0,说明这个正整数是a的b次方.1.遍历b范围.二分法求a,a初始范围是[2 ...
- 2022-07-23:给定N件物品,每个物品有重量(w[i])、有价值(v[i]), 只能最多选两件商品,重量不超过bag,返回价值最大能是多少? N <= 10^5, w[i] <= 10^5, v
2022-07-23:给定N件物品,每个物品有重量(w[i]).有价值(v[i]), 只能最多选两件商品,重量不超过bag,返回价值最大能是多少? N <= 10^5, w[i] <= 1 ...
- 2021-04-25:给定一个数组arr,和一个正数M,返回在arr的子数组在长度不超过M的情况下,求最大的累加和。
福大大 答案2021-04-25: 前缀和+左大右小的双端队列.时间太晚了,所以写得简单. 代码用golang编写.代码如下: package main import ( "containe ...