在大多数编程语言中,字典比哈希表更受欢迎。这背后的原因是什么?
当前回答
集合和泛型对于处理一组对象非常有用。在.NET中,所有集合对象都位于接口IEnumerable下,该接口又具有ArrayList(索引值)和HashTable(键值)。在.NET framework 2.0之后,ArrayList和HashTable被List和Dictionary取代。现在,Arraylist和HashTable在现在的项目中不再使用。
谈到HashTable和Dictionary之间的区别,Dictionary是泛型的,而Hastable不是泛型的。我们可以向HashTable中添加任何类型的对象,但在检索时需要将其转换为所需的类型。因此,它不是类型安全的。但对于字典,在声明自身时,我们可以指定键和值的类型,因此在检索时不需要强制转换。
我们来看一个示例:
散列表
class HashTableProgram
{
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
ht.Add(1, "One");
ht.Add(2, "Two");
ht.Add(3, "Three");
foreach (DictionaryEntry de in ht)
{
int Key = (int)de.Key; //Casting
string value = de.Value.ToString(); //Casting
Console.WriteLine(Key + " " + value);
}
}
}
词典
class DictionaryProgram
{
static void Main(string[] args)
{
Dictionary<int, string> dt = new Dictionary<int, string>();
dt.Add(1, "One");
dt.Add(2, "Two");
dt.Add(3, "Three");
foreach (KeyValuePair<int, String> kv in dt)
{
Console.WriteLine(kv.Key + " " + kv.Value);
}
}
}
其他回答
根据我使用.NET Reflector所看到的:
[Serializable, ComVisible(true)]
public abstract class DictionaryBase : IDictionary, ICollection, IEnumerable
{
// Fields
private Hashtable hashtable;
// Methods
protected DictionaryBase();
public void Clear();
.
.
.
}
Take note of these lines
// Fields
private Hashtable hashtable;
因此,我们可以确定DictionaryBase在内部使用HashTable。
因为Dictionary是一个泛型类(Dictionary<TKey,TValue>),所以访问其内容是类型安全的(即,不需要像Hashtable那样从Object转换)。
比较
var customers = new Dictionary<string, Customer>();
...
Customer customer = customers["Ali G"];
to
var customers = new Hashtable();
...
Customer customer = customers["Ali G"] as Customer;
然而,Dictionary在内部实现为哈希表,因此技术上它的工作方式相同。
集合和泛型对于处理一组对象非常有用。在.NET中,所有集合对象都位于接口IEnumerable下,该接口又具有ArrayList(索引值)和HashTable(键值)。在.NET framework 2.0之后,ArrayList和HashTable被List和Dictionary取代。现在,Arraylist和HashTable在现在的项目中不再使用。
谈到HashTable和Dictionary之间的区别,Dictionary是泛型的,而Hastable不是泛型的。我们可以向HashTable中添加任何类型的对象,但在检索时需要将其转换为所需的类型。因此,它不是类型安全的。但对于字典,在声明自身时,我们可以指定键和值的类型,因此在检索时不需要强制转换。
我们来看一个示例:
散列表
class HashTableProgram
{
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
ht.Add(1, "One");
ht.Add(2, "Two");
ht.Add(3, "Three");
foreach (DictionaryEntry de in ht)
{
int Key = (int)de.Key; //Casting
string value = de.Value.ToString(); //Casting
Console.WriteLine(Key + " " + value);
}
}
}
词典
class DictionaryProgram
{
static void Main(string[] args)
{
Dictionary<int, string> dt = new Dictionary<int, string>();
dt.Add(1, "One");
dt.Add(2, "Two");
dt.Add(3, "Three");
foreach (KeyValuePair<int, String> kv in dt)
{
Console.WriteLine(kv.Key + " " + kv.Value);
}
}
}
在大多数编程语言中,字典优先于哈希表
我认为这不一定是真的,大多数语言都有这两种语言,这取决于他们喜欢的术语。
然而,在C#中,很明显的原因(对我来说)是C#HashTables和System.Collections命名空间的其他成员在很大程度上已经过时了。它们出现在c#V1.1中。它们已从C#2.0替换为System.Collections.Generic命名空间中的Generic类。
在.NET中,Dictionary<,>和HashTable之间的区别主要在于前者是一种泛型类型,因此在静态类型检查方面可以获得泛型的所有好处(以及减少装箱,但这并不像人们在性能方面所想的那么大-尽管装箱会有一定的内存成本)。
推荐文章
- net HttpClient。如何POST字符串值?
- 我如何使一个方法的返回类型泛型?
- 何时处理CancellationTokenSource?
- 如何在Java中打印二叉树图?
- 如何获取正在执行的程序集版本?
- AutoMapper vs valueinjector
- 为什么控制台不。Writeline,控制台。在Visual Studio Express中编写工作?
- 什么是.NET程序集?
- 字符串不能识别为有效的日期时间“格式dd/MM/yyyy”
- 函数应该返回空对象还是空对象?
- 如何转换日期时间?将日期时间
- 如何在c#中连接列表?
- 在c#中引用类型变量的“ref”的用途是什么?
- 防止在ASP中缓存。NET MVC中使用属性的特定操作
- 转换为值类型'Int32'失败,因为物化值为空