• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

dynamic (?!) casting

 
Ranch Hand
Posts: 42
Mac
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Fred Close
Ranch Hand
Posts: 42
Mac
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Fred Close
Ranch Hand
Posts: 42
Mac
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It should. Could you post some code that demonstrates where it fails?
 
Fred Close
Ranch Hand
Posts: 42
Mac
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is every value in the map a PersonBean? Do you ever subclass PersonBean?
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 42
Mac
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 42
Mac
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 42
Mac
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
If you believe you can tell me what to think, I believe I can tell you where to go. Go read this tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic