简体   繁体   中英

Method in hashtable class needs to return Enumeration type

I have searched and can't seem to find an answer on this. I understand what an Enumeration type is, but I can't for the life of me determine how to create and return one in a function. Basically, I have a function:

public Enumeration getKeys(){
    //Returns an Enumeration of valid keys in the hash table
}

that needs to return an Enumeration.

My hashtable contains a key object and a value object, and I have already implemented methods like containsKey(Object key) and retrieve(Object key) to help me determine if a key is valid in the table and what the contents of the table at the key value are. Any help on understanding where to start with building an Enumeration type and returning it would be greatly appreciated.

It is "relatively" easy, change your code like this:

public Enumeration getKeys(){
       return new Enumeration() {

        @Override
        public boolean hasMoreElements() {
            // TODO Test if this enumeration contains more elements.
            return false;
        }

        @Override
        public Object nextElement() {
            // TODO Return the next element of this enumeration if this enumeration object has at least one more element to provide.
            return null;
        }
    };
    }

Now it is up to you to implement the two methods with respect of you business rules.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM