• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

JavaScript实现Java的Map、List功能_js

javascript 搞代码 7年前 (2018-06-13) 159次浏览 已收录 0个评论

JavaScript实现java的Map、List功能,如下代码:

  1. function HashMap(){ 
  2.     this.size=0; 
  3.     this.map=new Object(); 
  4.  
  5. HashMap.prototype.put=function(key,value){ 
  6.     if(!this.map[key]){ 
  7.         this.size++; 
  8.     } 
  9.     this.map[key]=value; 
  10. }; 
  11. HashMap.prototype.get=function(key){ 
  12.     return this.isKey(key)?this.map[key]:null
  13. }; 
  14. HashMap.prototype.isKey=function(key){ 
  15.     return (key in this.map); 
  16. }; 
  17. HashMap.prototype.remove=function(key){ 
  18.   ifthis.isKey(key) && (delete this.map[key])){   
  19.         this.size--;   
  20.   }   
  21. }; 
  22.  
  23. HashMap.prototype.size=function(){ 
  24.     return this.size; 
  25. }; 
  26.  
  27. HashMap.prototype.find=function(_callback){ 
  28.     for(var _key in this.map){ 
  29.         _callback.call(this,_key,this.map[_key]); 
  30.     } 
  31. }; 
  32. //List 
  33.  
  34. function ArrayList(){ 
  35.     this.size=0; 
  36.     this.list=new Object(); 
  37. }; 
  38. ArrayList.prototype.add=function(obj){ 
  39.     this.list[this.size++]; 
  40.     return this.size; 
  41. }; 
  42. ArrayList.prototype.remove=function(index){ 
  43.     if(this.size>index){ 
  44.         delete this.list[index--]; 
  45.         return this.size--; 
  46.     } 
  47.     return -1; 
  48. }; 
  49. ArrayList.prototype.size=function(){ 
  50.     return this.size; 
  51. }; 
  52. ArrayList.prototype.get=function(index){ 
  53.     return this.size>index?this.list[index]:null
  54. }; 
  55. ArrayList.prototype.clear=function(){ 
  56.     this.list=null
  57. }; 
  58. ArrayList.prototype.contains=function(obj){ 
  59.     return this.indexOf(obj)>=0?true:false
  60. }; 
  61. ArrayList.prototype.indexOf=function(obj){ 
  62.     for(var i=0;i<this.size;i++){ 
  63.         if(this.list[i]==obj){ 
  64.             return i; 
  65.         } 
  66.     } 
  67.     return -1; 
  68. }; 
  69. ArrayList.prototype.isEmpty=function(){ 
  70.     return this.size<0?true:false
  71. }; 
  72.  
  73.  
  74. function HashSet(){ 
  75.     this.size=0; 
  76.     this.set=new Object(); 
  77. }; 
  78. HashSet.prototype.add=function(obj){ 
  79.     this.indexOf(obj)<0?this.set[this.size++]=obj:null
  80.     return this.size; 
  81. }; 
  82. HashSet.prototype.remove=function(index){ 
  83.     if(this.size>index){ 
  84.         delete this.set[index--]; 
  85.         return this.size--; 
  86.     } 
  87.     return -1; 
  88. }; 
  89. HashSet.prototype.size=function(){ 
  90.     return this.size; 
  91. }; 
  92. HashSet.prototype.get=function(index){ 
  93.     return this.size>index?this.set[index]:null
  94. }; 
  95. HashSet.prototype.clear=function(){ 
  96.     this.set=null
  97. }; 
  98. HashSet.prototype.contains=function(obj){ 
  99.     return this.indexOf(obj)>=0?true:false
  100. }; 
  101. HashSet.prototype.indexOf=function(obj){ 
  102.     for(var i=0;i<this.size;i++){ 
  103.         if(this.set[i]==obj){ 
  104.             return i; 
  105.         } 
  106.     } 
  107.     return -1; 
  108. }; 
  109. HashSet.prototype.isEmpty=function(){ 
  110.     return this.size<0?true:false
  111. }; 

欢迎大家阅读《JavaScript实现Java的Map、List功能_js,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:JavaScript实现Java的Map、List功能_js
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址