javascript实现java的HashMap

function HashMap(){
  2    
  3    var ItemCollection=new Array();
  4    //private
  5    
  6    this.getMapCollection=function (){
  7        return ItemCollection;
  8    }

  9
 10    var MapItem=function (key,value){
 11        
 12        this.key=(key==null?"":key);        
 13        this.value=(value==null?"":value);        
 14        
 15        this.getKey=function (){
 16            return this.key;            
 17        }

 18        
 19        this.getValue=function (){
 20            return this.value;            
 21        }

 22        
 23        this.setKey=function (key){
 24            this.key=key;            
 25        }

 26        
 27        this.setValue=function (value){
 28            this.value=value;            
 29        }

 30    }

 31    
 32    this.put=function (key,value){
 33        var item=new MapItem();        
 34        if(isItemExist(key)==false)
 35        {
 36            item.setKey(key);            
 37            item.setValue(value);            
 38            ItemCollection[ItemCollection.length]=item;            
 39        }
else {
 40            item=getItem(key);            
 41            item.setValue(value);            
 42        }

 43    }

 44    
 45    this.putAll=function (itemCollection){
 46        if (HashMap.prototype.isPrototypeOf(itemCollection))
 47        {
 48            connectMap(itemCollection.getMapCollection());
 49            
 50            return true;
 51
 52        }
 else {
 53            if (Array.prototype.isPrototypeOf(itemCollection))
 54            {
 55                for (var j=0;j<itemCollection.length ;j++ )
 56                {
 57                    if (MapItem.prototype.isPrototypeOf(itemCollection[j])==false)
 58                    {
 59                        return false;
 60                    }

 61                }

 62
 63                connectMap(itemCollection);
 64                
 65                return true;
 66            }

 67        }

 68    }

 69
 70    var connectMap=function (itemsArray){
 71        if (ItemCollection.length==0)
 72        {
 73            for (var i=0;i<itemsArray.length;i++)
 74            {
 75                ItemCollection[i]=itemsArray[i];
 76            }

 77        }
 else {
 78            var j= (parseInt(itemsArray.length)+parseInt(ItemCollection.length));
 79            var orginalLength=parseInt(ItemCollection.length);                //save the length of ItemCollection before putting
 80            
 81            for (var i=parseInt(ItemCollection.length) ; i<j; i++ )
 82            {
 83                ItemCollection[i]=itemsArray[i-orginalLength];
 84            }

 85        }

 86    }

 87
 88    var getItem=function (key){
 89        var item=new MapItem();        
 90        
 91        for(var i=0;i<ItemCollection.length;i++)
 92        {
 93            if(ItemCollection[i].getKey()==key)
 94            {
 95                item=ItemCollection[i];                
 96                break;                
 97            }

 98        }

 99        return item;        
100    }

101    
102    this.get=function (key){
103        
104        var value="";        
105        
106        for(var i=0;i<ItemCollection.length;i++)
107        {
108            if(ItemCollection[i].getKey()==key)
109            {
110                value=ItemCollection[i].getValue();                
111                break;                
112            }

113        }

114        return value;        
115        
116    }

117    
118    var isItemExist=function (key){
119        
120        var item=new MapItem();        
121        item.setKey("");        
122        item.setValue("");        
123        var flag=false;        
124        
125        for(var i=0;i<ItemCollection.length;i++)
126        {
127            
128            if(ItemCollection[i].getKey()==key)
129            {
130                flag=true;                
131                break;    
132            }

133            
134        }

135        
136        return flag;        
137        
138    }
    
139
140    this.remove=function (key){
141        for (var i=0;i<ItemCollection.length;i++)
142        {
143            
144            if (ItemCollection[i].getKey()==key)
145            {
146                var mid=ItemCollection.length/2;
147
148                if (i<mid)
149                {
150                    for (var j=i;j>0 ;j-- )
151                    {
152                        ItemCollection[j]=ItemCollection[j-1];
153                    }

154                }
 else {
155                    for (var j=i;j<ItemCollection.length ;j++ )
156                    {
157                        ItemCollection[j]=ItemCollection[j+1];
158                    }

159                }
                
160                ItemCollection.length=ItemCollection.length-1;
161            }

162        }

163    }

164
165    this.removeAll=function (){
166        ItemCollection.length=0;
167    }

168
169    this.getSize=function (){
170        return ItemCollection.length;        
171    }

172
173
174    this.contain=function (key){
175        for (var i=0;i<ItemCollection.length ;i++ )
176        {
177            if (ItemCollection[i].getKey()==key)
178            {
179                return true;
180            }

181        }

182
183        return false;
184    }

185}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值