Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Java - vector.contains

 
Thread Tools Search this Thread
Old 07-22-2004, 09:25 PM   #1
Default vector.contains


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
  Reply With Quote
Old 07-22-2004, 09:31 PM   #2
Jeff Kish
 
Posts: n/a
Default Re: vector.contains
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
  Reply With Quote
Old 07-22-2004, 09:37 PM   #3
Carl Howells
 
Posts: n/a
Default Re: vector.contains
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
  Reply With Quote
Old 07-22-2004, 11:07 PM   #4
Ryan Stewart
 
Posts: n/a
Default Re: vector.contains
"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
  Reply With Quote
Old 07-22-2004, 11:15 PM   #5
Carl Howells
 
Posts: n/a
Default Re: vector.contains
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
  Reply With Quote
Old 07-22-2004, 11:36 PM   #6
Roedy Green
 
Posts: n/a
Default Re: vector.contains
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
  Reply With Quote
Old 07-23-2004, 02:38 AM   #7
bort
 
Posts: n/a
Default Re: vector.contains
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
  Reply With Quote
Old 07-23-2004, 06:51 PM   #8
Jeff Kish
 
Posts: n/a
Default Re: vector.contains
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
  Reply With Quote
Old 07-23-2004, 07:25 PM   #9
Jeff Kish
 
Posts: n/a
Default Re: vector.contains
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
  Reply With Quote
Old 07-23-2004, 08:04 PM   #10
Roedy Green
 
Posts: n/a
Default Re: vector.contains
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
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46