zoukankan      html  css  js  c++  java
  • Dictionary<byte[],string> 出现错误的解决方案

    今天在使用 Hbase 的Thrift接口的时候, C# 生成的代码出现错误为, “给定的关键字不在字典中”

    image

    经google 后找到答案, 原地址为: http://stackoverflow.com/questions/1440392/use-byte-as-key-in-dictionary

    下面是我的测试代码,原代码中 有一个错误  “ <> “ 要改为“!=” 就好了。

    public class ByteArrayComparer : IEqualityComparer<byte[]>
      {
          public bool Equals(byte[] left, byte[] right)
          {
              if (left == null || right == null)
              {
                  return left == right;
              }
              if (left.Length != right.Length)
              {
                  return false;
              }
              for (int i = 0; i < left.Length; i++)
              {
                  if (left[i] != right[i])
                  {
                      return false;
                  }
              }
              return true;
          }
          public int GetHashCode(byte[] key)
          {
              if (key == null)
                  throw new ArgumentNullException("key");
              int sum = 0;
              foreach (byte cur in key)
              {
                  sum += cur;
              }
              return sum;
          }
      }
       

      //public class ByteArrayComparer : IEqualityComparer<byte[]>
      //{
      //    public bool Equals(byte[] left, byte[] right)
      //    {
      //        if (left == null || right == null)
      //        {
      //            return left == right;
      //        }
      //        return left.Equals(right);
      //        //return left.SequenceEquals(right);
      //    }
      //    public int GetHashCode(byte[] key)
      //    {
      //        if (key == null)
      //            throw new ArgumentNullException("key");

      //      return  key.GetHashCode();
      //        //return key.Sum();
      //    }
      //}

      class Program
      {

          static void Main(string[] args)
          {

              Dictionary<byte[], string> dic = new Dictionary<byte[], string>(new ByteArrayComparer());
              dic.Add("key".ToBytes(), "zbbb");
              Console.WriteLine(dic["key".ToBytes()]);
    }

    }

    注意 gethashcode 可能会有问题,先放这里,随后再看。

  • 相关阅读:
    谈对信息增益与决策树的理解
    k近邻法
    感知机相关难点细解
    点到空间中面的距离
    统计学习方法中的标注问题
    Hoeffding不等式与泛化误差上界
    经验风险与期望风险
    先验概率与后验概率
    spring和springboot常用注解总结
    多环境下读取不同的配置文件
  • 原文地址:https://www.cnblogs.com/zbw911/p/2270841.html
Copyright © 2011-2022 走看看