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 and are more suitable for constructing, 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 method 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, except that List and Map are both defined in java.util, there is no direct connection between the two. This article will focus on the Maps included in the core Java distribution, but will also describe how to adopt or implement a specialized Map that is more suitable for your application's specific data.
After understanding Map in Java, it’s time to start coding!
<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对象,实现Map功能 * size() 获取Map元素个数 * isEmpty() 判断Map是否为空 * clear() 删除Map所有元素 * put(key, value) 向Map中增加元素(key, value) * remove(key) 删除指定key的元素,成功返回true,失败返回false * get(key) 获取指定key的元素值value,失败返回null * element(index) 获取指定索引的元素(使用element.key,element.value获取key和value),失败返回null * containsKey(key) 判断Map中是否含有指定key的元素 * containsValue(value) 判断Map中是否含有指定value的元素 * keys() 获取Map中所有key的数组(array) * values() 获取Map中所有value的数组(array) * */ function Map(){ this.elements = new Array(); //获取Map元素个数 this.size = function() { return this.elements.length; }, //判断Map是否为空 this.isEmpty = function() { return (this.elements.length < 1); }, //删除Map所有元素 this.clear = function() { this.elements = new Array(); }, //向Map中增加元素(key, value) 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 }); } }, //删除指定key的元素,成功返回true,失败返回false 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; }, //获取指定key的元素值value,失败返回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; } }, //获取指定索引的元素(使用element.key,element.value获取key和value),失败返回null this.element = function(_index) { if (_index < 0 || _index >= this.elements.length){ return null; } return this.elements[_index]; }, //判断Map中是否含有指定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; }, //判断Map中是否含有指定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; }, //获取Map中所有key的数组(array) this.keys = function() { var arr = new Array(); for (i = 0; i < this.elements.length; i++) { arr.push(this.elements[i].key); } return arr; }, //获取Map中所有value的数组(array) this.values = function() { var arr = new Array(); for (i = 0; i < this.elements.length; i++) { arr.push(this.elements[i].value); } return arr; }; } //测试map alert('测试map'); var map=new Map(); map.put(0,0); map.put(1,1); map.put(2,2); alert('map的大小为:'+map.size()); for(var i=0;i<map.size();i++){ alert('map的key'+i+'对应的value值为'+map.get(i)); } alert('获取map中不存在的键'+map.get('获取map中不存在的键')); alert('map中的所有键的长度'+map.keys().length); for(var i=0;i<map.keys().lenght;i++){ alert('map中的键值'+map.keys()[i]); } alert('map中的所有的value值的长度'+map.values().length); for(var i=0;i<map.values().length;i++){ alert('map中的value的值'+map.values()[i]); } alert('判断map中的值value是否存在3'+map.containsValue(3)); </script> <body> 测试map </body> </html>
Operation rendering:
The above is the entire content of this article, I hope it will be helpful to everyone’s study.