Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > how to use the Generics

Reply
Thread Tools

how to use the Generics

 
 
harryos
Guest
Posts: n/a
 
      12-17-2008
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 ?
thanks
harry
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      12-17-2008
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

 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      12-17-2008
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[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 ?


There are another thread with a very similar topic.

I would use:

@SuppressWarnings("unchecked")

Arne
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      12-18-2008

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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
generics depending on generics Soul VHDL 0 02-02-2009 09:14 AM
Does using generics complicate the use of interface-type declarations? jan V Java 15 09-12-2005 02:44 AM
Can't convert a generics list of objects into a generics list ofinterfaces Juergen Berchtel Java 1 05-20-2005 02:07 PM
Will the new Generics mechanism still use runtime casting? Dave Stallard Java 37 10-08-2003 05:23 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57