JavaScript实现Java的Map、List功能,如下代码:
- function HashMap(){
- this.size=0;
- this.map=new Object();
- }
- HashMap.prototype.put=function(key,value){
- if(!this.map[key]){
- this.size++;
- }
- this.map[key]=value;
- };
- HashMap.prototype.get=function(key){
- return this.isKey(key)?this.map[key]:null;
- };
- HashMap.prototype.isKey=function(key){
- return (key in this.map);
- };
- HashMap.prototype.remove=function(key){
- if( this.isKey(key) && (delete this.map[key])){
- this.size--;
- }
- };
- HashMap.prototype.size=function(){
- return this.size;
- };
- HashMap.prototype.find=function(_callback){
- for(var _key in this.map){
- _callback.call(this,_key,this.map[_key]);
- }
- };
- //List
- function ArrayList(){
- this.size=0;
- this.list=new Object();
- };
- ArrayList.prototype.add=function(obj){
- this.list[this.size++];
- return this.size;
- };
- ArrayList.prototype.remove=function(index){
- if(this.size>index){
- delete this.list[index--];
- return this.size--;
- }
- return -1;
- };
- ArrayList.prototype.size=function(){
- return this.size;
- };
- ArrayList.prototype.get=function(index){
- return this.size>index?this.list[index]:null;
- };
- ArrayList.prototype.clear=function(){
- this.list=null;
- };
- ArrayList.prototype.contains=function(obj){
- return this.indexOf(obj)>=0?true:false;
- };
- ArrayList.prototype.indexOf=function(obj){
- for(var i=0;i<this.size;i++){
- if(this.list[i]==obj){
- return i;
- }
- }
- return -1;
- };
- ArrayList.prototype.isEmpty=function(){
- return this.size<0?true:false;
- };
- function HashSet(){
- this.size=0;
- this.set=new Object();
- };
- HashSet.prototype.add=function(obj){
- this.indexOf(obj)<0?this.set[this.size++]=obj:null;
- return this.size;
- };
- HashSet.prototype.remove=function(index){
- if(this.size>index){
- delete this.set[index--];
- return this.size--;
- }
- return -1;
- };
- HashSet.prototype.size=function(){
- return this.size;
- };
- HashSet.prototype.get=function(index){
- return this.size>index?this.set[index]:null;
- };
- HashSet.prototype.clear=function(){
- this.set=null;
- };
- HashSet.prototype.contains=function(obj){
- return this.indexOf(obj)>=0?true:false;
- };
- HashSet.prototype.indexOf=function(obj){
- for(var i=0;i<this.size;i++){
- if(this.set[i]==obj){
- return i;
- }
- }
- return -1;
- };
- HashSet.prototype.isEmpty=function(){
- return this.size<0?true:false;
- };
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索arraylist
, hashmap
, return
, function
, this
, prototype
, list map
, size
, list中筛选hashmap
javascript实现map
javascript list map、javascript 实现map、list set map底层实现、list set map实现原理、javascript list,以便于您获取更多的相关知识。
时间: 2024-11-01 13:38:46