File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes from Object to concret Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "from Object to concret Object" Watch "from Object to concret Object" New topic
Author

from Object to concret Object

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
I have this:



You see, I have a object, which kind can change.

Now I want to assign a concrete Object to this:



But this does not work.

What is wrong?
Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142
Please try to be a bit more descriptive in your posts:

But this does not work.
does not give much info....what happens? A runtime error, compiler error, a power cut in your street?!!

What type of object is "myUserObject"? Anyway, I would assume you cannot cast a Map<User, List<String> to Map<Object, List<String> as they are different types, due to the type of the key changing.


<a href="http://faq.javaranch.com/java/UseCodeTags" target="_blank" rel="nofollow">Use Code Tags!!</a>
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

Map<Object, X> is not compatible with Map<User, X>

You may want to change the declaration of your Map:

This way, you can assign any map for which the value type is List<String>. You can't put anything in this Map though, because the compiler doesn't know the actual type and therefore cannot make sure the generic constraints are enforced.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
thanks,

I have solved it that way:



This works! And I get no Compiler-Error, even when I change the object-type!!

hmm...now I have the question:

What is better?

This:


or the putAll-Method ??
[ December 08, 2008: Message edited by: nimo frey ]
Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142
I think the is better as you then know what the map contains straight away
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

Originally posted by Tom Johnson:
I think the is better as you then know what the map contains straight away

But other Maps that have other object types of keys may also be needed (I don't know, but it doesn't seem unreasonable).

Originally posted by nimo frey:
hmm...now I have the question:

What is better?

This:


or the putAll-Method ??

It depends.

Using ? for the key type will remove the requirement of an extra HashMap object, but you can't add anything afterwards.
Using putAll allows you to add anything, but you will need an extra object.

Now if you don't need to add anything anymore, then I'd go for ?. You can still retrieve the keys as Objects, and it saves the one object (plus the time it takes for putAll to complete). Otherwise you won't have any choice, and you need putAll.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
If you go through the Java Tutorials, you find a section about cages for butterflies and lions, which explains why you can't do what you were trying to do.
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
okay thanks that helped me very much.

I use the ?. instead of putAll.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: from Object to concret Object
 
Similar Threads
is object a list of string or only a string?
Java Generic question
Map within a List
jaxb hashmap?
Iterating over Map using JSTL