Solved

JDK1.5 Generic Class Cast problem..

Posted on 2006-10-20
7
662 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 
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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 100
printf performancy 11 86
Java syntax, or is it Selenium 6 42
Java 8 to Java 6 8 33
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

733 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