Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How convert Iterator into Enumeration

131 views
Skip to first unread message

Jan Burse

unread,
Nov 28, 2012, 6:55:17 AM11/28/12
to
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;
}





Roedy Green

unread,
Nov 28, 2012, 8:22:42 AM11/28/12
to
On Wed, 28 Nov 2012 12:55:17 +0100, Jan Burse <janb...@fastmail.fm>
wrote, quoted or indirectly quoted someone who said :

>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.
--
Roedy Green Canadian Mind Products http://mindprod.com
Students who hire or con others to do their homework are as foolish
as couch potatoes who hire others to go to the gym for them.

Jim Janney

unread,
Nov 28, 2012, 10:53:26 AM11/28/12
to
Jan Burse <janb...@fastmail.fm> writes:

> Dear All,
>
> Is there a fast way to have an Enumeration from a HashMap?
>

IteratorUtils in Apache Commons has a conversion method.

http://commons.apache.org/collections/api-3.1/org/apache/commons/collections/IteratorUtils.html#asEnumeration%28java.util.Iterator%29

--
Jim Janney

Jim Janney

unread,
Nov 28, 2012, 1:11:47 PM11/28/12
to
Even easier, just use the enumeration method in java.util.Collections.

--
Jim Janney

Eric Sosman

unread,
Nov 28, 2012, 1:22:34 PM11/28/12
to
On 11/28/2012 6:55 AM, Jan Burse wrote:
> 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.)

--
Eric Sosman
eso...@comcast-dot-net.invalid

Eric Sosman

unread,
Nov 28, 2012, 1:28:13 PM11/28/12
to
On 11/28/2012 1:22 PM, Eric Sosman wrote:
>[...]
> 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 ...)

--
Eric Sosman
eso...@comcast-dot-net.invalid

Roedy Green

unread,
Nov 28, 2012, 3:36:16 PM11/28/12
to
On Wed, 28 Nov 2012 11:11:47 -0700, Jim Janney
<jja...@shell.xmission.com> wrote, quoted or indirectly quoted
someone who said :

>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.

Roedy Green

unread,
Nov 28, 2012, 3:38:05 PM11/28/12
to
On Wed, 28 Nov 2012 08:53:26 -0700, Jim Janney
<jja...@shell.xmission.com> wrote, quoted or indirectly quoted
someone who said :

>
>http://commons.apache.org/collections/api-3.1/org/apache/commons/collections/IteratorUtils.html#asEnumeration%28java.util.Iterator%29

In there is also a method to create an Iterator out of an array.

Daniel Pitts

unread,
Nov 28, 2012, 4:00:41 PM11/28/12
to
On 11/28/12 12:36 PM, Roedy Green wrote:
> On Wed, 28 Nov 2012 11:11:47 -0700, Jim Janney
> <jja...@shell.xmission.com> wrote, quoted or indirectly quoted
> someone who said :
>
>> 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.
>

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


Jim Janney

unread,
Nov 28, 2012, 4:01:21 PM11/28/12
to
Roedy Green <see_w...@mindprod.com.invalid> writes:

> On Wed, 28 Nov 2012 11:11:47 -0700, Jim Janney
> <jja...@shell.xmission.com> wrote, quoted or indirectly quoted
> someone who said :
>
>>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.

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

--
Jim Janney

markspace

unread,
Nov 28, 2012, 4:10:21 PM11/28/12
to
On 11/28/2012 10:28 AM, Eric Sosman wrote:

> 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.


Wanja Gayk

unread,
Nov 30, 2012, 2:23:42 PM11/30/12
to
In article <er3cb8ldjksik6tmv...@4ax.com>, Roedy Green
(see_w...@mindprod.com.invalid) says...

> 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.

Be sure to evade the ConcurrentModificationException then.

Kind regards,
-Wanja-


--
..Alesi's problem was that the back of the car was jumping up and down
dangerously - and I can assure you from having been teammate to
Jean Alesi and knowing what kind of cars that he can pull up with,
when Jean Alesi says that a car is dangerous - it is. [Jonathan Palmer]

--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---

Daniele Futtorovic

unread,
Dec 1, 2012, 11:49:52 AM12/1/12
to
On 28/11/2012 19:28, Eric Sosman allegedly wrote:
> On 11/28/2012 1:22 PM, Eric Sosman wrote:
>> [...]
>> 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.

--
DF.
0 new messages