Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

JDK1.5 Generic Class Cast problem..

Posted on 2006-10-20
7
Medium Priority
?
669 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 200 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.
Suggested Courses

886 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