How convert Iterator into Enumeration

J

Jan Burse

Dear All,

Is there a fast way to have an Enumeration from a HashMap?

I am trying to reimplement the HttpServletRequest interface,
and I am trying to use a HashMap for parameters and attributes.
I guess this is valid when my web application doesn't use
a HttpServletRequest concurrently, right? Or might the web
server populate it concurrently?

Now I am stuck here:

/**
* <p>Retrieve the parameter names.</p>
*
* @return The names.
*/
public Enumeration<String> getParameterNames() {
return parametermap.keys();
}

It requires a Enumeration, but when parametermap is
a HashMap, it will not deliver an Enumeration, but
instead an Iterator via keySet().iterator(). Any fast
way to have an Enumeration nevertheless?

Bye

P.S.:
Other interface methods would allow a migration to
HashMap, for example:

/**
* <p>Retrieve the parameter map.</p>
*
* @return The parameter map.
*/
public Map<String, String[]> getParameterMap() {
return parametermap;
}
 
R

Roedy Green

Is there a fast way to have an Enumeration from a HashMap?

An Enumeration is a primitive time of Iterator.

You could write some code that in the constructor took an Iterator and
behaved like an Enumerator. The Enumerator methods would make calls on
the corresponding Iterator methods.

There are a number of places where you still need the old
StringBuffer, Vector and Enumeration classes. Perhaps these uses
should be upgraded deprecating the old versions.
 
E

Eric Sosman

Dear All,

Is there a fast way to have an Enumeration from a HashMap?

I am trying to reimplement the HttpServletRequest interface,
and I am trying to use a HashMap for parameters and attributes.
I guess this is valid when my web application doesn't use
a HttpServletRequest concurrently, right? Or might the web
server populate it concurrently?

Now I am stuck here:

/**
* <p>Retrieve the parameter names.</p>
*
* @return The names.
*/
public Enumeration<String> getParameterNames() {
return parametermap.keys();
}

public Enumeration<String> getParameterNames() {
return new Enumeration<String>() {
private final Iterator<String> iter =
myHashMap.iterator();
@Override
public boolean hasMoreElements() {
return iter.hasNext();
}
@Override
public String nextElement() {
return iter.next();
}
};
}

If you do this sort of thing a lot write yourself a utility
class implementing Enumeration<T>, with a constructor that
takes an Iterator<T>. A companion class wrapping an Iterator<T>
around an Enumeration<T> is equally easy to write, and might
also be handy. (I'm a little surprised that Snoracle doesn't
provide such wrappers -- or maybe they do, but under names
that have escaped my notice.)
 
E

Eric Sosman

[...]
If you do this sort of thing a lot write yourself a utility
class implementing Enumeration<T>, with a constructor that
takes an Iterator<T>. A companion class wrapping an Iterator<T>
around an Enumeration<T> is equally easy to write, and might
also be handy. (I'm a little surprised that Snoracle doesn't
provide such wrappers -- or maybe they do, but under names
that have escaped my notice.)

Aha! Thanks to Jim Janney, I've just learned about the
Collections#enumeration(Collection) method. It's perhaps a
smidgen less general than enumeration(Iterator) would be, but
only a smidgen.

(And I still don't see an iterator(Enumeration) method
anywhere. Maybe Santa will bring one ...)
 
R

Roedy Green

enumeration method in java.util.Collections.

Are you referring to?
static <T> Enumeration<T> enumeration(Collection<T> c)

I don't see one that takes an Iterator.
 
D

Daniel Pitts

Are you referring to?
static <T> Enumeration<T> enumeration(Collection<T> c)

I don't see one that takes an Iterator.

public Enumeration<String> getParameterNames() {
return Collections.enumeration(parameters.keySet());
}
 
J

Jim Janney

Roedy Green said:
Are you referring to?
static <T> Enumeration<T> enumeration(Collection<T> c)

I don't see one that takes an Iterator.

Yes, and yes. Not as general as adapting an Iterator, but good enough
for the problem as originally described, which was to produce an
Enumeration from a HashMap. Assuming that parametermap is declared as

Map<String, Object> parametermap;

you can write

/**
* <p>Retrieve the parameter names.</p>
*
* @return The names.
*/
public Enumeration<String> getParameterNames() {
return java.util.Collections.enumeration(parametermap.keyset());
}
 
M

markspace

Aha! Thanks to Jim Janney, I've just learned about the
Collections#enumeration(Collection) method. It's perhaps a
smidgen less general than enumeration(Iterator) would be, but
only a smidgen.

(And I still don't see an iterator(Enumeration) method
anywhere. Maybe Santa will bring one ...)


Yes, that was a useful post. I also note some other interesting methods:

static <T> Enumeration<T> emptyEnumeration()
Returns an enumeration that has no elements.

static <T> Iterator<T> emptyIterator()
Returns an iterator that has no elements.

static <T> ListIterator<T> emptyListIterator()
Returns a list iterator that has no elements.

There's methods to return empty collections too (List, Map, Set) as well
as singletons for List, Map and Set, but I think more folks know about
those. The above methods I listed are new with Java 1.7.
 
D

Daniele Futtorovic

[...]
If you do this sort of thing a lot write yourself a utility
class implementing Enumeration<T>, with a constructor that
takes an Iterator<T>. A companion class wrapping an Iterator<T>
around an Enumeration<T> is equally easy to write, and might
also be handy. (I'm a little surprised that Snoracle doesn't
provide such wrappers -- or maybe they do, but under names
that have escaped my notice.)

Aha! Thanks to Jim Janney, I've just learned about the
Collections#enumeration(Collection) method. It's perhaps a
smidgen less general than enumeration(Iterator) would be, but
only a smidgen.

That's been there for a while. :)

Unfortunately, there's no enumeration(Iterable) support. Iterable, being
a somewhat late addition, is overall poorly integrated, which is a pity.
Same thing goes for CharSequence.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top