Re: About vector.contains()

From: Adam P. Jenkins (adam_at_REMOVE-thejenkins-ME.org)
Date: 07/19/04


Date: Mon, 19 Jul 2004 13:47:58 -0400


"Eric Sosman" <Eric.Sosman@sun.com> wrote in message
news:40FBFDC0.4060106@sun.com...
> Michelle wrote:
> > Hi all,
> > in the following simple java program, I expected a "true"
> > should be returned but I got a false instead. Seems that the
> > contains method of vector compare memory addresses instead
> > of contents.. (not sure about this).. Can I force the contains()
> > to check contents instead?
> >
> > Thanks.
> >
> > import java.util.*;
> >
> > public class Test5 {
> > public static void main (String[] args) {
> > Vector v = new Vector();
> >
> > String[] v1 = {"a1", "a2"};
> > String[] v2 = {"b1", "b2"};
> >
> > v.add(v1);
> > v.add(v2);
> >
> > String[] v3 = {"b1", "b2"};
> > if (v.contains(v3))
> > System.out.println("true");
> > else
> > System.out.println("false");
> > }
> > }
>
> You and I go to the bookstore, where each of us buys
> a copy of "Infective Java." The two books are, of course,
> identical in all respects: Same cover design, same page
> count, same content on corresponding pages, and so on.
> If I put my book in my briefcase, does my briefcase now
> contain your book?
>
> "Equality" and "identity" are different notions.

That's not a very helpful, or even correct response. Vector.contains(obj)
just uses obj.equals() for the comparisons, it doesn't explicitly use
identity or equality comparisons. The default Object.equals() method just
compares identity, but other equals() methods usually use some other form of
equality. So the original poster isn't wrong in principle. It's just that
in this case the objects in the Vector are arrays, and arrays just use the
default Object.equals() implementation which compares identity.

To the original poster: when you call v.contains(v3), the contains method
returns true if v3.equals(obj) is true for any obj in v. So if you're going
to use the contains() method, the equals() method of the objects in the
Vector has to do what you want. The problem here is that Java native arrays
just use the default Object.equals() method, which compares identity as you
noted. java.util.List implementations on the other hand, are required to
implement equals() method that does an element comparison as you want. So
if you wrap your arrays in a List then your program will work. For example:

import java.util.*;

 public class Test5 {
    public static void main (String[] args) {
       Vector v = new Vector();

       List v1 = Arrays.asList(new String[]{"a1", "a2"});
       List v2 = Arrays.asList(new String[]{"b1", "b2"});

       v.add(v1);
       v.add(v2);

       List v3 = Arrays.asList(new String[]{"b1", "b2"});
       if (v.contains(v3))
          System.out.println("true");
       else
          System.out.println("false");
    }
 }

This will print "true".



Relevant Pages

  • Re: InetAddress.equals() Anomoly?
    ... Compares this object against the specified object. ... array components is the same for the byte arrays. ... > If you consider that two InetAddress should be equal as soon as they have ... If you start overriding equals ...
    (microsoft.public.dotnet.vjsharp)
  • Re: About vector.contains()
    ... but other equals() methods usually use some other form ... So the original poster isn't wrong in principle. ... > in this case the objects in the Vector are arrays, ... > default Object.equalsimplementation which compares identity. ...
    (comp.lang.java.programmer)
  • Re: [PHP] in_array() related problem
    ... I really forgot the old PHP4 way and thought PHP5 compares ... To not flooding the bug tracking system I hope ... >> arrays for a match, not to mention those are object properties, not ...
    (php.general)
  • Re: equals vs ==
    ... == compares identity, equalscompares equality. ... references referring to it. ... equals() will return also true if the ... String overrides it by comparing the contents of the string. ...
    (comp.lang.java.programmer)
  • Re: String comparison with "==" is not reliable?
    ... .equalscompares the actual string value ... By default, equals() compares references, not values of the objects themselves ("string" or otherwise). ... Only if equalsis overridden does its behavior change, and then to that of the override, which is entirely up to the programmer. ...
    (comp.lang.java.programmer)