我目前正在尝试实现Hashtable集合-我已启动并运行了所有程序,但是在尝试为表定义自定义迭代器时遇到了一个概念性问题。我有一个名为``HashEntry''的内部类,它们是存储在数组中的实际对象-它们存储键,值和条目的状态,即空, Activity ,已删除。
private class HashEntry
{
private TKey m_key;
private TValue m_value;
private EntryStatus status;
//standard constructor
public HashEntry(TKey key, TValue value)
{
m_key = key;
m_value = value;
status = EntryStatus.ACTIVE;
}
public HashEntry(TKey key, TValue value, EntryStatus i) {
m_key = key;
m_value = value;
status = i;
}
//default 'empty' constructor
public HashEntry()
{
//calls default constructor, creates placeholder entry
m_key = null;
m_value = null;
status = EntryStatus.EMPTY;
}
//equals operator override, this override just compares compares
// the objects held in the entry, so any object used with this
// implementation must hae=ve its own equals override
@Override
public boolean equals(Object obj)
{
if (obj == null) { return false; }
if (getClass() != obj.getClass()) { return false; }
final HashEntry other = (HashEntry) obj;
return (!((this.m_key == null) ? (other.m_key != null) : !this.m_key.equals(other.m_key)));
}
// override of the hashCode() function--just calls the hashCode
// function of the embedded object, so that must be provided
@Override
public int hashCode()
{
return this.m_key.hashCode();
}
// toString override just returns the toString of the embedded object
@Override
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append(m_key.toString()).append(m_value.toString());
return sb.toString();
}
}
这是我的问题的第一部分-如果我希望能够遍历表,是否应该遍历(并因此返回)HashEntry对象,还是遍历表中存储的实际值的哈希表约定? ? HashEntry类是私有(private)的,因此我假设它的错误做法是返回它的实例...
但是,如果是这样,我如何创建一个遍历其HashEntrys对象的Hashtable迭代器?我必须在HashEntry类中定义一个迭代器/可迭代器吗?
最佳答案
一般而言,是的,如果您确实提供了一个在HashEntry
上进行迭代的迭代器,则可能会更好,这样用户在进行迭代时会同时获得键和值(以及状态)。通常,没有键就没有意义,反之亦然。
您为什么不只将HashEntry
类作为public static
通用内部类,而将实现特定的东西设为private
呢?您可能还需要使HashEntry
通用,因为我假设您的父类(我们称其为MyHashTable
)也是基于TKey
和TValue
的通用类。
所以,如果我是你,我会让你的HashEntry
和MyHashTable
看起来像这样:
// Note: implements Iterable<E> now
public class MyHashTable<TKey, TValue> implements Iterable<MyHashTable.HashEntry<TKey, TValue>>
{
public Iterator<MyHashTable.HashEntry<TKey, TValue>> iterator() {
// ...
// Make and return your iterator here
// ...
}
// Note: public and generic now
public static class HashEntry<TKey, TValue>
{
private TKey m_key;
private TValue m_value;
private EntryStatus status;
//standard constructor
// Note: private now
public HashEntry(TKey key, TValue value)
{
m_key = key;
m_value = value;
status = EntryStatus.ACTIVE;
}
// Note: private now
private HashEntry(TKey key, TValue value, EntryStatus i) {
m_key = key;
m_value = value;
status = i;
}
//default 'empty' constructor
// Note: private now
public HashEntry()
{
//calls default constructor, creates placeholder entry
m_key = null;
m_value = null;
status = EntryStatus.EMPTY;
}
public TKey getKey() {
return m_key;
}
public TValue getValue() {
return m_value;
}
public EntryStatus getEntryStatus() {
return status;
}
//equals operator override, this override just compares compares
// the objects held in the entry, so any object used with this
// implementation must hae=ve its own equals override
@Override
public boolean equals(Object obj)
{
if (obj == null) { return false; }
if (getClass() != obj.getClass()) { return false; }
final HashEntry other = (HashEntry) obj;
return (!((this.m_key == null) ? (other.m_key != null) : !this.m_key.equals(other.m_key)));
}
// override of the hashCode() function--just calls the hashCode
// function of the embedded object, so that must be provided
@Override
public int hashCode()
{
return this.m_key.hashCode();
}
// toString override just returns the toString of the embedded object
@Override
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append(m_key.toString()).append(m_value.toString());
return sb.toString();
}
}
}
请注意,
HashEntry
现在是MyHashTable
的内部类,它是通用的,并且其构造函数现在是private
。这保证了除外部类MyHashTable
之外的任何人都无法实例化HashEntry
,因为实例化哈希表之外的任何一个都没有意义(请参阅this)。但是,其他人可以通过getter访问条目的键和值。迭代器本身将是
Iterator<MyHashTable.HashEntry<TKey, TValue>>
的实例。至于编写一个,这取决于您自己的哈希表实现,但是基本上,您需要一种以任意顺序获取下一个元素的方法: Iterator<E>.next()
。例如,下面是一个
iterator()
方法的实现,它在一个简单的数组上进行迭代:private Type[] arrayList;
private int currentSize;
@Override
public Iterator<Type> iterator() {
Iterator<Type> it = new Iterator<Type>() {
private int currentIndex = 0;
@Override
public boolean hasNext() {
return currentIndex < currentSize && arrayList[currentIndex] != null;
}
@Override
public Type next() {
return arrayList[currentIndex++];
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
return it;
}
(来源:https://stackoverflow.com/a/5849625/837703)
希望这能有所帮助。