• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

from Object to concret Object

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the is better as you then know what the map contains straight away
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay thanks that helped me very much.

I use the ?. instead of putAll.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic