HashMap

12 篇文章 0 订阅
3 篇文章 0 订阅

自己设计一个HashMap

采用桶+ 链地址法来实现一个HashMap,

  1. 设计链表类
  2. 设计桶类 ,包括一个头节点,head = new ListNode(-1,-1);
  3. hashmap , 桶类数组情况 ,ListNode[] buckets = new ListNode[], 基础数目就可以了,桶数组
class ListNode {
    ListNode next;
        int key, val;
        ListNode(int key, int value) {
            this.key = key;
            this.val = value;
        }
}
// 保存头尾节点的节本情况
class Buckets{
  //  final ListNode head = new ListNode(-1,-1);
    ListNode head;
   // ListNode tail;
    public Buckets(){
        head =new ListNode(-1,-1);
     //   tail = head;
    }
}
    
public class MyHashMap{
   
    // 如果插入
    /** Initialize your data structure here. */
     final int size = 10000;
     Buckets[]  buckets =new Buckets[size];
     // 数据结构的存储
    public MyHashMap() {
              
    }
    
    // 不要使用
    /** value will always be non-negative. */
    private int hash(int key){
        return key % size;
    }
   
    private ListNode find(Buckets list,int key){
        if(list==null) return null;
        ListNode first = list.head.next;
        while(first!=null){
            if(first.key==key){
                return first;
            }
            //查找语句怎么可能没有
            first = first.next;
        }
        return null;
            
    }
        
    public void put(int key, int value) {
             //ListNode insert = new ListNode(key, value);
          int index = hash(key);
          if(buckets[index]==null)
              buckets[index]=new Buckets();
          ListNode findResult= find(buckets[index],key);    //如何存在并且并且不为空的话,我们可以做出额正确的选择
          ListNode node =null; 
        
        if(findResult==null)
         {
              ListNode insert= new ListNode(key,value);
               node = buckets[index].head;
             //该链表当中的最后一个节点加入即可
              while(node.next!=null){
                  node = node.next;
              }
            node.next=insert;
          }
        
        // 如果本身存在的话直接
          else
          {
              findResult.val = value;
          }
              
        
    }
    //如何进行查找, 如果是相同节点key, value相同荻花
    
    /** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
    public int get(int key) {
         int index = hash(key);
         ListNode result = find(buckets[index],key);
        if(result==null)
            return  -1;
        return result.val;
        
    }
    
    /** Removes the mapping of the specified value key if this map contains a mapping for the key */
    public void remove(int key) {
           // 链表实现一个删除吗?
         int index = hash(key);
    // 直接为null, 直接删除即可
         if(buckets[index]==null)   return;
         ListNode head = buckets[index].head;
         ListNode current=null;
         if(head!=null){
              current = head.next;
         }
        
        //
         while(head!=null && current!=null){
            if(current.key==key){
                // 直接删除即可,然后直接返回
                head.next = current.next;
                return;
            }
            // 接下来遍历下面的基本元素,遍历查找工程
            head = current;
            current = current.next;
        }
    }
    
}

/**
 * Your MyHashMap object will be instantiated and called as such:
 * MyHashMap obj = new MyHashMap();
 * obj.put(key,value);
 * int param_2 = obj.get(key);
 * obj.remove(key);
 */

// 优化上诉put ,remove 等操作涉及到的查找功能,情况

 private ListNode find(Buckets list,int key){
        if(list==null) return null;
        ListNode first = list.head.next;
        while(first!=null){
            if(first.key==key){
                return first;
            }
            //查找语句怎么可能没有
            first = first.next;
        }
        return null;  
    }

每次查找不直接返回要查找的节点情况,只是返回该节点的前驱节点, 如果查找不成功也可以返回桶当中的最后一个节点,这样插入put(),方法也可以重用这部分代码的情况

优化后的代码情况

class ListNode {
        ListNode next;
        int key, val;
        
        ListNode(int key, int value) {
            this.key = key;
            this.val = value;
        }
    }
    
    class Bucket {
        final ListNode head = new ListNode(-1, -1);
    }
    public class MyHashMap {
        
    
    final int size = 10000;
    final Bucket[] buckets = new Bucket[size];

    /** Initialize your data structure here. */
    public MyHashMap() {
        
    }
    
    //used to find the bucket
    private int hash(int key) {
        return key % size;
    }
    
    //used to find node by key in certain bucket
    private ListNode findNode(Bucket bucket, int key) {
        ListNode head = bucket.head;
        ListNode node = head;
        while (head != null && head.key != key) {
            //when head == null, or head.key == key, return its previous node
            node = head;
            head = head.next;
        }
        return node;
    }
    
    /** value will always be non-negative. */
    public void put(int key, int value) {
        int i = hash(key);
        if (buckets[i] == null) buckets[i] = new Bucket();
        ListNode pre = findNode(buckets[i], key);
        if (pre.next == null) {
            pre.next = new ListNode(key, value);
        } else {
            pre.next.val = value;
        }
    }
    
    /** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
    public int get(int key) {
        int i = hash(key);
        if (buckets[i] == null) return -1;
        ListNode pre = findNode(buckets[i], key);
        if (pre.next == null) {
            return -1;
        } else {
            return pre.next.val;
        }
    }
    
    /** Removes the mapping of the specified value key if this map contains a mapping for the key */
    public void remove(int key) {
        int i = hash(key);
        if (buckets[i] == null) return;
        ListNode pre = findNode(buckets[i], key);
        if (pre.next == null) return;
        else pre.next = pre.next.next;
    }

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值