File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes generics - map of lists Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "generics - map of lists" Watch "generics - map of lists" New topic
Author

generics - map of lists

Sharon whipple
Ranch Hand

Joined: Jul 31, 2003
Posts: 294
Hi all

On the following code
Compiler is giving this warning (line 1):
List is a raw type. References to generic type List<E> should be parameterized





What is the correct way to init levelsMap ?

Thank you very much
Sharon
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216


After all, you don't want to insert lists, you want to insert lists of strings.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Sharon whipple
Ranch Hand

Joined: Jul 31, 2003
Posts: 294
Thank you!
on the same matter, for this code :

The compiler gives warning for
Type safety: The expression of type List needs unchecked conversion to conform to List<String>
Why is that?

Thank you
Sharon
[ September 24, 2007: Message edited by: Sharon whipple ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

I can't be sure without seeing the code, but my guess is that query.getResultList() returns List, not List<String>.

You can either modify query.getResultList() to return List<String> instead, or if you can't (because it's not your code) put the following line above this one:

This will remove the warning, but it is really better to change the code of query.getResultList() instead.

For anyone who says you can cast to (List<String> ) instead of suppressing warnings, that cast will cause a warning as well
[ September 24, 2007: Message edited by: Rob Prime ]
Huan Niu
Greenhorn

Joined: Sep 21, 2007
Posts: 11
Modify your code to:

HashMap<String,List<String>> levelsMap = new HashMap<String,List<String>>();


It will be fine. Did that answer your question?
Huan Niu
Greenhorn

Joined: Sep 21, 2007
Posts: 11
At most occasions, we say:

Map<String,List<String>> levelsMap = new HashMap<String,List<String>>();


Using interface gives you more flexible, and it's the OOP way...
Sharon whipple
Ranch Hand

Joined: Jul 31, 2003
Posts: 294
Originally posted by Huan Niu:
Modify your code to:

HashMap<String,List<String>> levelsMap = new HashMap<String,List<String>>();

It will be fine. Did that answer your question?



Yes it answered my question. thank you

About the interfaces this code is imported code from other department = anyway I will be glad to hear other implementation suggestion.

About the query.getResultList()
What I don't understand is why query.getResultList() not generify?
(Query class: javax.persistence.Query) in Javaee-5.0.jar.
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
Yes, unfortunately EJB 3.0 Persistence did not generify their collections, and the Query.getResultList() returns a List and not a List<Object>. You can't cast your way out of a compiler warning (without other settings sent to the compiler).

So (as of Nov 2007),



This is something that EJB Persistence needs to fix.
It likely won't happen until mid 2008.
Make a note of these in your code so you can go clean them up later!


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: generics - map of lists
 
Similar Threads
confuse statement ?
generics and maps
if isEmpty or null
Confusion with generics
Transforming a class instance to fit into the generic bracket