Java 自定义HashMap代码问题的实现
我正在用Java准备自己的自定义HashMap实现。下面是我的建议Java 自定义HashMap代码问题的实现,java,hashmap,Java,Hashmap,我正在用Java准备自己的自定义HashMap实现。下面是我的建议 public class Entry<K,V> { private final K key; private V value; private Entry<K,V> next; public Entry(K key, V value, Entry<K,V> next) { this.key = key; this.value = value; this.nex
public class Entry<K,V> {
private final K key;
private V value;
private Entry<K,V> next;
public Entry(K key, V value, Entry<K,V> next) {
this.key = key;
this.value = value;
this.next = next;
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
public Entry<K, V> getNext() {
return next;
}
public void setNext(Entry<K, V> next) {
this.next = next;
}
public K getKey() {
return key;
}
}
public class MyCustomHashMap<K,V> {
private int DEFAULT_BUCKET_COUNT = 10;
private Entry<K,V>[] buckets;
public MyCustomHashMap() {
buckets = new Entry[DEFAULT_BUCKET_COUNT];
for (int i = 0;i<DEFAULT_BUCKET_COUNT;i++)
buckets[i] = null;
}
public void put(K key,V value){
/**
* This is the new node.
*/
Entry<K,V> newEntry = new Entry<K,V>(key, value, null);
/**
* If key is null, then null keys always map to hash 0, thus index 0
*/
if(key == null){
buckets[0] = newEntry;
}
/**
* get the hashCode of the key.
*/
int hash = hash(key);
/**
* if the index does of the bucket does not contain any element then assign the node to the index.
*/
if(buckets[hash] == null) {
buckets[hash] = newEntry;
} else {
/**
* we need to traverse the list and compare the key with each of the keys till the keys match OR if the keys does not match then we need
* to add the node at the end of the linked list.
*/
Entry<K,V> previous = null;
Entry<K,V> current = buckets[hash];
while(current != null) {
boolean done = false;
while(!done) {
if(current.getKey().equals(key)) {
current.setValue(value);
done = true; // if the keys are same then replace the old value with the new value;
} else if (current.getNext() == null) {
current.setNext(newEntry);
done = true;
}
current = current.getNext();
previous = current;
}
}
previous.setNext(newEntry);
}
}
public V getKey(K key) {
int hash = hash(key);
if(buckets[hash] == null) {
return null;
} else {
Entry<K,V> temp = buckets[hash];
while(temp != null) {
if(temp.getKey().equals(key))
return temp.getValue(); // returns value corresponding to key.
temp = temp.getNext();
}
return null; //return null if key is not found.
}
}
public void display() {
for(int i = 0; i < DEFAULT_BUCKET_COUNT; i++) {
if(buckets[i] != null) {
Entry<K,V> entry = buckets[i];
while(entry != null){
System.out.print("{"+entry.getKey()+"="+entry.getValue()+"}" +" ");
entry=entry.getNext();
}
}
}
}
public int bucketIndexForKey(K key) {
int bucketIndex = key.hashCode() % buckets.length;
return bucketIndex;
}
/**
*
* @param key
* @return
*/
private int hash(K key){
return Math.abs(key.hashCode()) % buckets.length;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
MyCustomHashMap<String, Integer> myCustomHashMap = new MyCustomHashMap<String, Integer>();
myCustomHashMap.put("S", 22);
myCustomHashMap.put("S", 1979);
myCustomHashMap.put("V", 5);
myCustomHashMap.put("R", 31);
System.out.println("Value corresponding to key R: "+myCustomHashMap.getKey("R"));
System.out.println("Value corresponding to key V: "+myCustomHashMap.getKey("V"));
System.out.println("Displaying the contents of the HashMap:: ");
myCustomHashMap.display();
}
}
公共类条目{
私钥;
私人价值;
下一步是私人进入;
公共输入(K键、V值、下一个输入){
this.key=key;
这个值=值;
this.next=next;
}
public V getValue(){
返回值;
}
公共无效设置值(V值){
这个值=值;
}
公共条目getNext(){
下一步返回;
}
公共作废设置下一步(条目下一步){
this.next=next;
}
公共K getKey(){
返回键;
}
}
公共类MyCustomHashMap{
private int DEFAULT_BUCKET_COUNT=10;
私人进入【】桶;
公共MyCustomHashMap(){
BUCKET=新条目[默认的BUCKET\u计数];
对于(int i=0;i
您错误地处理空密钥:
if(key == null){
buckets[0] = newEntry;
}
bucket[0]
可能已经包含条目,在这种情况下,您将丢失这些条目
以下循环存在一些问题:
Entry<K,V> previous = null;
Entry<K,V> current = buckets[hash];
while(current != null) {
boolean done = false;
while(!done) {
if(current.getKey().equals(key)) {
current.setValue(value);
done = true;
} else if (current.getNext() == null) {
current.setNext(newEntry);
done = true;
}
current = current.getNext();
previous = current; // you are not really setting previous to
// to the previous Entry in the list - you
// are setting it to the current Entry
}
}
previous.setNext(newEntry); // you don't need this statement. You
// already have a statement inside the
// loop that adds the new Entry to the list
然后将其用于:
int hash = hash(key.hashCode());
int bucket = hash % buckets.length;
此HashMap中还有一个概念上的缺陷。基本上,它只对非常少的项目有用,因为它不支持调整存储桶的大小。因此,对于大量项目,它的行为实际上就像linkedlist一样。@kolakao这是true@Eran: 1)您错误地处理了null。但是它不熟悉HashMap的定义吗?“HashMap允许一个null键和任意数量的null值”。此外,“如果键为null,则null键始终映射到哈希0,因此索引为0”。这段代码是:If(key==null){bucket[0]=newEntry;}替换已存在的值?以及如何调整hashMap的大小。.plesae do guide我对此不太清楚。@sid将空键映射到索引0没有问题,但请记住,其他键也可能映射到索引0,因此不能只分配bucket[0]=newEntry;
与您当前的操作相同,因为您的映射中可能已经有其他条目也映射到了索引0。@关于重新调整大小的sid-您可以创建一个更大的存储桶
数组(例如,两倍于当前数组的大小)然后将旧bucket
数组的所有条目添加到该数组中。请注意,这些条目可能映射到新数组中的不同bucket,因此您必须重新计算每个条目的索引。请验证这不是调试服务。您测试过它吗?它是否按预期工作?如果没有,与ex有什么不同佩特德?
int hash = hash(key.hashCode());
int bucket = hash % buckets.length;