zoukankan      html  css  js  c++  java
  • Hashtable哈希表和SortedList排序列表类[转贴]

    Hashtable类和SortedList类是两种被称为字典(dictionary)的集合。所谓字典,是指一种键值对的集合。

    Hashtable类被称为哈希表,Hashtable类在内部维护着一个内部哈希表。内部哈希表为插入到Hashtable的每个键进行哈希编码,在后续的检索操作中,通过哈希代码,可以遍历所有元素。这种方法为搜寻操作提供了较佳的性能。

    SortedList类是一种可以对“键值对”进行排序的字典集合。

    在.Net中,键和值可以是任何对象,在后台,当插入键值对到Hashtable中时,Hashtable使用每个键所引用对象的GetHashCode()方法,获取一个哈希编码,存入Hashtable中。

    ============================

    构建哈希表时,一些新的概念需要理解。

    1.在哈希表中,键被转换为哈希代码,而值存储在存储桶(bucket)中。

    2.初始容量指哈希表中的元素的数目,加载因子是哈希表元素与存储桶的最大比率,当初始容量需要自动扩展前,确定值与存储桶之间的最大比率。值越小,出现冲突也越小。.NET默认加载因子为1.0.

    using System;
    using System.Collections.Generic;
    using System.Collections;
    using System.Text;

    namespace ConsoleApplication19
    {
        class Program
        {
            static void Main(string[] args)
            {
                Hashtable h1 = new Hashtable();
                h1.Add("键1", "键值1");
                h1.Add("键2", "键值2");
                h1.Add("键3", "键值3");
                h1.Add("键4", "键值4");
                h1.Add("键5", "键值5");
                h1.Add("键6", "键值6");
                h1.Add("键7", "键值7");
                DisplayResult(h1);
                Console.WriteLine("通过键获取键值:" + h1["键1"]);
                Console.WriteLine("移除哈希表中的“键2”");
                h1.Remove("键2");
                Console.WriteLine("哈希表中的元素总数是:" + h1.Count);
                DisplayResult(h1);

                Console.ReadLine();
            }
            static void DisplayResult(Hashtable t1)
            {
                foreach (DictionaryEntry e in t1)
                    Console.WriteLine("哈希表中的键:{0},对应值{1}", e.Key, e.Value);
            }
        }
    }

    从输出结果可以发现,哈希表内部的排序是无序的,而且哈希表没有提供类似数组中的Sort方法,如果需要排序集合,可以考虑使用SortedList类。

    ===========================

    SortedList对象是可以排序的字典对象,SortedList内部管理值和相对应键的各一个数组。这些数组有初始容量,并可自动扩展,SortedList根据他们的键保持排序状态,这些特性要求为SortedList中指定的键实现IComparable接口。

    using System;
    using System.Collections.Generic;
    using System.Collections;
    using System.Text;

    namespace ConsoleApplication19
    {
        class Program
        {
            static void Main(string[] args)
            {
                SortedList h1 = new SortedList();
                h1.Add("键1", "键值1");
                h1.Add("键2", "键值2");
                h1.Add("键3", "键值3");
                h1.Add("键4", "键值4");
                h1.Add("键5", "键值5");
                h1.Add("键6", "键值6");
                h1.Add("键7", "键值7");
                Console.WriteLine("排序字典最初的初始化列表为:");
                DisplayResult(h1);
                Console.WriteLine("移除哈希表中的“键4”");
                h1.Remove("键4");
                Console.WriteLine("哈希表中的元素总数是:" + h1.Count);
                DisplayResult(h1);

                Console.ReadLine();
            }
            static void DisplayResult(SortedList t1)
            {
                foreach (DictionaryEntry e in t1)
                    Console.WriteLine("哈希表中的键:{0},对应值{1}", e.Key, e.Value);
            }
        }
    }

    ====================================

    搜索排序哈希表

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Hashtable ht = new Hashtable();
                ht.Add("key1", "this is value1");
                ht.Add("key2", "this is value2");
                ht.Add("key3", "this is value3");
                ht.Add("key4", "this is value4");
                ht.Add("key5", "this is value5");
                Console.WriteLine("排序前的哈希键值顺序为:");
                foreach (DictionaryEntry de in ht)
                    Console.WriteLine("键{0},值{1}", de.Key, de.Value);
                //获取键的集合
                ICollection keys = ht.Keys;
                //将键集合转换为ArrayList类
                ArrayList al = new ArrayList(keys);
                //使用ArrayList进行排序
                al.Sort();
                Console.WriteLine("排序后的哈希键值顺序为:");
                foreach (object key in al)
                    Console.WriteLine("键{0},值{1}", key, ht[key]);
                Console.ReadLine();
            }
        }
    }

    =========================

    实现用ArrayList搜索

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Hashtable ht = new Hashtable();
                ht.Add("key1", "this is value1");
                ht.Add("key2", "this is value2");
                ht.Add("key3", "this is value3");
                ht.Add("key4", "this is value4");
                ht.Add("key5", "this is value5");
                Console.WriteLine("排序前的哈希键值顺序为:");
                foreach (DictionaryEntry de in ht)
                    Console.WriteLine("键{0},值{1}", de.Key, de.Value);
                //获取键的集合
                ICollection keys = ht.Keys;
                //将键集合转换为ArrayList类
                ArrayList al = new ArrayList(keys);
                //使用ArrayList进行排序
                al.Sort();
                //在这里使用ArrayList的BinarySearch搜索指定键值的值
                int searchResult = al.BinarySearch("key3");
                if (searchResult > 0)
                    Console.WriteLine("搜索到的结果为:键{0},值{1}", al[searchResult], ht[al[searchResult]]);
                else
                    Console.WriteLine("没有搜索到有效的结果");
               
                Console.ReadLine();
            }
        }
    }


    转自:http://hi.baidu.com/zagj11/blog/item/f777ea6033ff1541eaf8f841.html

  • 相关阅读:
    Leetcode
    287. Find the Duplicate Number hard
    House Robber III leetcode 动态规划
    将一个数组分成奇数部分和偶数部分,并分别排好序 CVTE
    First Missing Positive && missing number
    permutation II (boss出来了)
    46. Permutations 回溯算法
    字符串分割 函数实现
    Combination Sum II Combinations
    用双缓存技术优化listview异步加载网络图片
  • 原文地址:https://www.cnblogs.com/liangwei389/p/1240941.html
Copyright © 2011-2022 走看看