Java 一个简单Hashtable的实现

Hashtable.GIF

如图,是Hashtable采用的链表式的结构,即键值冲突的时候直接加到此键值对应的链表后面。下面提供一个简单的Java类的实现。

  1. public class HashTableDef {
  2.     private int capacity = 1000;// 非重复的键值个数,即键值从0-999,可以在初始话的时候指定
  3.     private Map[] keys;// 存放键值所对应的key-value对
  4.     /**
  5.      * 默认容量的构造函数,默认容量为1000
  6.      */
  7.     public HashTableDef() {
  8.         keys = new Map[capacity];
  9.         for (int i = 0; i < capacity; i++) {
  10.             keys[i] = null;
  11.         }
  12.     }
  13.     /**
  14.      * 带容量的构造函数
  15.      * 
  16.      * @param cy
  17.      */
  18.     public HashTableDef(int cy) {
  19.         this.capacity = cy;
  20.         keys = new Map[this.capacity];
  21.         for (int i = 0; i < capacity; i++) {
  22.             keys[i] = null;
  23.         }
  24.     }
  25.     /**
  26.      * 把键值对key-value放到hash表中
  27.      * 
  28.      * @param key
  29.      * @param value
  30.      */
  31.     public void put(Object key, Object value) {
  32.         int keyIndex = key.hashCode() % capacity;// 得到此键对应的键值
  33.         boolean isSameKey = false;
  34.         Map map = new Map();
  35.         map.key = key;
  36.         map.value = value;
  37.         Map currentMap = keys[keyIndex], prevMap = null;
  38.         if (currentMap == null) {
  39.             // 当前键值没有冲突,
  40.             keys[keyIndex] = map;
  41.             System.out.println("直接放入到hashtable中" + keys[keyIndex] + " "
  42.                     + currentMap);
  43.         } else {
  44.             // 处理键值冲突,放到此键值对应的链表的最后
  45.             while (currentMap != null) {
  46.                 // 不光是键值冲突,而且键相等,此时此时覆盖以前的键对应的键值
  47.                 if (currentMap.key.equals(key) || currentMap.key == key) {
  48.                     currentMap.value = value;
  49.                     isSameKey = true;
  50.                     System.out.println("出现了同名的Key:" + key);
  51.                     break;
  52.                 }
  53.                 prevMap = currentMap;
  54.                 currentMap = currentMap.next;
  55.             }
  56.             if (isSameKey == false) {
  57.                 System.out.println("放到链表中");
  58.                 prevMap.next = map;
  59.             }
  60.         }
  61.     }
  62.     /**
  63.      * 给定键值,取得此键值对应的Value,如果没有,返回null
  64.      * 
  65.      * @param key
  66.      *            键值
  67.      * @return
  68.      */
  69.     public Object get(Object key) {
  70.         int keyIndex = key.hashCode() % capacity;// 得到此键对应的键值
  71.         Map currentMap = keys[keyIndex];
  72.         // 遍历此键值对应的入口链表
  73.         while (currentMap != null) {
  74.             // 出现相等的键,则返回
  75.             if (currentMap.key.equals(key) || currentMap.key == key) {
  76.                 return currentMap.value;
  77.             }
  78.             currentMap = currentMap.next;
  79.         }
  80.         return null;
  81.     }
  82.     private class Map {
  83.         public Object key;
  84.         public Object value;
  85.         public Map next = null;
  86.     }
  87. }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值