Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #20001 > unrolled thread

How convert Iterator into Enumeration

Started byJan Burse <janburse@fastmail.fm>
First post2012-11-28 12:55 +0100
Last post2012-12-01 17:49 +0100
Articles 13 — 8 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  How convert Iterator into Enumeration Jan Burse <janburse@fastmail.fm> - 2012-11-28 12:55 +0100
    Re: How convert Iterator into Enumeration Roedy Green <see_website@mindprod.com.invalid> - 2012-11-28 05:22 -0800
      Re: How convert Iterator into Enumeration Wanja Gayk <brixomatic@yahoo.com> - 2012-11-30 20:23 +0100
    Re: How convert Iterator into Enumeration Jim Janney <jjanney@shell.xmission.com> - 2012-11-28 08:53 -0700
      Re: How convert Iterator into Enumeration Jim Janney <jjanney@shell.xmission.com> - 2012-11-28 11:11 -0700
        Re: How convert Iterator into Enumeration Roedy Green <see_website@mindprod.com.invalid> - 2012-11-28 12:36 -0800
          Re: How convert Iterator into Enumeration Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-11-28 13:00 -0800
          Re: How convert Iterator into Enumeration Jim Janney <jjanney@shell.xmission.com> - 2012-11-28 14:01 -0700
      Re: How convert Iterator into Enumeration Roedy Green <see_website@mindprod.com.invalid> - 2012-11-28 12:38 -0800
    Re: How convert Iterator into Enumeration Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-11-28 13:22 -0500
      Re: How convert Iterator into Enumeration Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-11-28 13:28 -0500
        Re: How convert Iterator into Enumeration markspace <-@.> - 2012-11-28 13:10 -0800
        Re: How convert Iterator into Enumeration Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2012-12-01 17:49 +0100

#20001 — How convert Iterator into Enumeration

FromJan Burse <janburse@fastmail.fm>
Date2012-11-28 12:55 +0100
SubjectHow convert Iterator into Enumeration
Message-ID<k94u33$2vl$1@news.albasani.net>
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;
     }




[toc] | [next] | [standalone]


#20002

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-11-28 05:22 -0800
Message-ID<er3cb8ldjksik6tmvplaj5fqgf6p6v842s@4ax.com>
In reply to#20001
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. 

[toc] | [prev] | [next] | [standalone]


#20030

FromWanja Gayk <brixomatic@yahoo.com>
Date2012-11-30 20:23 +0100
Message-ID<MPG.2b23262ddb28779a989740@202.177.16.121>
In reply to#20002
In article <er3cb8ldjksik6tmvplaj5fqgf6p6v842s@4ax.com>, Roedy Green 
(see_website@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: news@netfront.net ---

[toc] | [prev] | [next] | [standalone]


#20003

FromJim Janney <jjanney@shell.xmission.com>
Date2012-11-28 08:53 -0700
Message-ID<ydn4nk9sozt.fsf@shell.xmission.com>
In reply to#20001
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/collections/api-3.1/org/apache/commons/collections/IteratorUtils.html#asEnumeration%28java.util.Iterator%29

-- 
Jim Janney

[toc] | [prev] | [next] | [standalone]


#20005

FromJim Janney <jjanney@shell.xmission.com>
Date2012-11-28 11:11 -0700
Message-ID<ydnzk21r40s.fsf@shell.xmission.com>
In reply to#20003
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/collections/api-3.1/org/apache/commons/collections/IteratorUtils.html#asEnumeration%28java.util.Iterator%29

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

-- 
Jim Janney

[toc] | [prev] | [next] | [standalone]


#20009

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-11-28 12:36 -0800
Message-ID<74tcb8dm7vrkptq3s5oa83a2ann907grn6@4ax.com>
In reply to#20005
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. 

[toc] | [prev] | [next] | [standalone]


#20011

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2012-11-28 13:00 -0800
Message-ID<ZXuts.13686$v83.1911@newsfe09.iad>
In reply to#20009
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());
}

[toc] | [prev] | [next] | [standalone]


#20012

FromJim Janney <jjanney@shell.xmission.com>
Date2012-11-28 14:01 -0700
Message-ID<ydnvccpqw66.fsf@shell.xmission.com>
In reply to#20009
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.keyset());
    }

-- 
Jim Janney

[toc] | [prev] | [next] | [standalone]


#20010

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-11-28 12:38 -0800
Message-ID<eetcb8h18u0uv2vi8tlbnj5ijtq69hh9dv@4ax.com>
In reply to#20003
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/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.
-- 
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. 

[toc] | [prev] | [next] | [standalone]


#20007

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2012-11-28 13:22 -0500
Message-ID<k95kp1$b4s$1@dont-email.me>
In reply to#20001
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

[toc] | [prev] | [next] | [standalone]


#20008

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2012-11-28 13:28 -0500
Message-ID<k95l3k$d5j$1@dont-email.me>
In reply to#20007
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

[toc] | [prev] | [next] | [standalone]


#20014

Frommarkspace <-@.>
Date2012-11-28 13:10 -0800
Message-ID<k95ujv$8ki$1@dont-email.me>
In reply to#20008
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.

[toc] | [prev] | [next] | [standalone]


#20034

FromDaniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Date2012-12-01 17:49 +0100
Message-ID<k9dcfe$q2u$1@dont-email.me>
In reply to#20008
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web