java-如何创建自定义哈希表迭代器?
我目前正在尝试实现一个哈希表集合--我已经准备好了所有的东西并在运行,但是当我试图为该表定义一个自定义迭代器时,我遇到了一个概念上的问题。我有一个名为“HashEntry”的内部类,它是数组中存储的实际对象——它们存储项的键、值和状态,即空、活动和已删除java-如何创建自定义哈希表迭代器?,java,iterator,hashmap,hashtable,inner-classes,Java,Iterator,Hashmap,Hashtable,Inner Classes,我目前正在尝试实现一个哈希表集合--我已经准备好了所有的东西并在运行,但是当我试图为该表定义一个自定义迭代器时,我遇到了一个概念上的问题。我有一个名为“HashEntry”的内部类,它是数组中存储的实际对象——它们存储项的键、值和状态,即空、活动和已删除 private class HashEntry { private TKey m_key; private TValue m_value; private EntryStatus status; //stand
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对象,还是按照hashtable惯例遍历表中存储的实际值?HashEntry类是私有的,所以我假设返回它的实例是不好的做法
但是如果是这样的话,我如何创建一个哈希表迭代器来遍历它的HashEntrys对象呢?我必须在HashEntry类中定义一个迭代器/iterable吗?一般来说,是的,如果您确实提供了一个迭代器来迭代HashEntrys,那么用户在迭代时会同时获得键和值(以及状态),这可能会更好。通常,如果没有键,值就没有意义,反之亦然 为什么不把
HashEntry
classapublic static
generic内部类,把实现特定的东西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();
}
}
}
(来源:)
希望这有点帮助。标准hashmap实现不保留插入顺序,您确定您的算法保留插入顺序吗?我并不真正关心插入顺序(算法并没有保留这一点——迭代器实际上只用于内部使用,这样我就可以编写基本上干净地删除表的方法,以及在任何其他方法中进行迭代。至少要使
HashEntry
泛型,如HashEntry
。非常好,谢谢@dudeprgm,我没有想到要制作HashEn尝试使用公共类,尽管我可能需要提供一些关于如何使用它的注释,现在它可以了。谢谢大家,这很有帮助。
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;
}