How convert Iterator into Enumeration

Discussion in 'Java' started by Jan Burse, Nov 28, 2012.

  1. Jan Burse

    Jan Burse Guest

    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;
    }
     
    Jan Burse, Nov 28, 2012
    #1
    1. Advertising

  2. Jan Burse

    Roedy Green Guest

    On Wed, 28 Nov 2012 12:55:17 +0100, Jan Burse <>
    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.
     
    Roedy Green, Nov 28, 2012
    #2
    1. Advertising

  3. Jan Burse

    Jim Janney Guest

    Jim Janney, Nov 28, 2012
    #3
  4. Jan Burse

    Jim Janney Guest

    Jim Janney, Nov 28, 2012
    #4
  5. Jan Burse

    Eric Sosman Guest

    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
    d
     
    Eric Sosman, Nov 28, 2012
    #5
  6. Jan Burse

    Eric Sosman Guest

    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
    d
     
    Eric Sosman, Nov 28, 2012
    #6
  7. Jan Burse

    Roedy Green Guest

    On Wed, 28 Nov 2012 11:11:47 -0700, Jim Janney
    <> 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, Nov 28, 2012
    #7
  8. Jan Burse

    Roedy Green Guest

    Roedy Green, Nov 28, 2012
    #8
  9. Jan Burse

    Daniel Pitts Guest

    On 11/28/12 12:36 PM, Roedy Green wrote:
    > On Wed, 28 Nov 2012 11:11:47 -0700, Jim Janney
    > <> 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());
    }
     
    Daniel Pitts, Nov 28, 2012
    #9
  10. Jan Burse

    Jim Janney Guest

    Roedy Green <> writes:

    > On Wed, 28 Nov 2012 11:11:47 -0700, Jim Janney
    > <> 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
     
    Jim Janney, Nov 28, 2012
    #10
  11. Jan Burse

    markspace Guest

    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.
     
    markspace, Nov 28, 2012
    #11
  12. 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.
     
    Daniele Futtorovic, Dec 1, 2012
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Olaf Petzold
    Replies:
    4
    Views:
    5,461
    Mike Treseler
    Nov 28, 2005
  2. Robert Maas, see http://tinyurl.com/uh3t

    My idea for filtering an Enumeration or Iterator

    Robert Maas, see http://tinyurl.com/uh3t, May 13, 2005, in forum: Java
    Replies:
    8
    Views:
    459
    Robert Maas, see http://tinyurl.com/uh3t
    Jul 28, 2005
  3. Replies:
    6
    Views:
    662
    Jim Langston
    Oct 30, 2005
  4. puvit82
    Replies:
    4
    Views:
    768
    puvit82
    Feb 1, 2008
  5. Stef
    Replies:
    7
    Views:
    2,717
    rickman
    May 18, 2008
Loading...

Share This Page