一、设计背景
.Net Framework自带的类库提供了一些基本的容器类,包括泛型的和非泛型的。其中我们用得最多的可能就是Dictionary<>、List<>(或者Hashtable、ArrayList)了。当我们需要实现的功能比较简单的时候,它们已经够用了,但是更多情况,在使用它们的时候我们还要写一些其实与系统业务没什么关系的代码:涉及多线程编程时需要线程安全地访问它们;有时我们希望容器内的元素按照某种顺序排列;还有些情况下需要把Dictionary<>和List<>结合起来使用,并保证它们的一致性。
二、SafeHashList介绍
SafeHashList<TKey,TValue>内置了Dictionary<TKey,TValue>和List<TKey>,Dictionary<TKey,TValue>用于存放键值对,List<Key>用于保存键的顺序,内部维护了两个容器元素的一致;
支持多线程安全访问;
支持按3种顺序排序:元素添加顺序(默认)、键的顺序(TKey类型须实现IComparable接口)、值的顺序(TValue类型须实现IComparable接口),自动排序并随时可在不同排序方式间切换;
支持Exists、Find、FindAll、FindLast、TrueForAll、ForEach等常用谓词匹配方法。
三、源码
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
SafeHashList
1![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*
2
******************************************************************
3
* 域名空间 :Collections
4
* 类名称 :SafeHashList
5
* 类功能 :线程安全的、保持特定顺序(默认的是添加顺序)的哈希表
6
* 类流程 :
7
* 运用算法 :
8
* 编写人 :Eashion
9
* 编写日期 :2009-7-14
10
* 最近更新日期:2009-11-5
11
* 版 本 :V1.0
12
* 描 述 :2009-9-10 :增加了排序功能,可按3种顺序排序;增加了Exists、Find、FindAll等方法
13
* :2009-11-5 :增加了FindLast、TrueForAll、ForEach方法
14
* 说 明 :使用请注明出处:http://www.cnblogs.com/Eashion
15
* 发现Bug或建议请反馈:Email:eashion08@gmail.com QQ: 27788280
16
* 谢谢!
17
******************************************************************
18
*/
19
using System;
20
using System.Collections.Generic;
21
using System.Collections.ObjectModel;
22![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
23
namespace Collections
24![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
26
/// 线程安全的、保持添加顺序的哈希表
27
/// </summary>
28
/// <typeparam name="TKey">键类型</typeparam>
29
/// <typeparam name="TValue">值类型</typeparam>
30
public class SafeHashList<TKey, TValue>
31![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
32![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
33
/// 枚举排序的依据类型
34
/// </summary>
35
public enum SortType
36![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
37![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
38
/// 按添加顺序排序,默认按照此顺序
39
/// </summary>
40
AddOrder = 1,
41![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
43
/// 按键排序,将根据键的IComparable排序
44
/// </summary>
45
Key,
46![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
48
/// 按值排序,将根据值的IComparable排序
49
/// </summary>
50
Value
51
}
52![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
private SortType sortBy;
54
private List<TKey> list;
55
private List<TKey> addingList;
56
private Dictionary<TKey, TValue> dictionary;
57![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
public SafeHashList()
59![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
60
this.sortBy = SafeHashList<TKey, TValue>.SortType.AddOrder;
61
this.list = new List<TKey>();
62
this.addingList = new List<TKey>();
63
this.dictionary = new Dictionary<TKey, TValue>();
64
}
65![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
66![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
67
/// 获取或设置 排序的依据
68
/// </summary>
69
public SortType SortBy
70![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
71![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return this.sortBy; }
72
set
73![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
74
lock (this.list)
75![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
76
lock (this.addingList)
77![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
78
lock (this.dictionary)
79![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
80
this.Sort(value);
81
}
82
}
83
}
84
}
85
}
86![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
87![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
88
/// 按指定依据排序
89
/// </summary>
90
/// <param name="sortBy">指定排序依据</param>
91
private void Sort(SortType sortBy)
92![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
93
switch (sortBy)
94![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
95
case SafeHashList<TKey, TValue>.SortType.AddOrder:
96![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
97
if (this.sortBy != sortBy)
98![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
99
this.list.Clear();
100
this.list.AddRange(this.addingList);
101
}
102
}
103
break;
104
case SafeHashList<TKey, TValue>.SortType.Key:
105![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
106
this.list.Sort();
107
}
108
break;
109
case SafeHashList<TKey, TValue>.SortType.Value:
110![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
111
this.list.Clear();
112
List<TValue> tmpList = new List<TValue>();
113
Dictionary<TValue, List<TKey>> tmpDictionary = new Dictionary<TValue, List<TKey>>();
114
List<TKey> keyList = null;
115![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
116
foreach (TKey k in this.addingList)
117![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
118
TValue v = this.dictionary[k];
119
if (!tmpDictionary.ContainsKey(v))
120![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
121
keyList = new List<TKey>();
122
tmpDictionary.Add(v, keyList);
123
tmpList.Add(this.dictionary[k]);
124
}
125
keyList = tmpDictionary[v];
126
keyList.Add(k);
127
}
128![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
129
tmpList.Sort();
130
foreach (TValue v in tmpList)
131![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
132
keyList = tmpDictionary[v];
133
this.list.AddRange(keyList);
134
}
135
}
136
break;
137
default:
138
break;
139
}
140![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
141
this.sortBy = sortBy;
142
}
143![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
144![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
145
/// 添加一个元素
146
/// </summary>
147
/// <param name="k">键类型</param>
148
/// <param name="v">值类型</param>
149
public void Add(TKey k, TValue v)
150![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
151
if (this.dictionary.ContainsKey(k))
152
throw new Exception("添加的元素主键值已经存在.");
153![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
154
lock (this.list)
155![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
156
lock (this.addingList)
157![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
158
lock (this.dictionary)
159![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
160
this.dictionary.Add(k, v);
161
this.addingList.Add(k);
162
this.list.Add(k);
163
this.Sort(this.sortBy);
164
}
165
}
166
}
167
}
168![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
169![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
170
/// 插入一个元素
171
/// </summary>
172
/// <param name="index">索引</param>
173
/// <param name="k">键类型</param>
174
/// <param name="v">值类型</param>
175
public void Insert(int index, TKey k, TValue v)
176![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
177
if (this.dictionary.ContainsKey(k))
178
throw new Exception("添加的元素主键值已经存在.");
179![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
180
lock (this.list)
181![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
182
lock (this.addingList)
183![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
184
lock (this.dictionary)
185![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
186
this.dictionary.Add(k, v);
187
this.addingList.Insert(index, k);
188
this.list.Insert(index, k);
189
this.Sort(this.sortBy);
190
}
191
}
192
}
193
}
194![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
195![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
196
/// 是否包含某指定的键
197
/// </summary>
198
/// <param name="key"></param>
199
/// <returns></returns>
200
public bool ContainsKey(TKey key)
201![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
202
return this.dictionary.ContainsKey(key);
203
}
204![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
205![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
206
/// 是否包含某指定的值
207
/// </summary>
208
/// <param name="value"></param>
209
/// <returns></returns>
210
public bool ContainsValue(TValue value)
211![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
212
return this.dictionary.ContainsValue(value);
213
}
214![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
215![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
216
/// 按指定键删除
217
/// </summary>
218
/// <param name="key">指定键</param>
219
public void Remove(TKey key)
220![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
221
if (this.dictionary.ContainsKey(key))
222![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
223
lock (this.list)
224![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
225
lock (this.addingList)
226![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
227
lock (this.dictionary)
228![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
229
this.dictionary.Remove(key);
230
this.addingList.Remove(key);
231
this.list.Remove(key);
232
}
233
}
234
}
235
}
236
}
237![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
238![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
239
/// 清除所有元素
240
/// </summary>
241
public void Clear()
242![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
243
lock (this.list)
244![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
245
lock (this.addingList)
246![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
247
lock (this.dictionary)
248![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
249
this.dictionary.Clear();
250
this.addingList.Clear();
251
this.list.Clear();
252
}
253
}
254
}
255
}
256![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
257![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
258
/// 搜索与指定谓词所定义的条件相匹配的元素,并返回整个 System.Collections.Generic.List<TKey> 中的第一个匹配元素。
259
/// </summary>
260
/// <param name="match">System.Predicate<TKey> 委托,用于定义要搜索的元素的条件。</param>
261
/// <returns>如果找到与指定谓词定义的条件匹配的第一个元素,则为该元素;否则为类型 TKey 的默认值。</returns>
262
public TKey Find(Predicate<TKey> match)
263![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
264
TKey result = default(TKey);
265![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
266
lock (this.list)
267![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
268
lock (this.addingList)
269![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
270
lock (this.dictionary)
271![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
272
result = this.list.Find(match);
273
}
274
}
275
}
276![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
277
return result;
278
}
279![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
280![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
281
/// 检索与指定谓词定义的条件匹配的所有元素。
282
/// </summary>
283
/// <param name="match">System.Predicate<TKey> 委托,用于定义要搜索的元素应满足的条件。</param>
284
/// <returns>如果找到,则为一个 System.Collections.Generic.List<TKey>,其中包含与指定谓词所定义的条件相匹配的所有元素;否则为一个空System.Collections.Generic.List<TKey>。</returns>
285
public List<TKey> FindAll(Predicate<TKey> match)
286![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
287
List<TKey> resultList = new List<TKey>();
288![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
289
lock (this.list)
290![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
291
lock (this.addingList)
292![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
293
lock (this.dictionary)
294![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
295
resultList = this.list.FindAll(match);
296
}
297
}
298
}
299![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
300
return resultList;
301
}
302![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
303![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
304
/// 搜索与指定谓词所定义的条件相匹配的元素,并返回整个 System.Collections.Generic.List<TKey> 中的最后一个匹配元素。
305
/// </summary>
306
/// <param name="match">System.Predicate<TKey> 委托,用于定义要搜索的元素的条件。</param>
307
/// <returns>如果找到与指定谓词定义的条件匹配的最后一个元素,则为该元素;否则为类型 TKey 的默认值。</returns>
308
public TKey FindLast(Predicate<TKey> match)
309![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
310
TKey result = default(TKey);
311![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
312
lock (this.list)
313![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
314
lock (this.addingList)
315![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
316
lock (this.dictionary)
317![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
318
result = this.list.FindLast(match);
319
}
320
}
321
}
322![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
323
return result;
324
}
325![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
326![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
327
/// 搜索与指定谓词所定义的条件相匹配的元素,并返回整个 System.Collections.Generic.List<TValue> 中的第一个匹配元素。
328
/// </summary>
329
/// <param name="match">System.Predicate<TValue> 委托,用于定义要搜索的元素的条件。</param>
330
/// <returns>如果找到与指定谓词定义的条件匹配的第一个元素,则为该元素;否则为类型 TValue 的默认值。</returns>
331
public TValue Find(Predicate<TValue> match)
332![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
333
TValue result = default(TValue);
334![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
335
List<TValue> values = new List<TValue>();
336
lock (this.list)
337![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
338
lock (this.addingList)
339![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
340
lock (this.dictionary)
341![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
342
foreach (TKey key in this.list)
343
values.Add(this.dictionary[key]);
344![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
345
result = values.Find(match);
346
}
347
}
348
}
349![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
350
return result;
351
}
352![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
353![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
354
/// 检索与指定谓词定义的条件匹配的所有元素。
355
/// </summary>
356
/// <param name="match">System.Predicate<TValue> 委托,用于定义要搜索的元素应满足的条件。</param>
357
/// <returns>如果找到,则为一个 System.Collections.Generic.List<TValue>,其中包含与指定谓词所定义的条件相匹配的所有元素;否则为一个空System.Collections.Generic.List<TValue>。</returns>
358
public List<TValue> FindAll(Predicate<TValue> match)
359![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
360
List<TValue> resultList = new List<TValue>();
361![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
362
List<TValue> values = new List<TValue>();
363
lock (this.list)
364![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
365
lock (this.addingList)
366![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
367
lock (this.dictionary)
368![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
369
foreach (TKey key in this.list)
370
values.Add(this.dictionary[key]);
371![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
372
resultList = values.FindAll(match);
373
}
374
}
375
}
376![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
377
return resultList;
378
}
379![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
380![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
381
/// 搜索与指定谓词所定义的条件相匹配的元素,并返回整个 System.Collections.Generic.List<TValue> 中的最后一个匹配元素。
382
/// </summary>
383
/// <param name="match">System.Predicate<TValue> 委托,用于定义要搜索的元素的条件。</param>
384
/// <returns>如果找到与指定谓词定义的条件匹配的最后一个元素,则为该元素;否则为类型 TValue 的默认值。</returns>
385
public TValue FindLast(Predicate<TValue> match)
386![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
387
TValue result = default(TValue);
388![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
389
List<TValue> values = new List<TValue>();
390
lock (this.list)
391![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
392
lock (this.addingList)
393![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
394
lock (this.dictionary)
395![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
396
foreach (TKey key in this.list)
397
values.Add(this.dictionary[key]);
398![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
399
result = values.FindLast(match);
400
}
401
}
402
}
403![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
404
return result;
405
}
406![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
407![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
408
/// 确定 System.Collections.Generic.List<TKey> 是否包含与指定谓词所定义的条件相匹配的元素。
409
/// </summary>
410
/// <param name="match">System.Predicate<TKey> 委托,用于定义要搜索的元素应满足的条件。</param>
411
/// <returns>如果 System.Collections.Generic.List<TKey> 包含一个或多个与指定谓词所定义的条件相匹配的元素,则为 true;否则为false。</returns>
412
public bool Exists(Predicate<TKey> match)
413![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
414
bool result = false;
415![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
416
lock (this.list)
417![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
418
lock (this.addingList)
419![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
420
lock (this.dictionary)
421![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
422
result = this.list.Exists(match);
423
}
424
}
425
}
426![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
427
return result;
428
}
429![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
430![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
431
/// 确定是否 System.Collections.Generic.List<TKey> 中的每一个元素都与指定谓词所定义的条件相匹配。
432
/// </summary>
433
/// <param name="match">System.Predicate<TKey> 委托,用于定义要搜索的元素应满足的条件。</param>
434
/// <returns>如果 System.Collections.Generic.List<TKey> 中的每一个元素都与指定谓词所定义的条件相匹配,则为 true;否则为false。</returns>
435
public bool TrueForAll(Predicate<TKey> match)
436![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
437
bool result = false;
438![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
439
lock (this.list)
440![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
441
lock (this.addingList)
442![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
443
lock (this.dictionary)
444![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
445
result = this.list.TrueForAll(match);
446
}
447
}
448
}
449![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
450
return result;
451
}
452![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
453![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
454
/// 对 System.Collections.Generic.List<TKey> 中的每一个元素执行指定操作
455
/// </summary>
456
/// <param name="action">要对 System.Collections.Generic.List<TKey> 的每个元素执行的 System.Action<TKey> 委托。</param>
457
public void ForEach(Action<TKey> action)
458![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
459
lock (this.list)
460![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
461
lock (this.addingList)
462![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
463
lock (this.dictionary)
464![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
465
this.list.ForEach(action);
466
}
467
}
468
}
469
}
470![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
471![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
472
/// 确定 System.Collections.Generic.List<TValue> 是否包含与指定谓词所定义的条件相匹配的元素。
473
/// </summary>
474
/// <param name="match">System.Predicate<TValue> 委托,用于定义要搜索的元素应满足的条件。</param>
475
/// <returns>如果 System.Collections.Generic.List<TValue> 包含一个或多个与指定谓词所定义的条件相匹配的元素,则为 true;否则为false。</returns>
476
public bool Exists(Predicate<TValue> match)
477![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
478
bool result = false;
479![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
480
List<TValue> values = new List<TValue>();
481
lock (this.list)
482![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
483
lock (this.addingList)
484![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
485
lock (this.dictionary)
486![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
487
foreach (TKey key in this.list)
488
values.Add(this.dictionary[key]);
489![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
490
result = values.Exists(match);
491
}
492
}
493
}
494![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
495
return result;
496
}
497![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
498![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
499
/// 确定是否 System.Collections.Generic.List<TValue> 中的每一个元素都与指定谓词所定义的条件相匹配。
500
/// </summary>
501
/// <param name="match">System.Predicate<TValue> 委托,用于定义要搜索的元素应满足的条件。</param>
502
/// <returns>如果 System.Collections.Generic.List<TValue> 中的每一个元素都与指定谓词所定义的条件相匹配,则为 true;否则为false。</returns>
503
public bool TrueForAll(Predicate<TValue> match)
504![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
505
bool result = false;
506![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
507
List<TValue> values = new List<TValue>();
508
lock (this.list)
509![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
510
lock (this.addingList)
511![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
512
lock (this.dictionary)
513![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
514
foreach (TKey key in this.list)
515
values.Add(this.dictionary[key]);
516![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
517
result = values.TrueForAll(match);
518
}
519
}
520
}
521![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
522
return result;
523
}
524![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
525![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
526
/// 对 System.Collections.Generic.List<TValue> 中的每一个元素执行指定操作
527
/// </summary>
528
/// <param name="action">要对 System.Collections.Generic.List<TValue> 的每个元素执行的 System.Action<TValue> 委托。</param>
529
public void ForEach(Action<TValue> action)
530![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
531
List<TValue> values = new List<TValue>();
532
lock (this.list)
533![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
534
lock (this.addingList)
535![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
536
lock (this.dictionary)
537![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
538
foreach (TKey key in this.list)
539
values.Add(this.dictionary[key]);
540![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
541
values.ForEach(action);
542
}
543
}
544
}
545
}
546![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
547![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
548
/// 获取当前元素个数
549
/// </summary>
550
public int Count
551![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
552![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return this.list.Count; }
553
}
554![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
555![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
556
/// 获取只读键列表
557
/// </summary>
558
public ReadOnlyCollection<TKey> Keys
559![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
560
get
561![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
562
ReadOnlyCollection<TKey> collection = null;
563
lock (this.list)
564![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
565
lock (this.addingList)
566![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
567
lock (this.dictionary)
568![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
569
collection = this.list.AsReadOnly();
570
}
571
}
572
}
573
return collection;
574
}
575
}
576![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
577![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
578
/// 获取值列表
579
/// </summary>
580
public List<TValue> Values
581![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
582
get
583![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
584
List<TValue> values = new List<TValue>();
585
lock (this.list)
586![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
587
lock (this.addingList)
588![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
589
lock (this.dictionary)
590![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
591
foreach (TKey key in this.list)
592
values.Add(this.dictionary[key]);
593
}
594
}
595
}
596
return values;
597
}
598
}
599![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
600![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
601
/// 获取或设置 索引器
602
/// </summary>
603
/// <param name="key">键</param>
604
/// <returns>值</returns>
605
public TValue this[TKey key]
606![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
607
get
608![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
609
if (!this.dictionary.ContainsKey(key))
610
throw new Exception("指定的主键值不存在。");
611![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
612
TValue value = default(TValue);
613
lock (this.list)
614![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
615
lock (this.addingList)
616![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
617
lock (this.dictionary)
618![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
619
value = this.dictionary[key];
620
}
621
}
622
}
623![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
624
return value;
625
}
626
set
627![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
628
if (!this.dictionary.ContainsKey(key))
629
throw new Exception("指定的主键值不存在。");
630![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
631
lock (this.list)
632![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
633
lock (this.addingList)
634![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
635
lock (this.dictionary)
636![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
637
this.dictionary[key] = value;
638
}
639
}
640
}
641
}
642
}
643![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
644![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
645
/// 获取或设置 索引器
646
/// </summary>
647
/// <param name="index">键索引</param>
648
/// <returns>值</returns>
649
public TValue this[int index]
650![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
651
get
652![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
653
if (this.list.Count <= index)
654
throw new Exception("索引值超出了范围。");
655![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
656
TValue value = default(TValue);
657
lock (this.list)
658![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
659
lock (this.addingList)
660![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
661
lock (this.dictionary)
662![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
663
value = this.dictionary[this.list[index]];
664
}
665
}
666
}
667![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
668
return value;
669
}
670
set
671![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
672
if (this.list.Count <= index)
673
throw new Exception("索引值超出了范围。");
674![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
675
lock (this.list)
676![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
677
lock (this.addingList)
678![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
679
lock (this.dictionary)
680![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
681
this.dictionary[this.list[index]] = value;
682
}
683
}
684
}
685
}
686
}
687
}
688
}
689![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)