簡體   English   中英

java - 如何創建自定義哈希表迭代器?

[英]java - how to create custom hashtable iterator?

我目前正在嘗試實現一個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?

一般來說,是的,如果你提供迭代HashEntry的迭代器可能會更好,因此用戶在迭代時同時獲得鍵和值(和狀態)。 通常,沒有密鑰,價值就沒有意義,反之亦然。

為什么不將HashEntry類作為public static泛型內部類,並使特定於實現的東西HashEntry private 您可能還需要使HashEntry通用的,因為我假設您的父類(我們只是稱之為MyHashTable )也是基於TKeyTValue通用。

所以,如果我是你,我會讓你的HashEntryMyHashTable看起來更像這樣:

// 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();
        }
    }
}

請注意, MyHashTable現在是HashEntry的內部類,它是通用的,它的構造函數現在是private 這保證除了外部類MyHashTable之外的任何人都可以實例化HashEntry ,因為在哈希表外部實例化一個是沒有意義的(參見這個 )。 但是,其他人可以通過getter訪問條目的鍵和值。

迭代器本身將是Iterator<MyHashTable.HashEntry<TKey, TValue>>一個實例Iterator<MyHashTable.HashEntry<TKey, TValue>> 至於寫一個,這取決於你自己的哈希表實現,但你基本上你需要一種方式獲得任何序列中的下一個元素: Iterator<E>.next()

例如,這是一個iterator()一個簡單數組的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

希望這有點幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM