How to compare two ArrayList of string?

J

John

If I have two ArrayList of String of the same number of elements, can I do
comparison?

I want them to be equal as long as they contain the same elements. I don't
care the order of the elements.

Is the statement possible: if (arraylist1==arraylist2) ......
 
S

Stefan Ram

John said:
arraylist1==arraylist2

java.util.Arrays.equals
( arraylist1.toArray( new java.lang.String[ 0 ]),
arraylist2.toArray( new java.lang.String[ 0 ]))
 
M

Matt Humphrey

| If I have two ArrayList of String of the same number of elements, can I do
| comparison?
|
| I want them to be equal as long as they contain the same elements. I don't
| care the order of the elements.
|
| Is the statement possible: if (arraylist1==arraylist2) ......

The == operator returns true only if both variables refer to the same
list--it does not attempt to determine if two distinct list have equivalent
contents. .equals () won't do you any good here either. If the lists have
the same length and you can remove all the items of the second list from the
first, they're equal. If there will never be identical elements in any list,
you can do something like:

Set s1 = new HashSet (arraylist1);
for (Object o : arraylist2) {
if (! s1.contains (o)) // Sets are definately not equal
s1.remove(o);
}

If there can be identical elements, you'll have to count them or otherwise
account for multiples
Map <Object,Integer> m = new HashMap <Object,Integer> ();
for (Object o : arraylist1) {
Integer countInt = m.get(o);
if (countInt == null) {
m.put (o, new Integer (1));
} else {
m.put (o, new Integer (1 + countInt.intValue()));
}
And then subtract the arraylist2 items int he same way. If any item can't
be subtracted, they're not equal.

Or, sort both lists (if the values can be ordered)
Collections.sort(arraylist1);
Collections.sort(arraylist2);
And compare each position. Any unequal item means the lists are not equal.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
P

Patricia Shanahan

John said:
If I have two ArrayList of String of the same number of elements, can I do
comparison?

I want them to be equal as long as they contain the same elements. I don't
care the order of the elements.

Is the statement possible: if (arraylist1==arraylist2) ......

No, that asks if the reference variables arraylist1 and arraylist2 are
equal. Two reference variables are equal if they are either both null or
refer to the same object.

Using the List equals method does not help because it takes order into
account.

How about set equality between sets containing their contents?

if( new HashSet(arraylist1).equals(new HashSet(arraylist2)) )

Patricia
 
R

Red Orchid

Message-ID said:
How about set equality between sets containing their contents?

if( new HashSet(arraylist1).equals(new HashSet(arraylist2)) )


I think that "new HashSet(arraylist2)" may be unnecessary overhead
(especially where the size of list is large). How about this ?

<code>
boolean isEqual(List<T> list1, List<T> list2) {

Set<T> s = new HashSet<T>(list1);

for (T elm : list2) {
if (!s.contains(elm)) return false;
}
return true;
}
</code>
 
M

Mike Schilling

John said:
If I have two ArrayList of String of the same number of elements, can I do
comparison?

I want them to be equal as long as they contain the same elements. I don't
care the order of the elements.

There are two basic approaches:

1.If there are no duplicate values, you can do:

if (new HashSet(arrlist1).equals(new HashSet(arrlist2))

2A. If you don't mind changing the order of the lists, sort them (with
Collections.sort()), and use equals() to compare the results..

2B. If you don't want to change the order, create new lists from your
existing lists and apply 2A.
 
M

Mike Schilling

Patricia Shanahan said:
No, that asks if the reference variables arraylist1 and arraylist2 are
equal. Two reference variables are equal if they are either both null or
refer to the same object.

Using the List equals method does not help because it takes order into
account.

How about set equality between sets containing their contents?

if( new HashSet(arraylist1).equals(new HashSet(arraylist2)) )

That reports that a list with two 'a's and one 'b' equals a list with one
'a' and two 'b's.
 
C

Chris Dollin

Mike said:
That reports that a list with two 'a's and one 'b' equals a list with one
'a' and two 'b's.

Those two lists "contain the same elements", viz `a` and `b`, so I'd
say the settification trick gets the right answer.

If not -- isn't there a Bag interface somewhere?
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Chris Dollin schreef:
Those two lists "contain the same elements", viz `a` and `b`, so I'd
say the settification trick gets the right answer.

If not -- isn't there a Bag interface somewhere?

Jakarta Commons Collections defines Bag and a lot of implementations.
(Unfortunately *still* not generified.)

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGQEIbe+7xMGD3itQRAg5wAJ9td9Cd+BCkAWsXJn4MqRRklf17WwCfRXVe
gHqzSjv8h10Aa95vTGYTNAg=
=rSYT
-----END PGP SIGNATURE-----
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Red Orchid schreef:
I think that "new HashSet(arraylist2)" may be unnecessary overhead
(especially where the size of list is large). How about this ?

<code>
boolean isEqual(List<T> list1, List<T> list2) {

Set<T> s = new HashSet<T>(list1);

for (T elm : list2) {
if (!s.contains(elm)) return false;
}
return true;
}
</code>

Why do you think this would be better? new HashSet(list) invokes
addAll(Collection<?>), so it cycles through the list, just like your
loop does. I doubt the overhead of creating a new object still makes a
significant difference in Java.

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGQEKGe+7xMGD3itQRArpjAJ0ZgdAUqP8GYtauVxL96ybVkL40MgCeMk17
juGUfvThG9nZHsUMXc7Bwrk=
=1RwB
-----END PGP SIGNATURE-----
 
P

Piotr Kobzda

Red said:
I think that "new HashSet(arraylist2)" may be unnecessary overhead
(especially where the size of list is large). How about this ?

<code>
boolean isEqual(List<T> list1, List<T> list2) {

Set<T> s = new HashSet<T>(list1);

for (T elm : list2) {
if (!s.contains(elm)) return false;
}
return true;
}
</code>

That method is incorrect. The following is 'true':

isEqual(Arrays.asList("a", "b"), Arrays.asList("a"))


piotr
 
R

Red Orchid

Message-ID said:
That method is incorrect. The following is 'true':

isEqual(Arrays.asList("a", "b"), Arrays.asList("a"))


OP wrote this:

"If I have two ArrayList of String of the same number of elements, ..."

From this, I supposed that list1's size is equal to the other.
it would be desirable that precondition checking is added.
 
P

Piotr Kobzda

Red said:
OP wrote this:

"If I have two ArrayList of String of the same number of elements, ..."

From this, I supposed that list1's size is equal to the other.
it would be desirable that precondition checking is added.

OK, what about that:

isEqual(Arrays.asList("a", "b"), Arrays.asList("a", "a"))

?


piotr
 
P

Patricia Shanahan

Mike said:
That reports that a list with two 'a's and one 'b' equals a list with one
'a' and two 'b's.

Yes, it tests for set equality between sets containing the lists'
contents, not bag equality. Only the OP knows which is required, or
indeed whether there can be the duplicates that make them different.

Patricia
 
R

Red Orchid

Message-ID said:
isEqual(Arrays.asList("a", "b"), Arrays.asList("a", "a"))

If duplicate values are allowed, "isEqual" is incorrect.

With duplicate values, if OP's "... they contain the same elements .. "
means that {"a", "b", "b"} is equal to {"a", "a", "b"}, hash set way will
be good.

But, if not equal, the sorting way will be good.
(For example, Mike Schilling's suggestion in this thread).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top