• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

generics - map of lists  RSS feed

 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 21502
96
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

After all, you don't want to insert lists, you want to insert lists of strings.
 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21502
96
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!