地图界面就是您要寻找的。根据Javadocs:
Dictionary 类是任何类的抽象父类,例如 Hashtable,它将键映射到值。每个键和每个值都是一个对象。在任何一个 Dictionary 对象中,每个键最多与一个值相关联。给定一个字典和一个键,可以查找相关的元素。任何非空对象都可以用作键和值。通常,此类的实现应使用 equals 方法来确定两个键是否相同。
注意:此类已过时。新的实现应该实现 Map 接口,而不是扩展这个类。
正如 Javadocs 所述,哈希映射包含键值对,以下是实现此类的方式:
package hashmap;
import java.util.*;
public class HashMap<K,V> implements Map<K,V> {
private final int TABLESIZE = 101;
private LinkedList<Node<K, V>>[] list;
@SuppressWarnings("unchecked")
public HashMap() {
// Creates a hash table with a table size of TABLESIZE
list = new LinkedList[TABLESIZE];
}
// Returns the Hasindex of a key
private int getHashIndex(Object key) {
String hashCode = key.toString();
int i = Integer.parseInt(hashCode.substring(hashCode.length() - 4));
return i % TABLESIZE;
}
// get 方法首先找到桶然后迭代每个桶 public V get(Object key) { try {
LinkedList<Node<K,V>> bucket = list[getHashIndex(key)];
Iterator<Node<K,V>> itr = bucket.iterator();
while (itr.hasNext()) {
Node<K,V> node = itr.next();
if (node.getKey().equals(key)) {
return node.getValue();
}
}
}
// if there is no bucket return null
catch (NullPointerException e) {
return null;
}
return null;
}
//add method
public V put(K key, V value) {
// Hvis der ikke er en spand i forvejen tilføjes en ny
if (list[getHashIndex(key)] == null) {
list[getHashIndex(key)] = new LinkedList<Node<K,V>>();
}
LinkedList<Node<K,V>> bucket = list[getHashIndex(key)];
Iterator<Node<K,V>> itr = bucket.iterator();
V prevouis = null;
while (itr.hasNext()) {
Node<K,V> node = itr.next();
if (node.getKey().equals(key)) {
prevouis = node.getValue();
bucket.remove(node);
break;
}
}
bucket.addLast(new Node<K,V>(key, value));
return prevouis;
}
// remove method
public V remove(Object key) {
// Itererer igennem spanden for at finde og slette den korrekte nøgle
LinkedList<Node<K,V>> bucket = list[getHashIndex(key)];
Iterator<Node<K,V>> itr = bucket.iterator();
V value = null;
while (itr.hasNext()) {
Node<K,V> node = itr.next();
if (node.getKey().equals(key)) {
value = node.getValue();
bucket.remove(node);
break;
}
}
if (bucket.isEmpty()) {
list[getHashIndex(key)] = null;
}
return value;
}
@Override
public void clear() {
// TODO Auto-generated method stub
}
@Override
public boolean containsKey(Object key) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean containsValue(Object value) {
// TODO Auto-generated method stub
return false;
}
@Override
public Set<java.util.Map.Entry<K, V>> entrySet() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public Set<K> keySet() {
// TODO Auto-generated method stub
return null;
}
@Override
public void putAll(Map<? extends K, ? extends V> m) {
// TODO Auto-generated method stub
}
@Override
public int size() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Collection<V> values() {
// TODO Auto-generated method stub
return null;
}
public void printHashTable() {
System.out.println("HashTable:");
int nodeCount = 0;
for (int i = 0; i < list.length; i++) {
LinkedList<Node<K,V>> bucket = list[i];
if (bucket != null) {
nodeCount += bucket.size();
System.out.println("Index " + i + ": " + bucket);
}
}
System.out.println("Total nodes: " + nodeCount + "\n");
}
}
此示例还显示了如何使用 Buckets(在本例中为 LinkedList)来避免碰撞 调用此方法seperate chaining