Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: How convert Iterator into Enumeration Date: Wed, 28 Nov 2012 13:22:34 -0500 Organization: A noiseless patient Spider Lines: 48 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 28 Nov 2012 18:22:25 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="ffb8f7085759b339c1002252b48331a4"; logging-data="11420"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+dsEszLMn3pdSnI+gTBGni" User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/17.0 Thunderbird/17.0 In-Reply-To: Cancel-Lock: sha1:ejlv7dg+67Q9Du3KRLO8s7kHtT4= Xref: csiph.com comp.lang.java.programmer:20007 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: > > /** > *

Retrieve the parameter names.

> * > * @return The names. > */ > public Enumeration getParameterNames() { > return parametermap.keys(); > } public Enumeration getParameterNames() { return new Enumeration() { private final Iterator 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, with a constructor that takes an Iterator. A companion class wrapping an Iterator around an Enumeration 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