集合Hashtable Dictionary Hashset
#region Dictionary<K,V>
Dictionary<string, Person> dict = new Dictionary<string, Person>();
dict.Add("ylp", new Person()); //添加
dict.ContainsKey("ylp");//是否包含
dict.Remove("ylp");//移除
//dict["ylp"].Name
//遍历键
foreach (string key in dict.Keys)
{}
//遍历值
foreach (Person value in dict.Values)
{}
//键值对一起遍历
foreach (KeyValuePair<string, Person> kv in dict)
{
Console.WriteLine(kv.Key + " " + kv.Value);
}
#endregion
class Program
{
static void Main(string[] args)
{
#region 键值对集合使用,Hashtable //1.创建一个集合对象
Hashtable hash = new Hashtable();
//增加内容
hash.Add("ylp", "闫刘盘");
hash.Add("lsx", "刘尚鑫");
hash.Add("ljj", "李晶晶");
hash.Add("cc", "陈超");
hash.Add("bsy", "鲍守营");
hash.Add("pll", "彭莉莉"); ////键值对集合不能使用for循环遍历,因为无法根据索引获取内容,只能根据键来获取内容。
//for (int i = 0; i < hash.Count; i++)//错误的
//{
// Console.WriteLine(hash[i]);
//} ////通过foreach循环来遍历
////遍历键值对集合的所有的键
//foreach (object item in hash.Keys)
//{
// Console.WriteLine(item.ToString());
//} ////遍历所有的值
//foreach (object item in hash.Values)
//{
// Console.WriteLine(item);//遍历所有的值
//} //直接遍历
//此处必须用DictionaryEntry
foreach (DictionaryEntry item in hash)
{
//直接遍历键值对
Console.WriteLine(item.Key + " " + item.Value);
}
Console.ReadKey(); ////键值对集合的一个特点:键不能重复。
////hash.Add("ylp","有老婆"); ////判断集合中是否已经存在某个键了
//if (hash.ContainsKey("ylp"))
//{
// Console.WriteLine("已经存在ylp了。");
//}
//else
//{
// hash.Add("ylp", "燕刘盼");
//} ////根据键获取值
//Console.WriteLine(hash["ylp"]);
//Console.WriteLine(hash["lsx"]); //修改
//hash["key"]="修改"; //根据键删除某个元素
//hash.Remove("key");
Console.ReadKey(); #endregion
}
}
一下是转载内容:
今天又去面试了,结果依然很悲催,平时太过于关注表面上的东西,有些实质却不太清楚,遇到HashTable和Dictionary相关的知识,记录下来,希望对后来人有所帮助,以及对自己以后复习可以参考。
1.HashTable
哈希表(HashTable)表示键/值对的集合。在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类似key-value的键值对,其中key通常可用来快速查找,同时key是区分大小写;value用于存储对应于key的值。Hashtable中key-value键值对均为object类型,所以Hashtable可以支持任何类型的keyvalue键值对,任何非 null 对象都可以用作键或值。
在哈希表中添加一个key/键值对:HashtableObject.Add(key,);
在哈希表中去除某个key/键值对:HashtableObject.Remove(key);
从哈希表中移除所有元素: HashtableObject.Clear();
判断哈希表是否包含特定键key: HashtableObject.Contains(key);
2.HashSet
HashSet<T>类主要是设计用来做高性能集运算的,例如对两个集合求交集、并集、差集等。集合中包含一组不重复出现且无特性顺序的元素,HashSet拒绝接受重复的对象。
HashSet<T>的一些特性如下:
a. HashSet<T>中的值不能重复且没有顺序。
b. HashSet<T>的容量会按需自动添加。
3.Dictionary
Dictionary表示键和值的集合。
Dictionary<string, string>是一个泛型
他本身有集合的功能有时候可以把它看成数组
他的结构是这样的:Dictionary<[key], [value]>
他的特点是存入对象是需要与[key]值一一对应的存入该泛型
通过某一个一定的[key]去找到对应的值
4.HashTable和Dictionary的区别:
(1).HashTable不支持泛型,而Dictionary支持泛型。
(2). Hashtable 的元素属于 Object 类型,所以在存储或检索值类型时通常发生装箱和拆箱的操作,所以你可能需要进行一些类型转换的操作,而且对于int,float这些值类型还需要进行装箱等操作,非常耗时。
(3).单线程程序中推荐使用 Dictionary, 有泛型优势, 且读取速度较快, 容量利用更充分。多线程程序中推荐使用 Hashtable, 默认的 Hashtable 允许单线程写入, 多线程读取, 对 Hashtable 进一步调用 Synchronized() 方法可以获得完全线程安全的类型. 而 Dictionary 非线程安全, 必须人为使用 lock 语句进行保护, 效率大减。
(4)在通过代码测试的时候发现key是整数型Dictionary的效率比Hashtable快,如果key是字符串型,Dictionary的效率没有Hashtable快。 今天面试最大的收获,不是总结以上的东西,而是让我明白了许多东西不要追求表面的,不是为了完成什么功能,而学习某种技术,更应该深入的去理解它的本质,基础很重要。三位面试官,都非常出色,讲了许多我平常很少用到的东西,和他们在一起交流,会学到不少的东西,真希望以后能进入像这样的公司,让自己快速的成长起来。
static void IntMethod()
{
int count = ;
Dictionary<int, int> dictionary = new Dictionary<int, int>();
Hashtable hashtable = new Hashtable();
for (int i = ; i < count; i++)
{
dictionary.Add(i,i);
hashtable.Add(i,i);
} Stopwatch stopwatch = Stopwatch.StartNew();
for (int i = ; i < count; i++)
{
int value = dictionary[i];
}
stopwatch.Stop();
Console.WriteLine(stopwatch.ElapsedMilliseconds); stopwatch = Stopwatch.StartNew();
for (int i = ; i < count; i++)
{
object value = hashtable[i];
}
stopwatch.Stop(); Console.WriteLine(stopwatch.ElapsedMilliseconds); } static void MethodString()
{
int count = ;
Dictionary<string, string> dictionary = new Dictionary<string, string>();
Hashtable hashtable=new Hashtable();
for (int i = ; i < count; i++)
{
dictionary.Add(i.ToString(),"aaa");
hashtable.Add(i.ToString(),"aaa");
} Stopwatch stopwatch = Stopwatch.StartNew();
for (int i = ; i < count; i++)
{
string value=dictionary[i.ToString()];
}
stopwatch.Stop();
Console.WriteLine(stopwatch.ElapsedMilliseconds); stopwatch = Stopwatch.StartNew();
for (int i = ; i < count; i++)
{
object value = hashtable[i.ToString()];
}
stopwatch.Stop(); Console.WriteLine(stopwatch.ElapsedMilliseconds);
}
面试,不止是为了找一份满意的工作,从中可以让你学会如何沟通,如何去推销自己,更重要的是让自己了解自己的不足,自己在哪些方面还欠缺,还有待提高。
知行情,知不足,知自己。
明天继续挑战,希望一切都顺利!!!
集合Hashtable Dictionary Hashset的更多相关文章
- HashTable、HashSet和Dictionary的区别(转载)
1.HashTable哈希表(HashTable)表示键/值对的集合.在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类 ...
- C#基础课程之五集合(HashTable,Dictionary)
HashTable例子: #region HashTable #region Add Hashtable hashTable = new Hashtable(); Hashtable hashTabl ...
- [转帖]C#中字典集合HashTable、Dictionary、ConcurrentDictionary三者区别
C#中字典集合HashTable.Dictionary.ConcurrentDictionary三者区别 https://blog.csdn.net/yinghuolsx/article/detail ...
- Java 集合系列 16 HashSet
java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...
- C#泛型集合之Dictionary<k, v>使用技巧
1.要使用Dictionary集合,需要导入C#泛型命名空间 System.Collections.Generic(程序集:mscorlib) 2.描述 1).从一组键(Key)到一组值(Value) ...
- 从内部剖析C# 集合之--Dictionary
Dictionary和hashtable用法有点相似,他们都是基于键值对的数据集合,但实际上他们内部的实现原理有很大的差异, 先简要概述一下他们主要的区别,稍后在分析Dictionary内部实现的大概 ...
- 类 Array Arraylist List Hashtable Dictionary
总结C# 集合类 Array Arraylist List Hashtable Dictionary Stack Queue 我们用的比较多的非泛型集合类主要有 ArrayList类 和 HashT ...
- Hashtable Dictionary List
Hashtable Dictionary List 谁效率更高 一 前言 很少接触HashTable晚上回来简单看了看,然后做一些增加和移除的操作,就想和List 与 Dictionary比较下存 ...
- java基础33 Set集合下的HashSet集合和TreeSet集合
单例集合体系: ---------| collection 单例集合的根接口--------------| List 如果实现了list接口的集合类,具备的特点:有序,可重复 注:集合 ...
随机推荐
- 《隆重介绍 思源黑体:一款Pan-CJK 开源字体》
关于思源黑体 思源黑体是谷歌与 Adobe 联合开发,支持简体中文.繁体中文.日文.韩文以及英文:支持 ExtraLight.Light.Normal.Regular.Medium.Bold 和 He ...
- Codeforces Round #243 (Div. 2) C. Sereja and Swaps
由于n比较小,直接暴力解决 #include <iostream> #include <vector> #include <algorithm> #include ...
- ACM: 敌兵布阵 解题报告 -线段树
敌兵布阵 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description Li ...
- 【BZOJ】1406: [AHOI2007]密码箱
http://www.lydsy.com/JudgeOnline/problem.php?id=1406 题意:求$0<=x<n, 1<=n<=2,000,000,000, 且 ...
- 【JAVA基础】 MAP 遍历
public static void main(String[] args) { Map<String, String> map = new HashMap<String, Stri ...
- javascript问题积累
今天在写网页时碰到了几个js可以解决的小问题,很好用,很简便 1.鼠标移动到图片上时可更换图片,比如用到给图片加颜色,去颜色. <img src="../img/02.gif" ...
- REST
REST (REpresentational State Transfer). 为REST模式的Web服务与复杂的SOAP和XML-RPC对比来讲明显的更加简洁. "设计良好的网络应用表现为 ...
- Centos 安装了 Wkhtmltopdf 却依旧显示 无法打印pdf
Odoo里判断wkhtmlpdf是否安装的代码在 openerp/tools/misc.py 文件中: def find_in_path(name): path = os.environ.get('P ...
- html制作一个美观的搜索框
一般情况下搜索栏就是form标签中的一个input标签. 如: <form> <input type="text" placeholder=" 搜索关键 ...
- zk编程语言: 如何改变datebox框值的大小及高度
<?page title="" contentType="text/html;charset=UTF-8"?> <zk > <st ...