首页 > 其他 > 详细

数据结构---基本数据结构---哈希表

时间:2019-03-07 19:15:43      阅读:114      评论:0      收藏:0      [点我收藏+]

1、概述

    1.1  哈希表:是一种数据结构,提供了快速插入、查找的操作;

    1.2  哈希表基于数组实现;

    1.3  哈希化:

          a,key为数字:

              将key作为数组索引;

          b,key为字母:

              将字母转换成ASCII码值,然后进行相加;(问题:转换后的hash值很容易相等)

              幂的连乘;(问题:转换后的hash值很大)

              压缩可选值:对转换后的hash值取余(问题:转换后的hash值冲突) 

                    解决:开放地址法、链地址法

package com.exiuge.mytest.hashtable;

public class Info {

    private String key;
    private String value;

    public Info(){

    }

    public Info(String key,String value){
        this.key=key;
        this.value=value;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}
package com.exiuge.mytest.hashtable;

public class HashTable {

    private Info[] arr;

    public HashTable(){
        arr=new Info[50];
    }

    /**
     * insert
     * @param info
     */
    public void insert(Info info){
        arr[hashCode(info.getKey())]=info;
    }

    /**
     * find
     * @param key
     * @return
     */
    public Info find(String key){
        return arr[hashCode(key)];
    }

    /**
     * hash值转换
     * @param key
     * @return
     */
    public int hashCode(String key){
        int hashValue=0;
        int mi=27;
        for (int i=key.length()-1;i>=0;i--) {
            int letter=key.charAt(i)-96;
            hashValue+=letter*mi;
        }
        return hashValue%arr.length;
    }
}

 

数据结构---基本数据结构---哈希表

原文:https://www.cnblogs.com/anpeiyong/p/10491402.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!