HashTable 应用

HashTable 是一个<key,value>容器,使用散列表存储数据。Hashtable中Key和Value均为object类型,可以在一个Hashtable中存储不同数据类型的键值对。

第一部分:查找分析

第二部分:操作

1,创建哈希表

Hashtable ht = new Hashtable();

2,添加,删除数据

ht.Add("ObjKey",  "ObjValue");

ht.Remove("ObjKey");

3,根据Key获取数据

string ObjValue= (string)ht["ObjKey"].ToString();

4,遍历哈希表

IDictionaryEnumerator en = hshTable.GetEnumerator();  //  遍历哈希表所有的键,读出相应的值

while (en.MoveNext())

{

  string ObjKey = en.Key.ToString();

  string ObjValue = en.Value.ToString();

}

foreach(DictionaryEntry en in ht)
{
      string ObjKey = en.Key.ToString();

  string ObjValue = en.Value.ToString();    
}

示例代码

       Hashtable ht = new Hashtable();

            ht.Add(1, 2);
            ht.Add("a", "b");
            ht.Add(3, "t");

            foreach(DictionaryEntry de in ht)
            {
                Console.WriteLine("ht:key={0}, value={1}",de.Key ,de.Value);
            }

            Console.WriteLine(ht[1]);

            ht.Remove(1);

            if(!ht.Contains(1))
            {
                Console.WriteLine("Key=1 delete");
            }

            IDictionaryEnumerator en = ht.GetEnumerator();
            while (en.MoveNext())
            {
                Console.WriteLine("ht:key={0}, value={1}", en.Key, en.Value);
            }
时间: 2024-08-09 22:02:02

HashTable 应用的相关文章

HashMap和Hashtable

HashMap和Hashtable的区别 HashMap和Hashtable都实现了Map接口,但决定用哪一个之前先要弄清楚它们之间的分别.主要的区别有:线程安全性,同步(synchronization),以及速度. HashMap几乎可以等价于Hashtable,除了HashMap是非synchronized的,并可以接受null(HashMap可以接受为null的键值(key)和值(value),而Hashtable则不行). HashMap是非synchronized,而Hashtable

HashMap,Hashtable,ConcurrentHashMap 和 synchronized Map 的原理和区别

HashMap 是否是线程安全的,如何在线程安全的前提下使用 HashMap,其实也就是HashMap,Hashtable,ConcurrentHashMap 和 synchronized Map 的原理和区别.当时有些紧张只是简单说了下HashMap不是线程安全的:Hashtable 线程安全,但效率低,因为是 Hashtable 是使用 synchronized 的,所有线程竞争同一把锁:而 ConcurrentHashMap 不仅线程安全而且效率高,因为它包含一个 segment 数组,将

hashtable

HashTable是一种能提供快速插入和查询的数据结构,无论其包含有多少Item,查询和插入操作的平均时间总是接近O(1).hash function 的作用就是将这些范围很大的数(domain of keys )转换成我们需要的序号(domain of location)..net framework采用Division Methed作为其散列算法,使用取模(modulo)操作将Hash code值域转换到合适的范围.即:arrayIndex = hashcode % arraySize;其中

简易的hashtable实现

参考他人思路而写,具体可参考:http://blog.csdn.net/anialy/article/details/7620469 1 #ifndef _HASHTABLE_ 2 #define _HASHTABLE_ 3 #include<iostream> 4 using namespace std; 5 6 7 8 template<typename key_type,typename value_type> 9 struct HashNode{ 10 key_type k

JDK的弃儿:Vector、Stack、Hashtable、Enumeration

随着JDK的发展,一些设计缺陷或者性能不足的类库难免会被淘汰,最常见的就是Vector.Stack.HashTable和Enumeration了. Vector(@since 1.0) 首先看看Vector的UML类图,可以看出,他是一个与ArrayList有着相同继承体系的类,大致功能也和ArrayList一样.Vector与ArrayList最大的不同点在于它是线程安全的,因为其内部几乎所有方法都用了synchronized来修饰.但是,Synchronized是重量级锁,读写操作也没有做适

HashTable集合遍历的三种方法

hashtable集合遍历可以根据key,value以及key+value 示例代码: Hashtable table = new Hashtable(); Student stu = new Student(); stu.Name = "李四"; stu.Age = 18; Student stu1 = new Student(); stu1.Name = "张三"; stu1.Age = 18; Student stu2 = new Student(); stu

HashSet, HashTable

HashTable 存储键值对 , Hashtable和Dictionary<TKey,TValue>都是存键值对 HashSet 只存储值,盛放不同的数据,相同的数据只保留一份 HashSet<T>对集合运算的操作 public void IntersectWithTest() { HashSet<int> set1 = new HashSet<int>() { 1, 2, 3 }; HashSet<int> set2 = new HashSe

HashTable vs HashMap(三)

HashTable的应用非常广泛,HashMap是新框架中用来代替HashTable的类,也就是说建议使用HashMap,不要使用HashTable. 可能你觉得HashTable很好用,为什么不用呢?这里简单分析他们的区别. 1.HashTable的方法是同步的,HashMap未经同步,所以在多线程场合要手动同步HashMap这个区别就像Vector和ArrayList一样. 2.HashTable不允许null值(key和value都不可以),HashMap允许null值(key和value

Java 集合系列14之 Map总结(HashMap, Hashtable, TreeMap, WeakHashMap等使用场景)

http://www.cnblogs.com/skywang12345/p/3311126.html 概要 学完了Map的全部内容,我们再回头开开Map的框架图. 本章内容包括:第1部分 Map概括第2部分 HashMap和Hashtable异同第3部分 HashMap和WeakHashMap异同 转载请注明出处:http://www.cnblogs.com/skywang12345/admin/EditPosts.aspx?postid=3311126 第1部分 Map概括 (01) Map

HashMap和Hashtable的区别

导读: 1 HashMap不是线程安全的 hastmap是一个接口 是map接口的子接口,是将键映射到值的对象,其中键和值都是对象,并且不能包含重复键,但可以包含重复值.HashMap允许null key和null value,而hashtable不允许. 2   HashTable是线程安全的一个Collection. HashMap是Hashtable的轻量级实现(非线程安全的实现),他们都完成了Map接口,主要区别在于HashMap允许空(null)键值(key),由于非线程安全,效率上可