• Post Reply Bookmark Topic Watch Topic
  • New Topic

how contains() method works in Vector  RSS feed

 
naidu rasagna
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have a student object and a vector with 2 student objects....
And if the student object exists in vector i have to display the student or else no need to display.I am using
VectorObject.contains(studentObject)
But it is always executing the else part.
How can i overcome this problem....

Thanks in advance....
 
dema rogatkin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does linear search using function equals(). So you need to be sure that StudentObject.equals(StudentObjectInVector)
 
Ryan McGuire
Ranch Hand
Posts: 1143
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by dema rogatkin:
It does linear search using function equals(). So you need to be sure that StudentObject.equals(StudentObjectInVector)


Just to expand on what dema wrote...

Vector uses the equals() method on each object it contains to see if it's the one being sought. The default implementation of equals() in Object is true iff the two objects are the same object. Some classes override equals() to make it return true if the two objects have the same value. For instance...



would print "true". However, unless your class overrides equals(), this...



...would most likely print "false", beacuse the compared objects are two different objects.

IF you're going to redefine equals(), keep these things in mind:
  • Make sure it's refexive. x.equals(x) is true
  • Make sure it's symmetric. x.equals(y) == y.equals(x)
  • Make sure it's transitive. if (x.equals(y) && y.equals(z)) then x.equals(z)
  • It's consitant. x.equals(y) returns the same value multiple times as long as no fields used in equals() is changed.
  • If x != null then x.equals(null) is false.
  • ALWAYS override hashcode() when you override equals(). Specifically, if x.equals(y) then x.hashcode() MUST == y.hashcode(). (The inverse doesn't have to be necessarily true.) Failure to do this will make things like HashMap fail.


  • Some people will tell you to make sure that multiple calls to hashcode must return the same value if none of the values used in equals() change, but that happens automatically if you verify the fourth and sixth items above.

    A reasonable implementation of equals() hashcode() might be something like this:


    Others might argue about how to make the "best" hashcode function. That's fine; this equals()/hashcode() pair "works", because it obeys the list above.

    Cool?
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    "rasagna,"

    You've been warned more than once about your display name. Please revise it immediately to meet the JavaRanch Naming Policy.

    Thanks,
    Marc
     
    dema rogatkin
    Ranch Hand
    Posts: 294
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Wow, Ryan
    Excellent post. I can only add that sometimes you can ignore some rules to get behavior matching some strange requirements.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!