A Simple Ordered Hashtable
来源:技术人生 责任编辑:栏目编辑 发表时间:2013-07-02 04:58 点击:次
This article illustrates how to implement an ordered hashtable, which maps keys to values. Any non-null object can be used as a key or as a value.
As with typical Hashtables, to successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method.
There are many instances where you would like to use an ordered Hashtable, for example, to keep your user interface elements ordered, or to keep ordered items from a database or backend while keeping rapid access via Hashtable keys, or to store and access any value you want to access using a key.
This ordered Hashtable is called simple because internally it uses the Legacy collection classes, a Vector to maintain the element's order and a Hashtable to provide hashing capabilities. Because Hashtable and Vector grow differently, the implementation of
[edit]
A Hashtable implementation
[edit]
A Hashtable implementation
SimpleOrderedHashtable is not the most efficient one, but may be good enough for your needs.
/* -----------------------------------------------------------------------------
* SimpleOrderedHashtable.java
* Author: C. Enrique Ortiz
* Copyright (c) 2004-2009 C. Enrique Ortiz <eortiz@j2medeveloper.com>
*
* SimpleOrderedHashtable.java implements a simple Hashtable that is
* chronologically ordered.
*
* This 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 of the License, or (at your option) any
* later version.
*
* Usage & redistributions of source code must retain the above copyright notice.
*
* This software 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 get a copy of the GNU Lesser General Public License from
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
* -----------------------------------------------------------------------------
*/
import java.util.Vector;
import java.util.Enumeration;
import java.util.Hashtable;
/**
* Implements an Ordered Hashtable, with elements in
* chronological order (i.e. insertion order)
*/
public class SimpleOrderedHashtable {
private Vector orderedKeys;
private Hashtable hashTable;
/**
* Constructor, creates an SimpleOrderedHashtable.
*/
public SimpleOrderedHashtable() {
orderedKeys = new Vector();
hashTable = new Hashtable();
}
/**
* Constructor, creates an SimpleOrderedHashtable.
* @param initialCapacity is the initial size for the container.
*/
public SimpleOrderedHashtable(int initialCapacity) {
orderedKeys = new Vector(initialCapacity);
hashTable = new Hashtable(initialCapacity);
}
/**
* Maps the specified key to the specified value in this SimpleOrderedHashtable.
* The value can be retrieved by calling the get method with a key that is
* equal to the original key.
* @
* SimpleOrderedHashtable.java
* Author: C. Enrique Ortiz
* Copyright (c) 2004-2009 C. Enrique Ortiz <eortiz@j2medeveloper.com>
*
* SimpleOrderedHashtable.java implements a simple Hashtable that is
* chronologically ordered.
*
* This 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 of the License, or (at your option) any
* later version.
*
* Usage & redistributions of source code must retain the above copyright notice.
*
* This software 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 get a copy of the GNU Lesser General Public License from
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
* -----------------------------------------------------------------------------
*/
import java.util.Vector;
import java.util.Enumeration;
import java.util.Hashtable;
/**
* Implements an Ordered Hashtable, with elements in
* chronological order (i.e. insertion order)
*/
public class SimpleOrderedHashtable {
private Vector orderedKeys;
private Hashtable hashTable;
/**
* Constructor, creates an SimpleOrderedHashtable.
*/
public SimpleOrderedHashtable() {
orderedKeys = new Vector();
hashTable = new Hashtable();
}
/**
* Constructor, creates an SimpleOrderedHashtable.
* @param initialCapacity is the initial size for the container.
*/
public SimpleOrderedHashtable(int initialCapacity) {
orderedKeys = new Vector(initialCapacity);
hashTable = new Hashtable(initialCapacity);
}
/**
* Maps the specified key to the specified value in this SimpleOrderedHashtable.
* The value can be retrieved by calling the get method with a key that is
* equal to the original key.
* @
相关新闻>>
- 发表评论
-
- 最新评论 更多>>