散列冲突

散列冲突概念:如果当一个元素被插入时与一个已经插入的元素散列到相同的值,那么就会产生冲突,这个冲突需要消除。解决这种冲突的方法有几种:本章介绍两种方法:分离链接法和开放定址法1.分离链接法    其做法就是将散列到同一个值得所有元素保留到一个表中。我们可以使用标准库的实现方法。如果空间很紧(因为表是双向链表的并且浪费空间)。为执行一次查找,我们使用散列函数来确定是那一个链表,然后我们在被确定的链表

大家好,又见面了,我是你们的朋友全栈君。

概念:如果当一个元素被插入时与一个已经插入的元素散列到相同的值, 那么就会产生冲突, 这个冲突需要消除。解决这种冲突的方法有几种:本章介绍两种方法:分离链接法和开放定址法

1.分离链接法

    其做法就是将散列到同一个值得所有元素保留到一个表中。我们可以使用标准库的实现方法。如果空间很紧(因为表是双向链表的并且浪费空间)。
为执行一次查找,我们使用散列函数来确定是那一个链表, 然后我们在被确定的链表中执行一次查找。

    import java.util.LinkedList;
import java.util.List;

public class SeparateChainingHashTable<AnyType> { 
   
    private static final int DEFAULT_TABLE_SIZE = 101;
    private List<AnyType>[] theLists;
    private int currentSize;

    public SeparateChainingHashTable(){
        this(DEFAULT_TABLE_SIZE);
    }
    public SeparateChainingHashTable(int size){
        theLists = new LinkedList[nextPrime(size)];

        for(int i = 0; i < theLists.length; i++)
            theLists[i] = new LinkedList<>();
    }

    public boolean contains(AnyType x){
        List<AnyType> whichList = theLists[myhash(x)];
        return whichList.contains(x);
    }

    /* * 数据的插入 */
    public void insert(AnyType x){
        List<AnyType> whichList = theLists[myhash(x)];
        if(!whichList.contains(x)){
            whichList.add(x);

            //
            if(++currentSize > theLists.length)
                rehash();
        }
    }

    /* * 数据的删除 */
    public void remove(AnyType x){
        List<AnyType> whichList = theLists[myhash(x)];
        if(whichList.contains(x))
        {
            whichList.remove(x);
            currentSize--;
        }
    }

    public void makeEmpty(){
        for(int i = 0; i < theLists.length; i++)
            theLists[i].clear();
        currentSize = 0;
    }

    private void allocateArray(){
        for(int i = 0; i < theLists.length; i++)
            theLists[i] = new LinkedList<>();
    }

    //将传过来的数变成质数
    private static int nextPrime(int n){
        if(n < 11)
            return 11;
        else if(isPrime(n))
            return n;   
        else
            while(true){
                n++;
                if(isPrime(n)){
                    return n;
                }
            }
    }

    //判断是不是质数
    private static boolean isPrime(int n){
        if(n % 2 != 0 && n % 3 != 0 && n % 5 != 0 && n % 7 != 0)
            return true;
        else
            return false;
    }

    /* * 对分离链接散列表和探测散列表的在散列 */
    private void rehash(){
        List<AnyType> [] oldList = theLists;
        theLists = new LinkedList[theLists.length * 2];
        allocateArray();
        for(int i = 0; i < oldList.length; i++){
            List<AnyType> whichList = oldList[i];
            for (AnyType x : whichList) {
                insert(x);
            }
        }
    }

    /* * 散列表的myHash的方法 */
    private int myhash(AnyType x){
        int hashVal = x.hashCode();

        hashVal %= theLists.length;
        if(hashVal < 0)
            hashVal += theLists.length;

        return hashVal;
    }

    /*测试*/
    public static void main(String[] args) {
        SeparateChainingHashTable<String> hash = new SeparateChainingHashTable<>();
        hash.insert("Tom");
        hash.insert("SanZi");
        System.out.println(hash.contains("Tom"));
    }
}

2.开放定址法

不用链表的散列表

2.1线性探测法

就是在插入冲突的时候, 当前位置有值存放的话, 那么就会到下一个位置存放。这种解决冲突的方法虽然在前期效果明显, 但是在插入数量比较庞大的时候。 它解决冲突的时间和链接法的时间相差无几。所以在线性探测这种情况下优化, (平方探测法)。

2.1平方探测法
public class QuadraicProbindHashTale<T> { 
   

    /** * 数据域 * @param <T> : 泛型,存放对象 */
    private static class HashEntry<T>{ 
   
        public T element;
        public boolean isActive;

        public HashEntry(T e){
            this(e, true);
        }

        public HashEntry(T e, boolean i){
            element = e;
            isActive = i;
        }
    }

    /* * 默认的大小为11 */
    private static final int DEFAULT_TABLE_SIZE = 11;

    private static int currentSize = 0;

    private HashEntry<T>[] array;

    public QuadraicProbindHashTale(){
        this(DEFAULT_TABLE_SIZE);
    }

    /** * * @param size : 表的大小 */
    public QuadraicProbindHashTale(int size) {
        allocateArray(size);//先判断传过来的size是不是质数, 如果不是, 把它变成质数, 这样方便hash计算和不容易出现冲突情况
        makeEmpty();
    }

    /* * 判断对象是否在这个hash表当中 */
    public boolean contains(T x){
        int currentPos = findPos(x);
        return isActive(currentPos);
    }

    /** * 数据的插入 * @param x :数据的元素 * 首先调用findPox方法来判断在第一次执行hash的时候里面有没有元素,如果没有直接插入 * 如果有元素, 那么在存放的位置往后挪。(线性探测, 平方探测) */
    public void insert(T x){
        int currentPos = findPos(x);
        if(isActive(currentPos))
            return;

        array[currentPos] = new HashEntry<>(x, true);

        if(currentSize > array.length / 2)
            rehash();
    }

    /** * 数据的删除 * @param x :要删除的数据 * 在数据域内有识别这个内容是否有效的一个boolean类型, 当isActive是为true的时候, 表示有效 * 如果有效的话, 那么就删除。 */
    public void remove(T x){
        int currentPos = findPos(x);
        if(isActive(currentPos))
            array[currentPos].isActive = false;
    }

    /** */
    private void rehash(){
        HashEntry<T>[] oldArray = array;

        allocateArray(nextPrime(2 * oldArray.length));
        currentSize = 0;

        for(int i = 0; i < oldArray.length; i++)
            if(oldArray[i] != null && oldArray[i].isActive)
                insert(oldArray[i].element);
    }

    private boolean isActive(int currentPos){
        return array[currentPos] != null && array[currentPos].isActive;
    }

    /** * 查找在hash表中元素 * @param x :要查找的元素 * @return 所在数组中的位置 */
    private int findPos(T x){
        int offset = 1;
        int currentPos = myhash(x);

        while(array[currentPos] != null && !array[currentPos].equals(x)){
            currentPos += offset;
            offset += 2;
            if(currentPos >= array.length)
                currentPos -= array.length;
        }

        return currentPos;
    }

    /* * 初始化hash表中的所有的值为空 */
    public void makeEmpty(){
        currentSize = 0;
        for(int i = 0; i < array.length; i++)
            array[i] = null;
    }

    private void allocateArray(int arraySize){
        array = new HashEntry[nextPrime(arraySize)];
    }

    //将传过来的数变成质数
        private static int nextPrime(int n){
            if(n < 11)
                return 11;
            else if(isPrime(n))
                return n;   
            else
                while(true){
                    n++;
                    if(isPrime(n)){
                        return n;
                    }
                }
        }

        //判断是不是质数
        private static boolean isPrime(int n){
            if(n % 2 != 0 && n % 3 != 0 && n % 5 != 0 && n % 7 != 0)
                return true;
            else
                return false;
        }

        /* * 散列表的myHash的方法 */
        private int myhash(T x){
            int hashVal = x.hashCode();

            hashVal %= array.length;
            if(hashVal < 0)
                hashVal += array.length;

            return hashVal;
        }
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/146361.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • (精华)2020年6月28日 JavaScript高级篇 设计模式-发布订阅模式

    (精华)2020年6月28日 JavaScript高级篇 设计模式-发布订阅模式//发布订阅对象vareventObj={//缓存列表,存放订阅者的信息list:{},//添加订阅listen:function(key,fn){if(!this.list[key]){this.list[key]=[];}typeoffn===’function’&&this.list[key].push(fn);},//发布信息

    2022年8月20日
    7
  • SSM项目(GitHub上找的)

    SSM项目(GitHub上找的)SSM项目1.学生信息管理系统链接:https://pan.baidu.com/s/1e9ar4OKetL-40mp6R0b_4w提取码:01c8运行环境:jdk1.8以上服务器:tomcat运行软件:eclipse界面如下2.学生考试系统运行环境:jdk1.8以上服务器:tomcat运行软件:eclipse2.1学生前台2.2后台3.房屋出租系统运行环境:jdk1.8以上服务器:tomcat运行软件:eclipse…

    2022年6月29日
    46
  • android基于xposed框架,基于Xposed框架的模块:Android EagleEye

    android基于xposed框架,基于Xposed框架的模块:Android EagleEye基于Xposed框架的模块:AndroidEagleEye2015-06-2609:59:42阅读:0次一个基于Xposed框架的模块(可对Android系统APIs和应用的方法进行hook操作)。相关联的被hook的APIs或应用的方法信息将被作为输出被记录。使用AndroidEagleEye的一切风险由自己承担特性对Android系统APIs和应用的方法进行hook操作Hook需要的…

    2022年8月16日
    6
  • MySQL字段重复出现多少次

    MySQL字段重复出现多少次

    2020年11月19日
    420
  • 经典排序算法(1)——冒泡排序算法详解

    经典排序算法(1)——冒泡排序算法详解冒泡排序(BubbleSort)是一种典型的交换排序算法,通过交换数据元素的位置进行排序。一、算法基本思想(1)基本思想冒泡排序的基本思想就是:从无序序列头部开始,进行两两比较,根据大小交换位置,直到最后将最大(小)的数据元素交换到了无序队列的队尾,从而成为有序序列的一部分;下一次继续这个过程,直到所有数据元素都排好序。算法的核心在于每次通过两两比较交换位置,选出

    2022年7月8日
    23
  • 宽字节注入原理学习

    宽字节注入原理学习0x01开篇本题用到考点是宽字节注入,遇到这种注入类型学习记录。推荐两篇链接:浅析白盒审计中的字符编码及SQL注入|离别歌Von的博客|VonBlog为方便自我下次忘记,总结一下:1.宽字节涉及到编码问题,便于理解需要看一看2.宽字节注入现在已经很少见,因为如今的编码大多使用utf-8常见url编码:空格–%20′–%27#–%23\–%5c0x02原理我们注入时都会简单输入一个’或者”,进行测试,如果数据库过滤不严格就会产生报错

    2022年10月14日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号