哈希01

// 什么是Hash
// Hash是否一个表结构----哈希表?
//Hash 是一个数据结构么?
// FNV 是一个简单的,理论上背过后可以手码
//Hash 是一类算法的总称
//解释:Hash,是一种算法,有非常多的哈希公式,又叫做,散列、杂凑、
// 把任意长度的输入,通过哈希算法变化成固定长度的输出,那么输出的那个值,就叫做哈希值
// 面试官问: 哈希是一种压缩映射?正确
//哈希表是什么? 哈希映射的实现,哈希表。。。表达不真确,但是表达了哈希的整体的意图
// 特点:只要输入相同,输出则相同
// 哈希碰撞,有可能输入不同,输出相同
// 哈希的常用内容
//MD5dICTIONARYHashset、hashtable
HashSet<string> vs = new HashSet<string>();
Hashtable hashtable = new Hashtable();

// 常用的哈希函数
// 1 直接寻址 (A+B) 数字分析(算)、 平方区中、折中、余数、随机(随机数 )
// 推论:

// 还有 redis、加密算法、上数据库分布式、分布式事务、理论上只要涉及分布式,哈希是逃不掉的
// 为什么分布式必须得到所谓的哈希算法?
// 涉及一个问题:哈希一致性算法是怎么来的,为什么需要哈希一致性、

.net  hashcode 会重复么,

.net  hash 的简单使用  

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 哈希
{
    class Program
    {
        static void Main(string[] args)
        {

            Hashtable  testHashes = new Hashtable();
            //for (int i = 0; i < 3; i++)
            //{
            //    testHashes.Add(new TestHash() { Name="hnzheng",Age=111}.GetHashCode(),i);
            //}
            {
                List<TestHash> vs = new
                     List<TestHash>();
                vs.Add(new TestHash());
                vs.Add(new TestHash());
                //  数据不会调用gethascode 方法,数据组查询时,会将整个数组遍历,所以效率较差
            }

            //  以下是哈希实现的;三种使用,当插入时先是根据hashcode 判断,相同是在调用equels 判断
            {
                HashSet<TestHash> vs = new
                  HashSet<TestHash>();
                vs.Add(new TestHash());
                vs.Add(new TestHash());
                //  这里实际上直插入了一个,第二个被判断未同一个对象,未插入
            }
            {
                Hashtable hashtable = new Hashtable();
                // 第一次插入,调用GetHashCode(),返回111,能够插入
                  hashtable.Add(new TestHash(),"11");
                // 第二次插入,调用GetHashCode() ,返回111,发现hashtable已经存在111,
                //再继续调用equels方法,进行比较,如果返回ture,说明是同一个对象,报错键值重复 
                hashtable.Add(new TestHash(), "11");            
            }
            {
                 Dictionary<TestHash,string> dic = new Dictionary<TestHash,string>();
                // 第一次插入,调用GetHashCode(),返回111,能够插入
                dic.Add(new TestHash(), "11");
                // 第二次插入,调用GetHashCode() ,返回111,发现hashtable已经存在111,
                //再继续调用equels方法,进行比较,如果返回ture,说明是同一个对象,报错键值重复 
                dic.Add(new TestHash(), "11");
            }
            TestHash testHash = new
                 TestHash();
            TestHash testHash1 = new
               TestHash();
            if (testHash.Equals(testHash1))
            {

            }
            Console.ReadLine();
        }

    }


    public class TestHash
    {

        public override int GetHashCode()
        {      
                return 111;
            //return base.GetHashCode();
        }
        public override bool Equals(object obj)
        {
            return true;
        }

        public string Name { get; set; }
        public int Age { get; set; }
    }
}

  

原文地址:https://www.cnblogs.com/hnzheng/p/12763889.html