java 创建哈希表_java – 如何创建自定义哈希表迭代器?

我目前正在尝试实现一个Hashtable集合 – 我已经完成了所有工作,但是当我尝试为表定义一个自定义迭代器时,我遇到了一个概念问题.我有一个名为’HashEntry’的内部类,它是存储在数组中的实际对象 – 它们存储条目的键,值和状态,即空,活动,已删除.

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类是私有的,所以我假设它的错误做法是返回它的实例……

但如果是这样,我如何创建一个迭代HashEntrys对象的Hashtable迭代器?我是否必须在HashEntry类中定义迭代器/ iterable?

最佳答案 一般来说,是的,如果您提供迭代HashEntrys的迭代器可能会更好,因此用户在迭代时同时获得键和值(和状态).通常,没有密钥,价值就没有意义,反之亦然.

为什么不将HashEntry类作为公共静态泛型内部类,并使特定于实现的东西变为私有?您可能还需要使HashEntry成为通用的,因为我假设您的父类(我们只是称之为MyHashTable)也是基于TKey和TValue的通用.

所以,如果我是你,我会让你的HashEntry和MyHashTable看起来更像这样:

// Note: implements Iterable now

public class MyHashTable implements Iterable>

{

public Iterator> iterator() {

// ...

// Make and return your iterator here

// ...

}

// Note: public and generic now

public static class HashEntry

{

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的内部类,它是通用的,它的构造函数现在是私有的.这保证除了外部类MyHashTable之外的任何人都可以实例化HashEntry,因为在哈希表外部实例化一个是没有意义的(参见this).但是,其他人可以通过getter访问条目的键和值.

迭代器本身将是Iterator< MyHashTable.HashEntry< TKey,TValue>>的实例.至于写一个,这取决于你自己的哈希表实现,但你基本上你需要一种方式获得任何序列的下一个元素:Iterator.next().

例如,这是一个迭代一个简单数组的iterator()方法实现:

private Type[] arrayList;

private int currentSize;

@Override

public Iterator iterator() {

Iterator it = new Iterator() {

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;

}

希望这有点帮助.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值