Solved

JDK1.5 Generic Class Cast problem..

Posted on 2006-10-20
7
659 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

809 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