How to handle objects which were digged out of a hashtable?

Hi!

I store a couple of objects into a hashtable:

Hashtable cars =new Hashtable();

cars.put("red one",new car("red") )

cars.put("green one",new car("green") )

...

After that I want to reference a specific object and use its methods:

cars.get("red one").openDoor()

But this does not work, also

myCar=cars.get("red one");

does not work... "cannot convert from Object to car"

Is there any way to handle these Objects....

[743 byte] By [caroditaa] at [2007-11-27 4:06:27]
# 1

Are you using the current (J2SE 6) or the previous (5) version of Java?

Then write:

Map<Car> cars = new HashMap<Car>();

and you're laughing. (Don't understand those pointy brackets? http://java.sun.com/docs/books/tutorial/extra/generics/index.html)

By the way, the new generalized for plays well with generics:

//open door on all cars

for(Car c : cars)

c.openDoor();

If you are using an old version and unable to update, cast Object to Car:

Car myCar=(Car) cars.get("red one");

DrLaszloJamfa at 2007-7-12 9:11:34 > top of Java-index,Java Essentials,Java Programming...
# 2

> Are you using the current (J2SE 6) or the previous

> (5) version of Java?

> Then write:

> > Map<Car> cars = new HashMap<Car>();

>

sorry for a small correction

i believe it should be

Map<String,Car> cars = new HashMap<String,Car>();

@OP

If you are using jdk 1.4 or below this is how you he to access properties.

Hashtable cars = new Hashtable();

cars.put("red one", new car("red") )

cars.put("green one", new car("green"));

mycar = (Car)cars.get("red one");

// or mycar = (Car)cars.get(((Object)"red one"));

and if it is j2se 5.0+

Hashtable<String,Car> cars = new Hashtable<String,Car>();

cars.put("red one", new car("red") )

cars.put("green one", new car("green"));

mycar = cars.get("red one");

and a small advice

once if you are free it would be great if you can sit down and read bit on usage of generics in Java (from 1.5) as that would do a world of good.

Hope this might help :)

REGARDS,

RaHuL

RahulSharnaa at 2007-7-12 9:11:34 > top of Java-index,Java Essentials,Java Programming...
# 3
> sorry for a small correction> i believe it should be>> Map<String,Car> cars = new HashMap<String,Car>();That's for the save. It's early in the morning for me :-)
DrLaszloJamfa at 2007-7-12 9:11:34 > top of Java-index,Java Essentials,Java Programming...
# 4
> That's for the save. It's early in the morning for me> :-):) Hope you have a great day and weekend ahead....REGARDS,RaHuL
RahulSharnaa at 2007-7-12 9:11:34 > top of Java-index,Java Essentials,Java Programming...
# 5
Since I'm forced to use Java 1.4, the cast from Object to car was the solution for me...Thanks to all of you. Once again, this was extremely fast and well-fitting help. I wish SAP would be comparable....
caroditaa at 2007-7-12 9:11:34 > top of Java-index,Java Essentials,Java Programming...
# 6
> I wish SAP would be comparable....Then, as mentioned in a concurrent thread, SAP must match against< T extends Comparable < ? super T > >
DrLaszloJamfa at 2007-7-12 9:11:34 > top of Java-index,Java Essentials,Java Programming...
# 7

> Since I'm forced to use Java 1.4, the cast from

> Object to car was the solution for me...

Also, you should probably use the Map and HashMap (without the generics stuff) instead of Hashtable. If you really need the synchronization (that Hashtable has), lookup Collections.synchronizedMap. :)

doremifasollatidoa at 2007-7-12 9:11:34 > top of Java-index,Java Essentials,Java Programming...
# 8

> > Since I'm forced to use Java 1.4, the cast from

> > Object to car was the solution for me...

>

> Also, you should probably use the Map and HashMap

> (without the generics stuff) instead of Hashtable.

> If you really need the synchronization (that

> Hashtable has), lookup Collections.synchronizedMap.

> :)

Well as far as i think Hastable is not all of being sychronized but it does not allow you to insert null values which could well be running inside the mind OP .

So letz just it to him to decide of what would be the best suited to his requirement.

REGARDS,

RaHuL

RahulSharnaa at 2007-7-12 9:11:34 > top of Java-index,Java Essentials,Java Programming...
# 9

> > > Since I'm forced to use Java 1.4, the cast from

> > > Object to car was the solution for me...

> >

> > Also, you should probably use the Map and HashMap

> > (without the generics stuff) instead of Hashtable.

> > If you really need the synchronization (that

> > Hashtable has), lookup Collections.synchronizedMap. :)

>

>

> Well as far as i think Hastable is not all of being

> sychronized but it does not allow you to insert null

> values which could well be running inside the mind OP .

> So letz just it to him to decide of what would be the

> best suited to his requirement.

>

> REGARDS,

> RaHuL

Yes, the OP has the final decision of requirements, and he didn't state what those requirements are. I never said he had to use HashMap--I was just giving him more information about possibilities. He can still decide what best suits him when he looks up HashMap (and ask questions if he doesn't know why HashMap might be a better choice than Hashtable). I wasn't the first to mention HashMap, but I did want to remind him that HashMap might be worth considering. If he doesn't know about casting objects, it is fair to assume that he probably isn't an expert in the differences between Hashtable and HashMap. He might have missed the mentioning of HashMap because the suggestion to cast works for Hashtable, too.

Null values may be a requirement, in which case Hashtable may be required. However, the suggestion to use Map ("code to the interface") and HashMap (more modern than Hashtable, and doesn't incur synchronization overhead if not needed) and Collections.synchronizedMap (synchronization along with more modern HashMap) is something he should consider if null values aren't required. He could use Map (again, "code to the interface") and Hashtable if he needs the null values. Many new Java coders on this forum use Hashtable and Vector simply because they've never heard of the newer HashMap and ArrayList. The newer ones may not meet all requirements, but it is fair to mention that they exist, in case the OP never heard of them at all. As you say, let the OP decide, but give him the information he needs for an informed decision (and if the info given leads to more questions in his mind, he can ask those questions [if he can't find the answers by searching/reading existing posts, API, etc.]).

Regards,

DoReMi

doremifasollatidoa at 2007-7-12 9:11:34 > top of Java-index,Java Essentials,Java Programming...