利用ArrayList对Hashtable其进行排序
2013-12-04 21:14
323 查看
前言:
最近在使用Hashtable的时候发现一个问题:就是当你对Hashtable进行遍历的时候整个输出结果是毫无顺序的,上网查了一下说是Hashtable有自己内部的排序机制,如果要自定义排序的话就要写算法,反正是很麻烦,而我现在
需要实现的仅仅是输入顺序和输入顺序对应即可,没必要这么麻烦的去写算法。
这里我们就需要使用到ArrayList,大家都知道ArrayList默认是不排序的(添加的顺序就是输出的顺序)。让它
和hashtable结合不就实现这种功能的吗?这样继承了Hashtable具有Hashtable的丰富功能,又满足ArrayList不排序
的功能。上网查了一些资料,顺便也介绍一些其他的排序方法,大家一起学习。
没有排序之前:
using System.Collections; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Hashtable map = new Hashtable(); map.Add("TableName", "T_English_Close"); map.Add("QuestionID", "10"); map.Add("Scope", "课内"); map.Add("BookName", "第一册"); map.Add("Chapter", "第一章"); map.Add("QuestionType", "完型填空"); map.Add("Degree", "2"); map.Add("Fraction", "10"); foreach (string str in map.Keys) { Console.WriteLine(str + " : " +map[str]); } Console.Read(); } } }
输出结果:
一:输入什么顺序,输出就是什么顺序。
首先我们需要声明一个类来继承Hashtable,并重新它的一些方法,使ArrayList与Hashtable结合在一起:public class AltHashTable : Hashtable { private ArrayList list = new ArrayList(); public override void Add(object key, object value) { base.Add(key, value); list.Add(key); } public override void Clear() { base.Clear(); list.Clear(); } public override void Remove(object key) { base.Remove(key); list.Remove(key); } public override ICollection Keys { get { return list; } } }
然后我们重新测试一下:
using System.Collections; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { AltHashTable map= newAltHashTable(); map.Add("TableName", "T_English_Close"); map.Add("QuestionID", "10"); map.Add("Scope", "课内"); map.Add("BookName", "第一册"); map.Add("Chapter", "第一章"); map.Add("QuestionType", "完型填空"); map.Add("Degree", "2"); map.Add("Fraction", "10"); foreach (string str in map.Keys) { Console.WriteLine(str + " : " +map[str]); } Console.Read(); } } }
输出结果正是我们想要的:
二:下面我们在分别介绍一下按Key值和Value值排序的方法。
首先要知道如何将Hashtable转换为ArrayList:ArrayList list = new ArrayList(ht.Values); ArrayList list= new ArrayList(ht.Keys);
按Key值排序:
using System.Collections; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Hashtable map = new Hashtable (); map.Add("TableName", "T_English_Close"); map.Add("QuestionID", "10"); map.Add("Scope", "课内"); map.Add("BookName", "第一册"); map.Add("Chapter", "第一章"); map.Add("QuestionType", "完型填空"); map.Add("Degree", "2"); map.Add("Fraction", "10"); ArrayList list = new ArrayList(map.Keys); list.Sort(); foreach (string str in list) { Console.WriteLine(str + ":" + map[str]); } Console.Read(); } } }输出结果:
按Value值排序跟按Key值排序一样,在这里就不在写代码了。大家可以自己动手实践一下!
原理:
实际上是按照每一个字符的ASCII的值就行排序的。从左到右比较每个字符的Ascii的值,直到满足两个字符的ASCII的值不同即停止比较 。
总结:
针对Hashtable排序的特殊要求,可以先把它的键或值转化成ArrayList,针对ArrayList进行排序,进而也就实现了Hashtable的排序.毕竟ArrayList已经支持一些排序。而且ArrayList的排序种类非常多,我们在下面文章中在重点说一
下ArrayList的使用。
相关文章推荐
- 利用ArrayList对Hashtable其进行排序
- hasSet,TreeSet,ArrayList,LinkedList,Vector,HashMap,HashTable,TreeMap利用Iterator进行输出
- JAVA 利用Arraylist.sort()进行排序的实现。(comparable接口的实现)
- 利用J2ME里的RMS对记录进行排序
- 利用社会安全号码对学生记录构成的数组排序。1000个桶的基数排序并分三趟进行
- 对ArrayList实例对象中的元素进行排序
- [置顶] 利用冒泡排序法,对任意数组进行排序;
- 如何利用php array_multisort函数 对数据库结果进行复杂排序
- 利用MapReduce进行排序
- 对ArrayList中的元素按照某一列进行排序
- C#.NET 对HashTable数组进行按值排序
- 使用指定的比较器对整个 System.Collections.ArrayList 中的元素进行排序。
- 用ArrayList对数组进行排序
- java利用冒泡排序对数组进行排序
- NSArray利用Cocoa框架进行汉字排序
- 第十六周 利用指针对两个数字进行排序
- java中利用二叉树算法进行排序
- 利用Linux命令行进行文本按行去重并按重复次数排序
- 利用Comparable进行排序
- java对arrayList中的元素进行排序