• Post Reply Bookmark Topic Watch Topic
  • New Topic

Casting from Array List to hashmap  RSS feed

 
Isaac Ferguson
Ranch Hand
Posts: 1063
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I need to cast from ArrayList to HashMap.



I get a error the next error:



Any idea?

Regards,
Isaac
 
Tim Cooke
Marshal
Posts: 4048
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A List and a Map is not the same thing, so you cannot simply cast from one to the other.

What did you expect to happen?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Simply, you can't. ArrayList and HashMap are not related. Either one is simply *not* IS-A the type of the other.

You will have to write a converter that will move the elements from collection to the other. And even with this, you will have to work out an algorithm on what to do, since one is index based, and the other is key based.

Henry
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to add to what has been said, you need to remember that (ignoring primitive values for now) casting doesn't do anything at all to the object itself. It doesn't convert it from one type to another. Casting only affects the reference that is being used to access it.

So if you've got a List variable, and the object it references is actually an ArrayList you can cast the reference to an ArrayList. That's fine - you can now treat it like an ArrayList...but that only works because that's what it really is. If you try to cast an ArrayList to a HashMap, you've still got an ArrayList, but you're trying to point a HashMap reference at it. This doesn't make sense, which is why the compiler won't let you do it.

So what you really want to do is create a new HashMap, and then for every object in the ArrayList put a key-value pair in it. But you need to decide what the key and what the value is, because the computer can't guess for you.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote: . . . (ignoring primitive values for now) casting doesn't do anything at all to the object itself. . . .
I think it would have prevented a lot of confusion if they hadn't used the same word “cast” for a primitive cast and a reference‑type cast.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot automatically convert an object from one type to a totally different type by casting - that is not what casting is for.

Sometimes (on rare occasions) you have some object in your program, and you know better than the compiler what type of object it is. In such a case, you can use a cast to tell the compiler "I have this object here, and I want you to treat it as if it is of type X".

This will not do any kind of conversion from the object's actual type to type X. The only thing it does is to tell the compiler that it should not check the type. The type will still be checked, at runtime. If, at runtime, the object is not of the type that you're casting it to, you'll get a ClassCastException.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!