Solved

JDK1.5 Generic Class Cast problem..

Posted on 2006-10-20
7
656 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumHeights2  challenge 7 78
word0 challenge 4 54
wordlen challenge 3 49
Error with Java/Cache JDBC Classpath 2 20
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…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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:

746 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

9 Experts available now in Live!

Get 1:1 Help Now