Solved

JDK1.5 Generic Class Cast problem..

Posted on 2006-10-20
7
657 Views
Last Modified: 2011-04-14
Map<?, ?> map = (Map<?, ?>) req.getParameterMap();

Set<String> set = (Set<String>) map.keySet();

=========================

I received the following compilatio warning:

warning: [unchecked] unchecked cast
found   : java.util.Set<capture of ?>
required: java.util.Set<java.lang.String>
                Set<String> set = (Set<String>)map.keySet();

===========
How do I get rid of this warning??
0
Comment
Question by:zollen
  • 3
  • 2
  • 2
7 Comments
 
LVL 14

Expert Comment

by:hoomanv
ID: 17773748
Because of Map<?, ?>
You can only say
Set<?> set = (Set<?>) map.keySet();
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17773792
Try

java.util.Enumeration<String> enum = req.getParameterNames();

or

java.util.Enumeration<String> enum = (java.util.Enumeration<String>)req.getParameterNames();
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 17773981
CEHJ there is no Generics support in javaee according to the documentation
http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletRequest.html#getParameterMap()
All collections are being used like traditional non-generic form
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

by:CEHJ
ID: 17774007
That's true
0
 
LVL 9

Expert Comment

by:shinobun
ID: 17774061
Since the J2EE specs do not return generalized (?) Maps, the best you can do is annotate your method with SuppressWarnings to tell the compiler to ignore the warning.

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/SuppressWarnings.html
0
 
LVL 9

Expert Comment

by:shinobun
ID: 17774081
I'm not saying that it will work at runtime, though...  You still might get a ClassCastException.
0
 
LVL 9

Accepted Solution

by:
shinobun earned 50 total points
ID: 17774143
@SuppressWarnings("unchecked")
void yourMethod() {
    Map<?, ?> map = (Map<?, ?>) req.getParameterMap();
    Set<String> set = (Set<String>) map.keySet();
}

cf. http://java.sun.com/docs/books/tutorial/java/javaOO/annotations.html
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now