C#Dictionary不能添加重复键的解决方法


重载了一个方法:

 public class RepeatDictionaryComparer : IEqualityComparer<string>
    {
        public bool Equals(string x, string y)
        {
            return x != y;
        }
        public int GetHashCode(string obj)
        {
            return obj.GetHashCode();
        }
    }

  调用:

Dictionary<string, string> _dic = new Dictionary<string, string>(new RepeatDictionaryComparer());

  这样重复的键也可以添加了。

java中Map集合类:

Map集合类                              Key                       Value                 Super          说明

HashTable                         不允许为null            不允许为null      Dictionary    线程安全

concurrentHashMap         不允许为null            不允许为null       AbstractMap   锁分段技术和CAS

TreeMap                            不允许为null            允许为null         AbstractMap    线程不安全

HashMap                          允许为null               允许为null          AbstractMap   线程不安全,resize死链问题

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM