how to use the Generics

Discussion in 'Java' started by harryos, Dec 17, 2008.

  1. harryos

    harryos Guest

    hi,
    In an application I need to retrieve the ParameterMap from a servlet
    request and use an iterator to go through the map entries and get the
    key value pairs.I did it like this

    public void doPost(HttpServletRequest request,
    HttpServletResponse response) throws ServletException,IOException{
    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter( );
    ...
    Map param_map=request.getParameterMap();
    Iterator iterator = param_map.entrySet( ).iterator();
    while(iterator.hasNext( )){
    Map.Entry me = (Map.Entry)iterator.next( );
    out.println(me.getKey( ) + ":>> ");
    String[] arr = (String[]) (me.getValue( ));
    for(int i=0;i<arr.length;i++){
    out.println(arr);
    if (i > 0 && i != arr.length-1)
    out.println(", ");
    }
    out.println("<br><br>");
    }
    ....
    }

    Though this code compiles without any errors,I think I need to use the
    <K,V > notation to specify the types.I tried to do that as below.

    Map<String,String[]> param_map=(Map<String,String[]>)
    (request.getParameterMap());

    I am getting a warning for unchecked cast.

    warning: [unchecked] unchecked cast
    [javac] found : java.util.Map
    [javac] required: java.util.Map<java.lang.String,java.lang.String[]
    >

    [javac] Map<String,String[]> param_map=(Map<String,String[]>)
    (request.getParameterMap());


    Can someone help me correct this ?
    thanks
    harry
     
    harryos, Dec 17, 2008
    #1
    1. Advertising

  2. harryos

    Lew Guest

    On Dec 17, 10:14 am, harryos <> wrote:
    > hi,
    > In an application I need to retrieve the ParameterMap from a servlet
    > request and use an iterator to go through the map entries and get the
    > key value pairs.I did it like this
    >
    > public void doPost(HttpServletRequest request,
    >    HttpServletResponse response) throws ServletException,IOException{
    >    response.setContentType("text/html");
    >    java.io.PrintWriter out = response.getWriter( );
    >    ...
    >    Map param_map=request.getParameterMap();
    >   Iterator iterator = param_map.entrySet( ).iterator();
    >   while(iterator.hasNext( )){
    >            Map.Entry me = (Map.Entry)iterator.next( );
    >            out.println(me.getKey( ) + ":>> ");
    >            String[] arr = (String[]) (me.getValue( ));
    >            for(int i=0;i<arr.length;i++){
    >                 out.println(arr);
    >                  if (i > 0 && i != arr.length-1)
    >                 out.println(", ");
    >            }
    >            out.println("<br><br>");
    >          }
    > ...
    >
    > }
    >
    > Though this code compiles without any errors,I think I need to use the
    > <K,V > notation to specify the types.I tried to do that as below.
    >
    > Map<String,String[]> param_map=(Map<String,String[]>)
    > (request.getParameterMap());
    >
    > I am getting a warning for unchecked cast.
    >
    > warning: [unchecked] unchecked cast
    >    [javac] found   : java.util.Map
    >    [javac] required: java.util.Map<java.lang.String,java.lang.String[]
    >
    >    [javac] Map<String,String[]> param_map=(Map<String,String[]>)
    > (request.getParameterMap());
    >
    > Can someone help me correct this ?


    On the Sun page for Joshua Bloch's seminal /Effective Java/
    <http://java.sun.com/docs/books/effective/>
    there's a link to the free chapter on generics:
    <http://java.sun.com/docs/books/effective/generics.pdf>

    Read and study it.

    --
    Lew
     
    Lew, Dec 17, 2008
    #2
    1. Advertising

  3. harryos

    Arne Vajhøj Guest

    harryos wrote:
    > In an application I need to retrieve the ParameterMap from a servlet
    > request and use an iterator to go through the map entries and get the
    > key value pairs.I did it like this
    >
    > public void doPost(HttpServletRequest request,
    > HttpServletResponse response) throws ServletException,IOException{
    > response.setContentType("text/html");
    > java.io.PrintWriter out = response.getWriter( );
    > ...
    > Map param_map=request.getParameterMap();
    > Iterator iterator = param_map.entrySet( ).iterator();
    > while(iterator.hasNext( )){
    > Map.Entry me = (Map.Entry)iterator.next( );
    > out.println(me.getKey( ) + ":>> ");
    > String[] arr = (String[]) (me.getValue( ));
    > for(int i=0;i<arr.length;i++){
    > out.println(arr);
    > if (i > 0 && i != arr.length-1)
    > out.println(", ");
    > }
    > out.println("<br><br>");
    > }
    > ...
    > }
    >
    > Though this code compiles without any errors,I think I need to use the
    > <K,V > notation to specify the types.I tried to do that as below.
    >
    > Map<String,String[]> param_map=(Map<String,String[]>)
    > (request.getParameterMap());
    >
    > I am getting a warning for unchecked cast.
    >
    > warning: [unchecked] unchecked cast
    > [javac] found : java.util.Map
    > [javac] required: java.util.Map<java.lang.String,java.lang.String[]
    > [javac] Map<String,String[]> param_map=(Map<String,String[]>)
    > (request.getParameterMap());
    >
    >
    > Can someone help me correct this ?


    There are another thread with a very similar topic.

    I would use:

    @SuppressWarnings("unchecked")

    Arne
     
    Arne Vajhøj, Dec 17, 2008
    #3
  4. harryos

    Lew Guest

    harryos wrote:
    >> I am getting a warning for unchecked cast.
    >>
    >> warning: [unchecked] unchecked cast
    >> [javac] found : java.util.Map
    >> [javac] required: java.util.Map<java.lang.String,java.lang.String[]
    >> [javac] Map<String,String[]> param_map=(Map<String,String[]>)
    >> (request.getParameterMap());
    >>
    >>
    >> Can someone help me correct this ?


    Arne Vajhøj wrote:
    > There are another thread with a very similar topic.
    >
    > I would use:
    >
    > @SuppressWarnings("unchecked")


    But do be sure to read and study the chapter from /Effective Java/ referenced
    upthread for the correct way to use this annotation.

    --
    Lew
     
    Lew, Dec 18, 2008
    #4
    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. Dave Stallard
    Replies:
    37
    Views:
    982
    Roedy Green
    Oct 8, 2003
  2. Juergen Berchtel
    Replies:
    1
    Views:
    6,100
    John C. Bollinger
    May 20, 2005
  3. jan V
    Replies:
    15
    Views:
    672
    Dale King
    Sep 12, 2005
  4. Andrew FPGA
    Replies:
    19
    Views:
    918
    Jim Lewis
    Oct 11, 2006
  5. Soul
    Replies:
    0
    Views:
    553
Loading...

Share This Page