Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   How convert Iterator into Enumeration (http://www.velocityreviews.com/forums/t954929-how-convert-iterator-into-enumeration.html)

Jan Burse 11-28-2012 11:55 AM

How convert Iterator into Enumeration
 
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 11-28-2012 01:22 PM

Re: How convert Iterator into Enumeration
 
On Wed, 28 Nov 2012 12:55:17 +0100, Jan Burse <janburse@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 11-28-2012 03:53 PM

Re: How convert Iterator into Enumeration
 
Jan Burse <janburse@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/collection...il.Iterator%29

--
Jim Janney

Jim Janney 11-28-2012 06:11 PM

Re: How convert Iterator into Enumeration
 
Jim Janney <jjanney@shell.xmission.com> writes:

> Jan Burse <janburse@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/collection...il.Iterator%29


Even easier, just use the enumeration method in java.util.Collections.

--
Jim Janney


Eric Sosman 11-28-2012 06:22 PM

Re: How convert Iterator into Enumeration
 
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
esosman@comcast-dot-net.invalid

Eric Sosman 11-28-2012 06:28 PM

Re: How convert Iterator into Enumeration
 
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
esosman@comcast-dot-net.invalid

Roedy Green 11-28-2012 08:36 PM

Re: How convert Iterator into Enumeration
 
On Wed, 28 Nov 2012 11:11:47 -0700, Jim Janney
<jjanney@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 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.

Roedy Green 11-28-2012 08:38 PM

Re: How convert Iterator into Enumeration
 
On Wed, 28 Nov 2012 08:53:26 -0700, Jim Janney
<jjanney@shell.xmission.com> wrote, quoted or indirectly quoted
someone who said :

>
>http://commons.apache.org/collection...il.Iterator%29


In there is also a method to create an Iterator out of an array.
--
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.

Daniel Pitts 11-28-2012 09:00 PM

Re: How convert Iterator into Enumeration
 
On 11/28/12 12:36 PM, Roedy Green wrote:
> On Wed, 28 Nov 2012 11:11:47 -0700, Jim Janney
> <jjanney@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 11-28-2012 09:01 PM

Re: How convert Iterator into Enumeration
 
Roedy Green <see_website@mindprod.com.invalid> writes:

> On Wed, 28 Nov 2012 11:11:47 -0700, Jim Janney
> <jjanney@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.key set());
}

--
Jim Janney


All times are GMT. The time now is 03:11 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.