Re: About vector.contains()
From: Adam P. Jenkins (adam_at_REMOVE-thejenkins-ME.org)
Date: 07/19/04
- Next message: Michelle: "Re: About vector.contains()"
- Previous message: Eric Sosman: "Re: How to not synchronize on an object already locked ?"
- In reply to: Eric Sosman: "Re: About vector.contains()"
- Next in thread: Michelle: "Re: About vector.contains()"
- Reply: Michelle: "Re: About vector.contains()"
- Reply: Sam: "Re: About vector.contains()"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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".
- Next message: Michelle: "Re: About vector.contains()"
- Previous message: Eric Sosman: "Re: How to not synchronize on an object already locked ?"
- In reply to: Eric Sosman: "Re: About vector.contains()"
- Next in thread: Michelle: "Re: About vector.contains()"
- Reply: Michelle: "Re: About vector.contains()"
- Reply: Sam: "Re: About vector.contains()"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|