admin管理员组文章数量:1636895
Implement Hashmap in Java.
原理: Hashmap就是array of linkedlist,就是利用hash算法,算index之后,然后冲突了,就看后面有没有 Key Value pair 相等,如果相同,则覆盖,如果没有,则加在bucket的beginning。 源代码: http://developer.classpath/doc/java/util/HashMap-source.html
public class ImplementHashMap {
class Node{
String key;
Integer val;
Node next;
public Node(String key, Integer val){
this.key = key;
this.val = val;
}
}
private int size = 3;
private Node[] array;
public ImplementHashMap() {
array = new Node[size];
}
public void put(String key, Integer value){
int index = key.hashCode()%this.size;
System.out.println("index is:"+index);
Node node = array[index];
while(node!=null) {
if(node.key.equals(key)){
node.val = value;
return;
} else {
node = node.next;
}
}
// add new entry to the beginning of the bucket;
Node newnode = new Node(key,value);
newnode.next = array[index];
array[index] = newnode;
return;
}
public Integer get(String key){
int index = key.hashCode()%this.size;
Node node = array[index];
while(node!=null){
if(node.key.equals(key)){
return node.val;
} else {
node = node.next;
}
}
return null;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ImplementHashMap hashmap = new ImplementHashMap();
hashmap.put("a", 2);
System.out.println(hashmap.get("a"));
hashmap.put("b", 5);
System.out.println(hashmap.get("b"));
hashmap.put("c", 7);
System.out.println(hashmap.get("c"));
hashmap.put("d", 8);
System.out.println(hashmap.get("d"));
hashmap.put("e", 9);
System.out.println(hashmap.get("e"));
hashmap.put("f", 10);
System.out.println(hashmap.get("f"));
}
}
index is:1
2
index is:2
5
index is:0
7
index is:1
8
index is:2
9
index is:0
10
版权声明:本文标题:Implement HashMap in Java 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1729234296a1191815.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论