On Dec 17, 10:14*am, harryos <oswald.ha...@gmail.com> 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[i]);
> * * * * * * * * *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
|