首页 新闻 会员 周边 捐助

Hashtable和Dictionary<T,K>的内部排序

0
[已解决问题] 解决于 2008-08-04 11:14
<P>Hashtable和Dictionary&lt;T,K&gt;的内部排序算法不一样,请看代码</P><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> <DIV class=cnblogs_code><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dictionary</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;ht</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Dictionary</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">();<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ht.Add(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">http://www.sina.com.cn</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #800000">""</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ht.Add(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">http://www.bjut.edu.cn</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #800000">""</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ht.Add(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">http://lib.bjut.edu.cn</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #800000">""</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ht.Add(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">http://news.bjut.edu.cn</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #800000">""</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ht.Add(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">http://sse.bjut.edu.cn</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #800000">""</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ht.Add(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">http://lexus.cnblogs.com</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #800000">""</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ht.Add(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">http://www.sina.com.cn/sport</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #800000">""</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ht.Add(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">http://www.sina.com.cn/ent</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #800000">""</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">foreach</SPAN><SPAN style="COLOR: #000000">(var&nbsp;kvp&nbsp;</SPAN><SPAN style="COLOR: #0000ff">in</SPAN><SPAN style="COLOR: #000000">&nbsp;ht)<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(kvp.Key);<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">============================================</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hashtable&nbsp;ht2</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Hashtable();<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ht2.Add(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">http://www.sina.com.cn</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #800000">""</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ht2.Add(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">http://www.bjut.edu.cn</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #800000">""</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ht2.Add(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">http://lib.bjut.edu.cn</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #800000">""</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ht2.Add(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">http://news.bjut.edu.cn</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #800000">""</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ht2.Add(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">http://sse.bjut.edu.cn</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #800000">""</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ht2.Add(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">http://lexus.cnblogs.com</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #800000">""</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ht2.Add(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">http://www.sina.com.cn/sport</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #800000">""</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ht2.Add(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">http://www.sina.com.cn/ent</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #800000">""</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">foreach</SPAN><SPAN style="COLOR: #000000">(DictionaryEntry&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #0000ff">in</SPAN><SPAN style="COLOR: #000000">&nbsp;ht2)<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(i.Key);</SPAN></DIV> <DIV class=cnblogs_code><SPAN style="COLOR: #000000">两组实现的代码一样,但是输出的排序结果不一样,这是为什么,我觉得Hashtable的排序应该是比较正常的,大家觉得呢?</SPAN></DIV>
lexus的主页 lexus | 初学一级 | 园豆:0
提问于:2008-08-04 09:33
< >
分享
最佳答案
-1
Dictionary<T,K>默认没有排序的意思是它将按你Add的顺序来存放,本身不会做排序操作 Hashtable的排序是根据Key的HashCode来进行的,HashCode来自于Key的GetHashCode方法,一般值类型都直接使用系统已经实现的GetHashCode方法,但引用类型多数情况下建议重写GetHashCode()
丁学 | 专家六级 |园豆:18730 | 2008-08-04 11:01
其他回答(2)
1
两者都相当于无序集合,顺序是无所谓的.
deerchao | 园豆:8367 (大侠五级) | 2008-08-04 09:39
0
Dictionary<T,K>默认没有排序,而hashtable默认有一个排序. 参考一下这个: http://sifang2004.cnblogs.com/archive/2006/01/05/311418.html
侯垒 | 园豆:3435 (老鸟四级) | 2008-08-04 09:59
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册