hashmap的简单实现
  sAAkk3Vxfaa8 2023年11月02日 53 0
  1.  Java Code 
package org.vocano.java.tst;          

import java.util.*;          

public           class SimpleHashMap<K,V>           extends AbstractMap<K,V> {          
              // Choose a prime number for the hash table
              // size, to achieve a uniform distribution:
              static           final           int SIZE =           997;          
              // You can't have a physical array of generics,
              // but you can upcast to one:
    @SuppressWarnings(          "unchecked")          
    LinkedList<SimpleEntry<K,V>>[] buckets =          
                  new LinkedList[SIZE];          
              public V put(K key, V value) {          
        V oldValue = null;          
                  int index = Math.abs(key.hashCode()) % SIZE;          
                  if(buckets[index] == null)          
            buckets[index] =           new LinkedList<SimpleEntry<K,V>>();          
        LinkedList<SimpleEntry<K,V>> bucket = buckets[index];          
        SimpleEntry<K,V> pair =           new SimpleEntry<K,V>(key, value);          
                  boolean found = false;          
        ListIterator<SimpleEntry<K,V>> it = bucket.listIterator();          
                  while(it.hasNext()) {          
            SimpleEntry<K,V> iPair = it.next();          
                      if(iPair.getKey().equals(key)) {          
                oldValue = iPair.getValue();          
                it.set(pair);           // Replace old with new
                found = true;          
                          break;          
            }          
        }          
                  if(!found)          
            buckets[index].add(pair);          
                  return oldValue;          
    }          
              public V get(Object key) {          
                  int index = Math.abs(key.hashCode()) % SIZE;          
                  if(buckets[index] == null)           return null;          
                  for(SimpleEntry<K,V> iPair : buckets[index])          
                      if(iPair.getKey().equals(key))          
                          return iPair.getValue();          
                  return null;          
    }          
              public Set<Map.Entry<K,V>> entrySet() {          
        Set<Map.Entry<K,V>> set=           new HashSet<Map.Entry<K,V>>();          
                  for(LinkedList<SimpleEntry<K,V>> bucket : buckets) {          
                      if(bucket == null)           continue;          
                      for(SimpleEntry<K,V> mpair : bucket)          
                set.add(mpair);          
        }          
                  return set;          
    }          
              public           static           void main(          String[] args) {          
        SimpleHashMap<          String,          String> m =          
                      new SimpleHashMap<          String,          String>();          
        m.put(          "a",           "b");          
        System.out.println(m.get(          "a"));          
    }          
}
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  2Vtxr3XfwhHq   2024年05月17日   55   0   0 Java
  Tnh5bgG19sRf   2024年05月20日   110   0   0 Java
  8s1LUHPryisj   2024年05月17日   46   0   0 Java
  aRSRdgycpgWt   2024年05月17日   47   0   0 Java
sAAkk3Vxfaa8