[ASK]-关于IDictionary,hastable,arraylist [英] [ASK] - About IDictionary, hastable, arraylist

查看:52
本文介绍了[ASK]-关于IDictionary,hastable,arraylist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,如上所述..我想问你,哪一种最适合将数据临时存储在内存中? :〜请给我您的解释,以便我能为我的项目找到最好的一个. :)

Hi all, as the topic above.. i wanna ask you, which one is the best for store data temporary in memory ?? :~ Give me your explanation, so i can find the best one for my project.. Thank you. :)

推荐答案

实际上,IDictionary 是一个接口,由Hashtable实现.因此,我想您实际上需要在Hashtable ArrayList之间进行比较.
现在,这两个在本质上是不同的.

ArrayList 可以容纳任何种类的对象,并且不需要密钥.也就是说,

Actually, IDictionary is an interface, which is implemented by Hashtable. So, I guess you actually need a comparison between a Hashtable and a ArrayList.

Now, these two are different in nature.

An ArrayList can hold objects of any kind, and, requires no key. That is,

int a =10;
Person p = new Person();

ArrayList list = new ArrayList();
list.Add(a);
list.Add(p);



HashTable 仅保存DictionaryEntry 的对象. DictionaryEntry obejct有两个字段,一个Key Value.



Whereas, a HashTable holds objects of DictionaryEntry only. A DictionaryEntry obejct has two fields, a Key and a Value.

That is,
Hashtable ht = new Hashtable();         
ht.Add("key","value");



稍后,您可以使用以下键从Hashtable 中检索值:



Later, you can retrieve the value from the Hashtable using the key as follows:

string value = (string)ht["key"];



在内部,将数据存储在Hashtable内部后,每个条目都将成为DictionaryEntry

因此,如果需要使用键/值对存储任何数据,则需要使用Hashtbale;并且,如果仅需要存储数据而不使用任何键,则可以使用ArrayList.这取决于您的要求,并且这两者之间没有最佳实践的问题.



Internally, after you store data inside the Hashtable, each entry becomes a DictionaryEntry

So, if you need to store any data using Key/Value pair, you need to use Hashtbale, And, if you need to store only data without using any key, you can use ArrayList. It depends on your requirement, and, there is no question of best practices between these two.


要回答您的问题,您需要首先考虑要临时存储哪种数据存储在内存中,这非常重要,因为hastable和arraylist在如何存储数据以及可以存储哪种数据方面彼此非常不同.
例如:

1)在Arraylist中,我们可以添加任何数据类型值,
中的每个项目 arraylist被视为对象.

2)Hashtable是键,值对的集合
i)密钥可以是任何数据类型
ii)密钥不能为空引用
iii)但是值可以为null引用
并且不要忘记Hashtable实现了IDictionary

3)IDictionary是一个接口.您可以实施它来创建自己的
提供键对结构的集合.
哈希表就是一个例子.

在性能上明智通过哈希表中的键检索数据要比在Arraylist中检索数据更快.

希望这对您有帮助
To have an answer your question you need to think first about what kind of data you want to temporary store in memory, that is very important as hastable and arraylist are very diffrent from each other in how they store data and what kind of data they can store.
for example:

1)In an Arraylist We Can Add any datatype value,Every item in an
arraylist is treated as an object.

2)Hashtable is collection of key,value pairs
i)Key Can be any datatype
ii)Key Cannot be null refrrence
iii)but value can be null referrence
And don''t forget that Hashtable implements IDictionary

3) IDictionary is an interface. you can implement it to create your own
collection which provides Key-pair structures.
Hashtable is an example.

performance wise Retrieving the data by key in a Hashtable is faster than retrieving it in an Arraylist.

I hope this helps


每个收集类都是不同的,无法说出哪一个是最好的……几乎所有的收集类都将数据临时存储在内存中.

您的选择取决于您的需求.您需要动态构建集合吗?您需要节省时间或内存空间吗?您需要分类的还是未分类的集合?集合需要为只读吗?

ps.无论您选择什么,都不要使用ArrayList ...
Each collection class is different and it''s not possible to say which one is the best... And almost all of them store data temporarily in memmory.

Your choice depends on your needs. Do you need to build the collection dynamically? Do you need to save time or memory space? Do you need a sorted or unsorted collection? Does the collection need to be be read-only?

ps. No matter what you choose, do not use ArrayList...


这篇关于[ASK]-关于IDictionary,hastable,arraylist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆