CollectionsUtil 类
定义
创建忽略字符串大小写的集合。Creates collections that ignore the case in strings.
public ref class CollectionsUtil
public class CollectionsUtil
type CollectionsUtil = class
Public Class CollectionsUtil
- 继承
-
CollectionsUtil
示例
下面的示例使用两个集合(一个哈希表和一个已排序的列表)来保存一组城市的人口值。The following example uses two collections, a hash table and a sorted list, to hold population values for a group of cities. 使用 city 名称作为键从集合中检索值。The values are retrieved from the collections by using the city names as keys. 城市名称采用混合大小写,以将其用作不区分大小写的键。The city names are in mixed case to show their use as case-insensitive keys.
#using <System.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
ref class TestCollectionsUtils
{
public:
static void Main()
{
Hashtable^ population1 = CollectionsUtil::CreateCaseInsensitiveHashtable();
population1["Trapperville"] = 15;
population1["Doggerton"] = 230;
population1["New Hollow"] = 1234;
population1["McHenry"] = 185;
// Select cities from the table using mixed case.
Console::WriteLine("Case insensitive hashtable results:\n");
Console::WriteLine("{0}'s population is: {1}", "Trapperville", population1["trapperville"]);
Console::WriteLine("{0}'s population is: {1}", "Doggerton", population1["DOGGERTON"]);
Console::WriteLine("{0}'s population is: {1}", "New Hollow", population1["New hoLLow"]);
Console::WriteLine("{0}'s population is: {1}", "McHenry", population1["MchenrY"]);
SortedList^ population2 = CollectionsUtil::CreateCaseInsensitiveSortedList();
for each (String^ city in population1->Keys)
{
population2->Add(city, population1[city]);
}
// Select cities from the sorted list using mixed case.
Console::WriteLine("\nCase insensitive sorted list results:\n");
Console::WriteLine("{0}'s population is: {1}", "Trapperville", population2["trapPeRVille"]);
Console::WriteLine("{0}'s population is: {1}", "Doggerton", population2["dOGGeRtON"]);
Console::WriteLine("{0}'s population is: {1}", "New Hollow", population2["nEW hOLLOW"]);
Console::WriteLine("{0}'s population is: {1}", "McHenry", population2["MchEnrY"]);
}
};
int main()
{
TestCollectionsUtils::Main();
}
// This program displays the following output to the console
//
// Case insensitive hashtable results:
//
// Trapperville's population is: 15
// Doggerton's population is: 230
// New Hollow's population is: 1234
// McHenry's population is: 185
//
// Case insensitive sorted list results:
//
// Trapperville's population is: 15
// Doggerton's population is: 230
// New Hollow's population is: 1234
// McHenry's population is: 185
using System;
using System.Collections;
using System.Collections.Specialized;
class TestCollectionsUtils
{
public static void Main()
{
Hashtable population1 = CollectionsUtil.CreateCaseInsensitiveHashtable();
population1["Trapperville"] = 15;
population1["Doggerton"] = 230;
population1["New Hollow"] = 1234;
population1["McHenry"] = 185;
// Select cities from the table using mixed case.
Console.WriteLine("Case insensitive hashtable results:\n");
Console.WriteLine("{0}'s population is: {1}", "Trapperville", population1["trapperville"]);
Console.WriteLine("{0}'s population is: {1}", "Doggerton", population1["DOGGERTON"]);
Console.WriteLine("{0}'s population is: {1}", "New Hollow", population1["New hoLLow"]);
Console.WriteLine("{0}'s population is: {1}", "McHenry", population1["MchenrY"]);
SortedList population2 = CollectionsUtil.CreateCaseInsensitiveSortedList();
foreach (string city in population1.Keys)
{
population2.Add(city, population1[city]);
}
// Select cities from the sorted list using mixed case.
Console.WriteLine("\nCase insensitive sorted list results:\n");
Console.WriteLine("{0}'s population is: {1}", "Trapperville", population2["trapPeRVille"]);
Console.WriteLine("{0}'s population is: {1}", "Doggerton", population2["dOGGeRtON"]);
Console.WriteLine("{0}'s population is: {1}", "New Hollow", population2["nEW hOLLOW"]);
Console.WriteLine("{0}'s population is: {1}", "McHenry", population2["MchEnrY"]);
}
}
// This program displays the following output to the console
//
// Case insensitive hashtable results:
//
// Trapperville's population is: 15
// Doggerton's population is: 230
// New Hollow's population is: 1234
// McHenry's population is: 185
//
// Case insensitive sorted list results:
//
// Trapperville's population is: 15
// Doggerton's population is: 230
// New Hollow's population is: 1234
// McHenry's population is: 185
Imports System.Collections
Imports System.Collections.Specialized
Class TestCollectionsUtils
Public Shared Sub Main()
Dim population1 As Hashtable = CollectionsUtil.CreateCaseInsensitiveHashtable()
population1("Trapperville") = 15
population1("Doggerton") = 230
population1("New Hollow") = 1234
population1("McHenry") = 185
' Select cities from the table using mixed case.
Console.WriteLine("Case insensitive hashtable results:" + Environment.NewLine)
Console.WriteLine("{0}'s population is: {1}", "Trapperville", population1("trapperville"))
Console.WriteLine("{0}'s population is: {1}", "Doggerton", population1("DOGGERTON"))
Console.WriteLine("{0}'s population is: {1}", "New Hollow", population1("New hoLLow"))
Console.WriteLine("{0}'s population is: {1}", "McHenry", population1("MchenrY"))
Dim population2 As SortedList = CollectionsUtil.CreateCaseInsensitiveSortedList()
For Each city As String In population1.Keys
population2.Add(city, population1(city))
Next city
' Select cities from the sorted list using mixed case.
Console.WriteLine(Environment.NewLine + "Case insensitive sorted list results:" + Environment.NewLine)
Console.WriteLine("{0}'s population is: {1}", "Trapperville", population2("trapPeRVille"))
Console.WriteLine("{0}'s population is: {1}", "Doggerton", population2("dOGGeRtON"))
Console.WriteLine("{0}'s population is: {1}", "New Hollow", population2("nEW hOLLOW"))
Console.WriteLine("{0}'s population is: {1}", "McHenry", population2("MchEnrY"))
End Sub
End Class
' This program displays the following output to the console
'
' Case insensitive hashtable results:
'
' Trapperville's population is: 15
' Doggerton's population is: 230
' New Hollow's population is: 1234
' McHenry's population is: 185
'
' Case insensitive sorted list results:
'
' Trapperville's population is: 15
' Doggerton's population is: 230
' New Hollow's population is: 1234
' McHenry's population is: 185
注解
这些方法使用哈希代码提供程序和比较器的不区分大小写的实现,生成集合的不区分大小写的实例。These methods generate a case-insensitive instance of the collection using case-insensitive implementations of the hash code provider and the comparer. 生成的实例可以像该类的任何其他实例一样使用,但其行为可能不同。The resulting instance can be used like any other instances of that class, although it may behave differently.
例如,假设有两个对象的关键字为 "hello","HELLO" 将添加到哈希表中。For example, suppose two objects with the keys "hello" and "HELLO" are to be added to a hash table. 区分大小写的哈希表将创建两个不同的条目:然而,不区分大小写的哈希表会在添加第二个对象时引发异常。A case-sensitive hash table would create two different entries; whereas, a case-insensitive hash table would throw an exception when adding the second object.
构造函数
| CollectionsUtil() |
初始化 CollectionsUtil 类的新实例。Initializes a new instance of the CollectionsUtil class. |
方法
| CreateCaseInsensitiveHashtable() |
创建 Hashtable 类具有默认初始容量的不区分大小写的新实例。Creates a new case-insensitive instance of the Hashtable class with the default initial capacity. |
| CreateCaseInsensitiveHashtable(IDictionary) |
将项从指定字典复制到 Hashtable 类的不区分大小写的新实例,该实例具有与复制项的数量相同的初始容量。Copies the entries from the specified dictionary to a new case-insensitive instance of the Hashtable class with the same initial capacity as the number of entries copied. |
| CreateCaseInsensitiveHashtable(Int32) |
创建 Hashtable 类具有指定初始容量的不区分大小写的新实例。Creates a new case-insensitive instance of the Hashtable class with the specified initial capacity. |
| CreateCaseInsensitiveSortedList() |
创建 SortedList 类的新实例,该实例忽略字符串的大小写。Creates a new instance of the SortedList class that ignores the case of strings. |
| Equals(Object) |
确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object. (继承自 Object) |
| GetHashCode() |
作为默认哈希函数。Serves as the default hash function. (继承自 Object) |
| GetType() |
获取当前实例的 Type。Gets the Type of the current instance. (继承自 Object) |
| MemberwiseClone() |
创建当前 Object 的浅表副本。Creates a shallow copy of the current Object. (继承自 Object) |
| ToString() |
返回表示当前对象的字符串。Returns a string that represents the current object. (继承自 Object) |
适用于
线程安全性
Hashtable可以同时支持一个编写器和多个读取器。A Hashtable can support one writer and multiple readers concurrently. 若要支持多个编写器,必须通过方法返回的包装完成所有操作 Synchronized(Hashtable) 。To support multiple writers, all operations must be done through the wrapper returned by the Synchronized(Hashtable) method.
SortedList只要不修改集合,就可以同时支持多个读取器。A SortedList can support multiple readers concurrently, as long as the collection is not modified. 若要保证的线程安全 SortedList ,必须通过方法返回的包装执行所有操作 Synchronized(SortedList) 。To guarantee the thread safety of the SortedList, all operations must be done through the wrapper returned by the Synchronized(SortedList) method.
枚举集合本质上不是线程安全的过程。Enumerating through a collection is intrinsically not a thread safe procedure. 即使某个集合已同步,其他线程仍可以修改该集合,这会导致枚举数引发异常。Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. 若要确保枚举过程中的线程安全性,可以在整个枚举期间锁定集合,或者捕获由其他线程进行的更改所导致的异常。To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.