两个哈希表之间如何判断是不是一样 如果不一样怎么输出不一样的列

解决方案 »

  1.   

    因为hashtable默认是FIFO的存储模式.
    所以要比较的话,先得把两个对象内的数据按相同的规则排序.然后再逐项比较.得出不一样的列.
      

  2.   

    不好意思.上面讲错了,不是FIFO.只是无序存储.
      

  3.   

    循环第一个hash表的键值,然后看第2个里面有没有,有的话再判断value是不是相同
      

  4.   


    HashTable ht1 = new HashTable();
    HashTable ht2 = new HashTable();
    ht1.Add(1,1);
    ht1.Add(2,2);
    ht1.Add(3,3);
    ht2.Add(2,2);
    ht2.Add(3,3);
    ht2.Add(4,4);
    IDictionaryEnumerator ide = ht1.GetEnumerator();
    HashTable table = new HashTable();
    while(ide.MoveNext())
    {
        if(!ht2.ContainsKey(ide.Key))
        {
            table.Add("ht1有但ht2没有的Key:"+ide.Key.ToString(),ide.Value);
        }
        else
        {
            if(!ht2.ContainsValue(ide.Value))
            {
                table.Add(ide.Key,"两者都有相同Key但Value不一样的ht1中的Value:"+ide.Value.ToString());
            }
        }   
    }
    ide = ht2.GetEnumerator();
    while(ide.MoveNext())
    {
        if(!ht1.ContainsKey(ide.Key))
        {
            table.Add("ht2有但ht1没有的Key:"+ide.Key.ToString(),ide.Value);
        }
        else
        {
            if(!ht1.ContainsValue(ide.Value))
            {
                table.Add(ide.Key,"两者都有相同Key但Value不一样的ht2中的Value:"+ide.Value.ToString());
            }
        }   
    }
    if(table.Count>0)
    {
        ide = table.GetEnumerator();
        while(table.MoveNext())
            Console.WriteLine(table.Key.ToString()+","+table.Value.ToString());
    }
    else
        Console.WriteLine("两个哈希表是一样的");