how to use the Generics

H

harryos

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
 
L

Lew

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.
 
A

Arne Vajhøj

harryos said:
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
 
L

Lew

harryos said:
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")

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top