zoukankan      html  css  js  c++  java
  • HashMap底层实现练习

    /**
     * Created by chengbx on 2018/5/19.
     * 简单版
     */
    public class CbxMap {
        CbxEntry [] cbxEntries = new CbxEntry[999];
        int size;
        public void put(Object key,Object value){
            //解决键重复的问题
            for (int i = 0; i < size; i++) {
                if(cbxEntries[i].getKey().equals(key)){
                    cbxEntries[i].setValue(value);
                    return ;
                }
            }
            CbxEntry cbxEntry = new CbxEntry(key,value);
            cbxEntries[size++] = cbxEntry;
        }
        public Object get(Object key){
            for (int i = 0; i < size; i++) {
                if(cbxEntries[i].getKey().equals(key)){
                    return cbxEntries[i].getValue();
                }
            }
            return null;
        }
        public boolean containsKey(Object key){
            for (int i = 0; i < size; i++) {
                if(cbxEntries[i].getKey().equals(key)){
                    return true;
                }
            }
            return  false;
        }
        public boolean containsValue(Object value){
            for (int i = 0; i < size; i++) {
                if(cbxEntries[i].getValue().equals(value)){
                    return true;
                }
            }
            return  false;
        }
    
    }
    class CbxEntry{
        private Object key;
        private Object value;
    
        public Object getKey() {
            return key;
        }
    
        public void setKey(Object key) {
            this.key = key;
        }
    
        public Object getValue() {
            return value;
        }
    
        public void setValue(Object value) {
            this.value = value;
        }
    
        public CbxEntry(Object key, Object value) {
            this.key = key;
            this.value = value;
        }
    }
    /**
     * Created by chengbx on 2018/5/19.
     * 优化版
     */
    public class CbxHashMap {
        LinkedList[] linkedArr = new LinkedList[999];//Map的底层结构就是:数组 + 链表
        int size;
        public  int getHashCodeByKey(Object key){
            return key.hashCode() % linkedArr.length;
        }
    
        public void put(Object key,Object value){
           CbxEntry cbxEntry = new CbxEntry(key,value);
           int arrIndex = getHashCodeByKey(key);
           if(linkedArr[arrIndex]==null){
                LinkedList linkedList = new LinkedList();
                linkedList.add(cbxEntry);
                linkedArr[arrIndex] = linkedList;
           }else{
               linkedArr[arrIndex].add(cbxEntry);
           }
        }
    
        public Object get(Object key){
            int arrIndex = getHashCodeByKey(key);
            if(linkedArr[arrIndex]!=null){
                LinkedList linkedList= linkedArr[arrIndex];
                for (int i = 0; i <linkedList.size() ; i++) {
                    CbxEntry cbxEntry = (CbxEntry)linkedList.get(i);
                    if(cbxEntry.getKey().equals(key)){
                        return cbxEntry.getValue();
                    }
                }
            }
            return null;
        }
    
    }
  • 相关阅读:
    VS生成Map文件
    Google Android Studio Kotlin 开发环境配置
    byte数组存储到mysql
    C# 读取 appconfig文件配置数据库连接字符串,和配置文件
    关于byte[]与string、Image转换
    运行vs2010,Debug时发生“无法启动程序"http://localhost:xxx",系统找不到指定文件
    【转】使用Navicat for Oracle新建表空间、用户及权限赋予
    eclipse创建maven项目
    使用eclipse自动生成WSDL客户端代码
    使用Apache CXF根据wsdl文件生成代码
  • 原文地址:https://www.cnblogs.com/cbxBlog/p/9127128.html
Copyright © 2011-2022 走看看