Detailed explanation of js imitating java's Map collection

       This article mainly introduces the relevant information of js imitating java's Map collection. Some of the most commonly used collection classes in Java are List and Map. Interested friends can learn about them.

 

The collection classes in java.util contain some of the most commonly used classes in Java. The most commonly used collection classes are List and Map. Specific implementations of List include ArrayList and Vector, which are variable-sized lists that are more suitable for building, storing, and manipulating lists of elements of any type of object. List is suitable for accessing elements by numerical index.
Map provides a more general way of storing elements. The Map collection class is used to store pairs of elements (called "keys" and "values"), where each key maps to a value. Conceptually, you can think of a List as a Map with numeric keys. In fact, in addition to List and Map are defined in java.util, there is no direct connection between the two. This article will focus on the Maps that come with the core Java distribution, but will also describe how to adopt or implement specialized Maps that are more suitable for your application-specific data.
After understanding the Map in java, go directly to the code!

 

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>测试map</title>
</head>
<style type="text/css">
</style>
<script type="text/javascript">
/*
 * Map object, which implements the Map function
 * size() Get the number of Map elements
 * isEmpty() to determine whether the Map is empty
 * clear() delete all elements of Map
 * put(key, value) adds an element (key, value) to the Map  
 * remove(key) removes the element of the specified key, returns true if successful, false if failed
 * get(key) Get the element value value of the specified key, if it fails, return null
 * element(index) Get the element at the specified index (use element.key, element.value to get the key and value), if it fails, return null
 * containsKey(key) Determines whether the Map contains the element with the specified key
 * containsValue(value) Determines whether the Map contains the element with the specified value
 * keys() Get the array of all keys in the Map (array)
 * values() Get the array of all values ​​in the Map (array)
 *
 */
function Map(){
  this.elements = new Array();
   
  //Get the number of Map elements
  this.size = function() {
    return this.elements.length;
  },
   
  //Check if Map is empty
  this.isEmpty = function() {
    return (this.elements.length < 1);
  },
   
  // delete all elements of the map
  this.clear = function() {
    this.elements = new Array();
  },
   
  //Add elements (key, value) to the Map  
  this.put = function(_key, _value) {
    if (this.containsKey(_key) == true) {
      if(this.containsValue(_value)){
        if(this.remove(_key) == true){
          this.elements.push( {
            key : _key,
            value : _value
          });
        }
      }else{
        this.elements.push( {
          key : _key,
          value : _value
        });
      }
    } else {
      this.elements.push( {
        key : _key,
        value : _value
      });
    }
  },
   
  //Delete the element of the specified key, return true on success, false on failure
  this.remove = function(_key) {
    var bln = false;
    try {  
      for (i = 0; i < this.elements.length; i++) {  
        if (this.elements[i].key == _key){
          this.elements.splice(i, 1);
          return true;
        }
      }
    }catch(e){
      bln = false;  
    }
    return bln;
  },
   
  //Get the element value value of the specified key, if it fails, return null
  this.get = function(_key) {
    try{  
      for (i = 0; i < this.elements.length; i++) {
        if (this.elements[i].key == _key) {
          return this.elements[i].value;
        }
      }
    }catch(e) {
      return null;  
    }
  },
   
  //Get the element of the specified index (use element.key, element.value to get the key and value), if it fails, return null
  this.element = function(_index) {
    if (_index < 0 || _index >= this.elements.length){
      return null;
    }
    return this.elements[_index];
  },
   
  //Determine whether the Map contains the element with the specified key
  this.containsKey = function(_key) {
    var bln = false;
    try {
      for (i = 0; i < this.elements.length; i++) {  
        if (this.elements[i].key == _key){
          bln = true;
        }
      }
    }catch(e) {
      bln = false;  
    }
    return bln;
  },
    
  / / Determine whether the Map contains the element with the specified value
  this.containsValue = function(_value) {
    var bln = false;
    try {
      for (i = 0; i < this.elements.length; i++) {  
        if (this.elements[i].value == _value){
          bln = true;
        }
      }
    }catch(e) {
      bln = false;  
    }
    return bln;
  },
   
  //Get the array of all keys in the Map (array)
  this.keys = function() {
    var arr = new Array();
    for (i = 0; i < this.elements.length; i++) {  
      arr.push(this.elements[i].key);
    }
    return arr;
  },
  
  //Get the array of all values ​​in the Map (array)
  this.values = function() {
    var arr = new Array();
    for (i = 0; i < this.elements.length; i++) {  
      arr.push(this.elements[i].value);
    }
    return arr;
  };
}
//test map
alert('test map');
var map = new Map ();
map.put(0,0);
map.put(1,1);
map.put(2,2);
alert('The size of the map is: '+map.size());
for(var i=0;i<map.size();i++){
  alert('map's key'+i+' corresponds to the value of'+map.get(i));
}
alert('Get the key that does not exist in the map'+map.get('Get the key that does not exist in the map'));
alert('The length of all keys in the map'+map.keys().length);
for(var i=0;i<map.keys().lenght;i++){
  alert('key value in map'+map.keys()[i]);
}
alert('The length of all value values ​​in the map'+map.values().length);
for(var i=0;i<map.values().length;i++){
  alert('value of value in map'+map.values()[i]);
}
alert('Determine whether the value in the map exists 3'+map.containsValue(3));
</script>
<body>
test map
</body>
</html>

 

 

Running renderings:

 



Source: http://www.jb51.net/article/77567.htm

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326483760&siteId=291194637