![]() |
|
|
|
#1 |
|
If I write a class and have a vector containing various instantiations of it,
and I want to see if the vector contains another,i.e. theVector.contains(theClass) What do I need to overload/write/tweak to make sure "contains" finds the vector element if it is equal to the "theClass"? Is there some method I add to myClass to make the contains work? Thanks Jeff Kish Jeff Kish |
|
|
|
|
#2 |
|
Posts: n/a
|
On Thu, 22 Jul 2004 16:25:40 -0400, Jeff Kish <> wrote:
>If I write a class and have a vector containing various instantiations of it, >and I want to see if the vector contains another,i.e. >theVector.contains(theClass) >What do I need to overload/write/tweak to make sure "contains" finds the >vector element if it is equal to the "theClass"? > >Is there some method I add to myClass to make the contains work? > >Thanks >Jeff Kish I did some digging and found that contains relies on a "equals" function. Do I just need to define my own for my class for contains to work? Any guidelines here? Thanks Jeff Kish Jeff Kish |
|
|
|
#3 |
|
Posts: n/a
|
Jeff Kish wrote:
> On Thu, 22 Jul 2004 16:25:40 -0400, Jeff Kish <> wrote: > > >>If I write a class and have a vector containing various instantiations of it, >>and I want to see if the vector contains another,i.e. >>theVector.contains(theClass) >>What do I need to overload/write/tweak to make sure "contains" finds the >>vector element if it is equal to the "theClass"? >> >>Is there some method I add to myClass to make the contains work? >> >>Thanks >>Jeff Kish > > I did some digging and found that contains relies on a "equals" function. > Do I just need to define my own for my class for contains to work? > Any guidelines here? > Thanks > Jeff Kish Read up on the entry for equals() in the java.lang.Object class. It should give you all the information you need. Be sure to also read up on hashCode() at the same time... The two are related, and need to both be considered together. Carl Howells |
|
|
|
#4 |
|
Posts: n/a
|
"Carl Howells" <> wrote in message
news:... > Read up on the entry for equals() in the java.lang.Object class. It > should give you all the information you need. Be sure to also read up > on hashCode() at the same time... The two are related, and need to both > be considered together. > Which brings up a point of interest. How do all of you handle the hashCode method? Where I work, people almost always override equals and ignore hashCode. Ryan Stewart |
|
|
|
#5 |
|
Posts: n/a
|
Ryan Stewart wrote:
> Which brings up a point of interest. How do all of you handle the hashCode > method? Where I work, people almost always override equals and ignore > hashCode. > Any object that overrides equals in a meaningful way SHOULD override hashCode as well. If it doesn't, its implementation of hashCode is in violation of the method's contract. Whether that has immediate consequences depends on what is done with the object, but the potential for problems in the future is present. It's best to follow the basic rule that's given everywhere: If you override equals, override hashCode to obey the method's contract, as listed in the documentation for java.lang.Object. Carl Howells |
|
|
|
#6 |
|
Posts: n/a
|
On Thu, 22 Jul 2004 17:07:54 -0500, "Ryan Stewart"
<> wrote or quoted : >Where I work, people almost always override equals and ignore >hashCode. Sun's docs say they must work in sync. Perhaps people have just ignored the problem and empirically noticed most of the time they get away with it. Not good from a maintenance point of view. It is sort of like setting a mousetrap. -- Canadian Mind Products, Roedy Green. Coaching, problem solving, economical contract programming. See http://mindprod.com/jgloss/jgloss.html for The Java Glossary. Roedy Green |
|
|
|
#7 |
|
Posts: n/a
|
In that class that you've created, you'll need to override the equals method
to define what conditions need to be met in order for two objects of this class to be considered equal. bort "Jeff Kish" <> wrote in message news:... > On Thu, 22 Jul 2004 16:25:40 -0400, Jeff Kish <> wrote: > > >If I write a class and have a vector containing various instantiations of it, > >and I want to see if the vector contains another,i.e. > >theVector.contains(theClass) > >What do I need to overload/write/tweak to make sure "contains" finds the > >vector element if it is equal to the "theClass"? > > > >Is there some method I add to myClass to make the contains work? > > > >Thanks > >Jeff Kish > I did some digging and found that contains relies on a "equals" function. > Do I just need to define my own for my class for contains to work? > Any guidelines here? > Thanks > Jeff Kish bort |
|
|
|
#8 |
|
Posts: n/a
|
On Thu, 22 Jul 2004 17:07:54 -0500, "Ryan Stewart" <>
wrote: >"Carl Howells" <> wrote in message >news:... >> Read up on the entry for equals() in the java.lang.Object class. It >> should give you all the information you need. Be sure to also read up >> on hashCode() at the same time... The two are related, and need to both >> be considered together. >> >Which brings up a point of interest. How do all of you handle the hashCode >method? Where I work, people almost always override equals and ignore >hashCode. > Can anyone give me a hint on exactly what to do with hashcode, being the sorry rookie I feel like? Thanks Jeff Kish Jeff Kish |
|
|
|
#9 |
|
Posts: n/a
|
On Thu, 22 Jul 2004 17:07:54 -0500, "Ryan Stewart" <>
wrote: >"Carl Howells" <> wrote in message >news:... >> Read up on the entry for equals() in the java.lang.Object class. It >> should give you all the information you need. Be sure to also read up >> on hashCode() at the same time... The two are related, and need to both >> be considered together. >> >Which brings up a point of interest. How do all of you handle the hashCode >method? Where I work, people almost always override equals and ignore >hashCode. > Somebody told me this: "Overriding hashcode is only necessary if you are going to put the vector into another container (primarily HashTables/HashMaps). It is usually used just to provide a unique key for those types of structures" is this inaccurate? Thanks Jeff Kish Jeff Kish |
|
|
|
#10 |
|
Posts: n/a
|
On Fri, 23 Jul 2004 13:51:34 -0400, Jeff Kish <>
wrote or quoted : >Can anyone give me a hint on exactly what to do with hashcode, being the sorry >rookie I feel like? for a start, read http://mindprod.com/jgloss/hashcodeh.html This is general advice. If you want an introduction to something vaguely concerning Java, look up the word in the Java glossary. -- Canadian Mind Products, Roedy Green. Coaching, problem solving, economical contract programming. See http://mindprod.com/jgloss/jgloss.html for The Java Glossary. Roedy Green |
|
![]() |
| Thread Tools | Search this Thread |
|
|