Saltar al contenido

Hashtable JavaScript

Esta es una implementaci贸n de lo que ser铆a una hashtable de java, pero para javascript, puede resultar muy 煤til a la hora de manejar colecciones de datos en javascript.

Yo la he utilizado frecuentemente para almacenar objetos javascript y realmente funciona muy bien, su creador es Michael Synovic, se puede obtener tambi茅n de su web (creo que es la oficial)

http://work.synovic.net/hashtable/hashtable.html

En esta p谩gina adem谩s del c贸digo fuente, existe una herramienta que te muestra el funcionamiento de la librer铆a.

FUNCIONALIDADES:

Contructor(s):

Hashtable()

Creates a new, empty hashtable聽聽聽聽聽聽聽聽聽

Method(s):

void clear()

Clears this hashtable so that it contains no keys.

boolean containsKey(String key)

Tests if the specified object is a key in this hashtable.

boolean containsValue(Object value)

Returns true if this Hashtable maps one or more keys to this value.

Object get(String key)

Returns the value to which the specified key is mapped in this hashtable.

boolean isEmpty()

Tests if this hashtable maps no keys to values.

Array keys()

Returns an array of the keys in this hashtable.

void put(String key, Object value)

Maps the specified key to the specified value in this hashtable. A NullPointerException is thrown is the key or value is null.

Object remove(String key)

Removes the key (and its corresponding value) from this hashtable. Returns the value of the key that was removed

int size()

Returns the number of keys in this hashtable.

String toString()

Returns a string representation of this Hashtable object in the form of a set of entries, enclosed in braces and separated by the ASCII characters 鈥, 鈥 (comma and space).

Array values()

Returns a array view of the values contained in this Hashtable.

Array entrySet()

Returns a reference to the internal array that stores the data. The Set is backed by the Hashtable, so changes to the Hashtable are reflected in the Set, and vice-versa.

/**
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Created by: Michael Synovic
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 on: 01/12/2003
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 This is a Javascript implementation of the Java Hashtable object.
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Copyright (C) 2003聽 Michael Synovic聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 This library is free software; you can redistribute it and/or
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 modify it under the terms of the GNU Lesser General Public
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 License as published by the Free Software Foundation; either
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 version 2.1 of the License, or (at your option) any later version.

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 This library is distributed in the hope that it will be useful,
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 but WITHOUT ANY WARRANTY; without even the implied warranty of
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.聽 See the GNU
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Lesser General Public License for more details.

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 You should have received a copy of the GNU Lesser General Public
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 License along with this library; if not, write to the Free Software
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA聽 02110-1301聽 USA聽

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Contructor(s):
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Hashtable()
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Creates a new, empty hashtable
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Method(s):
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 void clear()
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Clears this hashtable so that it contains no keys.
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 boolean containsKey(String key)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Tests if the specified object is a key in this hashtable.
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 boolean containsValue(Object value)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Returns true if this Hashtable maps one or more keys to this value.
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Object get(String key)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Returns the value to which the specified key is mapped in this hashtable.
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 boolean isEmpty()
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Tests if this hashtable maps no keys to values.
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Array keys()
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Returns an array of the keys in this hashtable.
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 void put(String key, Object value)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Maps the specified key to the specified value in this hashtable. A NullPointerException is thrown is the key or value is null.
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Object remove(String key)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Removes the key (and its corresponding value) from this hashtable. Returns the value of the key that was removed
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 int size()
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Returns the number of keys in this hashtable.
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 String toString()
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Returns a string representation of this Hashtable object in the form of a set of entries, enclosed in braces and separated by the ASCII characters 鈥, 鈥 (comma and space).
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Array values()
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Returns a array view of the values contained in this Hashtable.
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Array entrySet()
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Returns a reference to the internal array that stores the data. The Set is backed by the Hashtable, so changes to the Hashtable are reflected in the Set, and vice-versa.
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 */
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 function Hashtable(){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 this.hashtable = new Array();
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 /* privileged functions */
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Hashtable.prototype.clear = function(){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 this.hashtable = new Array();
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 }聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Hashtable.prototype.containsKey = function(key){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 var exists = false;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 for (var i in this.hashtable) {
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 if (i == key && this.hashtable[i] != null) {
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 exists = true;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 break;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 }聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 return exists;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Hashtable.prototype.containsValue = function(value){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 var contains = false;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 if (value != null) {
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 for (var i in this.hashtable) {
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 if (this.hashtable[i] == value) {
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 contains = true;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 break;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 }聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 return contains;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Hashtable.prototype.get = function(key){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 return this.hashtable[key];
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Hashtable.prototype.isEmpty = function(){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 return (parseInt(this.size()) == 0) ? true : false;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Hashtable.prototype.keys = function(){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 var keys = new Array();
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 for (var i in this.hashtable) {
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 if (this.hashtable[i] != null)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 keys.push(i);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 return keys;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Hashtable.prototype.put = function(key, value){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 if (key == null || value == null) {
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 throw 鈥淣ullPointerException {鈥 + key + 鈥渳,{鈥 + value + 鈥渳鈥;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 }else{
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 this.hashtable[key] = value;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Hashtable.prototype.remove = function(key){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 var rtn = this.hashtable[key];
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 this.hashtable[key] = null;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 return rtn;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 }聽聽聽
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Hashtable.prototype.size = function(){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 var size = 0;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 for (var i in this.hashtable) {
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 if (this.hashtable[i] != null)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 size ++;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 return size;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Hashtable.prototype.toString = function(){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 var result = 鈥溾;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 for (var i in this.hashtable)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 {聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 if (this.hashtable[i] != null)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 result += 鈥渰鈥 + i + 鈥渳,{鈥 + this.hashtable[i] + 鈥渳\n鈥;聽
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 return result;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 }聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Hashtable.prototype.values = function(){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 var values = new Array();
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 for (var i in this.hashtable) {
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 if (this.hashtable[i] != null)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 values.push(this.hashtable[i]);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 return values;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 }聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Hashtable.prototype.entrySet = function(){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 return this.hashtable;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 }