首页 > 其他 > 详细

implement HashMap

时间:2014-04-10 07:54:21      阅读:457      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
 1 public class HashMap {
 2     private static final int SIZE = 16;
 3     private Entry table [] = new Entry [SIZE] ;
 4     class Entry{
 5         private final String key;
 6         String value;
 7         Entry next;
 8         Entry(String k,String v){
 9             key = k;
10             value = v;
11         }
12         public String getValue() {
13             return value;
14         }
15         public void setValue(String value) {
16             this.value = value;
17         }
18         public String getKey() {
19             return key;
20         }
21     }
22     public Entry get(String key){
23         int hash = key.hashCode()&(SIZE-1);
24         Entry e = table[hash];
25         while(e!=null){
26             if(e.getKey().equals(key)){
27                 return e;
28             }
29             e=e.next;
30         }
31         return null;
32     }
33     public void put(String key,String value){
34         int hash = key.hashCode()&(SIZE-1);
35         Entry e = table[hash];
36         if(e !=null){
37             if(e.getKey().equals(key)){
38                 e.setValue(value);
39             }
40             else{
41                 while(e.next!=null){
42                     e=e.next;
43                 }
44                 e.next = new Entry(key,value);
45             }
46         }else{
47             table[hash] = new Entry(key,value);
48         }
49     }
50 }
View Code

 

implement HashMap,布布扣,bubuko.com

implement HashMap

原文:http://www.cnblogs.com/krunning/p/3655591.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!