wood burning stoves 2.0*
The moose likes Java in General and the fly likes dynamic (?!) casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "dynamic (?!) casting" Watch "dynamic (?!) casting" New topic
Author

dynamic (?!) casting

Fred Close
Ranch Hand

Joined: Mar 13, 2001
Posts: 42

Hi,
I don't really know where to look for the problem I'm having right now ...
so let me explain
I've got a method
public void doSomething(Object object)
{
HashMap hMap = new HashMap();
(...)
get and id : anId
(...)
hMap.put(anId, object);
}

instead of putting in the hashmap the object as an object I would like to put it in the hashmap as the real type it is
I tried
Class klass = object.getClass();
hMap.put(anId, (klass)object);

but that doesn't compile ...
is my problem clear ? can somebody help me ?
thanks
FREd
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
You can only put an Object into the HashMap. When you get it back out, you'll need to cast it to what it really is.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Fred Close
Ranch Hand

Joined: Mar 13, 2001
Posts: 42

thanks Ron for this answer :
the reason why I wanted to put the real type of the object in the hashmap was the following :
I had an hashmap filled with Integer as key and PersonBean as value
when I tried to do :
PersonBean pBean = new PersonBean("1", "jan", "raoul");
hMap.containsValue(pBean) returns false !!!?
although an object PersonBean has been added to the hashMap !
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Can you show more of your code?
Certainly an object that you've just now created isn't going to be contained in the map as a value.
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
Here's what you do...
Let's say you are putting a java.lang.String in your Hashmap:

Then to get the value later...

The .get() method returns a generic Object reference, so you must tell the compiler what specific kind of object it is. That's done by casting it using (String)myObject.


Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
Fred Close
Ranch Hand

Joined: Mar 13, 2001
Posts: 42

Thanks Blake, that's what I did (but thought it was not good practice) iterate through the Set of values of the hashmap cast to the correct type and test with the equals(Object obj) method
but I felt like the containsValue method of the hashMap should find out if an object is contained in the set of values ...
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
It should. Could you post some code that demonstrates where it fails?
Fred Close
Ranch Hand

Joined: Mar 13, 2001
Posts: 42

I have extended the DefaultTableModel swing class to be able to pass javabean to the model and add them to a jtable as a row
my model has a method in which I add the beans to an hashmap for example PersonBean
addBean(Object bean)
{
(...)
//add the object to an hashmap
hMap.put(anInteger, bean);
}
The object I add to the hashMap all respects the javabean specification :
for example :
public class PersonBean
{
private int id;
private String firstName;
private String lastName;
public PersonBean()
{
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getFirstName()
{
return firstName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String getLastName()
{
return lastName;
}
//test equality method
public boolean equals(java.lang.Object obj)
{
if(obj instanceof PersonBean && this.id == ((PersonBean)obj).getId())
return true;
else
return false;
}
}
so I though I could create a new PersonBean (newBean) which has the same value for the id attribute
and that
hMap.containsValue(newBean) would return me true

PS : it's not easy to send the entire code, but that's the general idea
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Is every value in the map a PersonBean? Do you ever subclass PersonBean?
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
Oops, I didn't see your code post before I posted this so nevermind...
If you want that to work, then I believe you need to override equals() and you should override hashCode().
The default behavior of Object.equals() returns true only if the two objects are the same (i.e. they point to the same place in memory). If you need a less discriminating comparison, then you need to override equals().
[ September 16, 2002: Message edited by: Blake Minghelli ]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
This should work and the preliminary tests I have run show that it works fine. We are going to need to see more code (preferably enough code to run to see how it isn't working) because you must be doing something wrong somewhere and everything looks fine in the code you showed us.
[ September 16, 2002: Message edited by: Thomas Paul ]
[ September 16, 2002: Message edited by: Thomas Paul ]

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
He did override equals(), but he did not override hashCode(). Could this be the real problem? Does containsValue() compare hash codes as an optimization, to avoid unneeded calls on equals() ?
[ September 16, 2002: Message edited by: Ron Newman ]
Fred Close
Ranch Hand

Joined: Mar 13, 2001
Posts: 42

Thanks for your answers ...
I think Ron found the problem ...
I subclass the bean :
I first receive a vector of bean (for example a Vector of PersonBean ...) which I then transform to an array of object with the Vector.toArray() method
and then I pass this array of Object to my Model
addBeanList(Ojbect[] beans)
{
for(int i=0;i<beans.length;i++)
{
addBean(beans[i]);
}
}

addBean(Object bean)
{
(...)
//add the object to an hashmap
hMap.put(anInteger, bean);
}
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
That in itself doesn't constitute "subclassing" PersonBean.
I'm just trying to look out for the possibility of an inconsistent equals() definition between PersonBean and some subclass of it.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Ron Newman:
He did override equals(), but he did not override hashCode(). Could this be the real problem? Does containsValue() compare hash codes as an optimization, to avoid unneeded calls on equals() ?
According to the API, it uses the equals() method.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Fred Close:
Thanks for your answers ...
I think Ron found the problem ...
I didn't see anything there that should cause a problem.
Fred Close
Ranch Hand

Joined: Mar 13, 2001
Posts: 42

indeed when I do simple examples ... it's working there's probably something wrong in my code
I'll keep you informed when I've found the bug
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
I should also point out that containsValue() is not an efficient thing to do regularly; its execution time is linear with respect to the size of the map. If you need to do this a lot, consider putting the values in a Set.
john guthrie
Ranch Hand

Joined: Aug 05, 2002
Posts: 124
was this resolved? i think the advice that he override hashCode() is correct. according to joshua bloch's 'effective java', overriding equals() without overriding hashCode() is suicidal, and i myself have encountered odd behavior when i ignored this advice.
Fred Close
Ranch Hand

Joined: Mar 13, 2001
Posts: 42

Hi guys,
I come back a bit ashamed to you ...
Doing simple test I saw that there's no problem with the HashMap.containsValue(object) method ... and that the only thing necessary is that your object implements the equals method ...
the problem I had in my code was that I was testing the containsValue but I should have tested the containsKey method !!!
and the containsKey method with my 'famous' PersonBean object only worked with
the equals method
and the hashCode() method implemented

so thanks again for your help ... hope you will not get mad at me
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Oh yes, if you're using an object as a hash key, and you have overridden equals(), then you absolutely must override hashCode().
But in all of the code samples you've posted, you have used your PersonBean as a value, not a key.
[ September 17, 2002: Message edited by: Ron Newman ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: dynamic (?!) casting
 
Similar Threads
how to access object inside static block
HashMap question
HashMap
Storing objects in hashmap
OOP-4 Lookup hashmap