• 手写HashTable


    首先罗列几个HashTable的方法:

    1.size();

    2.isEmpty();

    3.clear();

    4.put(String Key,Integer value);

    5.get(String key);

    6.containsKey(String key);

    7.ContainsKey(Integer Value);

    8.remove(String key);

    public class HashTable{

        private ListNode[] map;

        private int size;

        private float loadFactor;

     //static final variable are global variable;

    public final static int DEFAULT_CAPACITY = 16;

    public final static int DEFAULT_LOAD_FACTOR = 0.75f;

      public HashTable(int capacity,float loadFactory){

          map = new ListNode(capacity);

          this.loadFactory = loadFactory;

    }

      public HashTable(){

          this(DEFAULT_CAPACITY,DEFAULT_LOAD_FACTOR);

    }

      public HashTable(int capacity){

          this(capacity,DEFAULT_LOAD_FACTOR);      

    }

       public int size(){

          return this.size;  

    }

      public void clear(){

          map = new ListNode(map.length);

          this.size = 0;

    }

      private int getIndex(int hashcode){

       return hashcode % capacity;     

    }

      public int getHashCode(String key){

        if(key == null){

          return 0;

    }

      int hash = key.hashcode();

      if(hash < 0) 

      { hash = hash & 7FFFFFF;

        return hash;

    }

    }

      public void put(String key,Integer value){

          int hash = getHashCode(key);

          int index = getIndex(hash);

          ListNode node = map[index];

          if(node == null){

            node = new ListNode(key,value);

            map[index] = node;

          }

            else{

            while(node.next != null){

              if(node.key.equals(key)){

                 node.value = value;

                 return;

                                    }

                node = node.next;

                       }

            node.next = new ListNode(key,value);

    }

    private boolean equalsKey(K k1, K k2){

        if(k1 == null && k2 == null){

          return true;

    }

        if(k1 == null || k2 == null){

          return false;

    }

        return k1.equals(k2);

    }

    public V put(K key, V value){

        int index = getIndex(key);

        Node<K,V> head = array[index];

        Node<K,V> node = head;

        while(node != null){

          if((equalsKey(key1,key2)){

            V result = node.value;

            node.value = value;

            return result;

    }

        node = node.next;

    }

    }

    }

      

      

      

    }

  • 相关阅读:
    成为一个优秀的C++程序员
    C++强制类型转换:static_cast、dynamic_cast、const_cast、reinterpret_cast
    C++经典类库(C++开发必看)
    auto_ptr, unique_ptr, shared_ptr and weak_ptr智能指针讲解
    C++ clone()函数的用法
    c++ operator操作符的两种用法:重载和隐式类型转换,string转其他基本数据类型的简洁实现string_cast
    C++模板详解(转)
    static变量的作用(转)
    2018年12月17日,新工作,可是一点提不起兴趣,在原点转啊转,园子什么时候会关了呢,我们终将老去
    给C#Control组件统一增加加属性
  • 原文地址:https://www.cnblogs.com/xujiangxi/p/12267129.html
Copyright © 2020-2023  润新知