Java Forum / General / May 2007
How to compare two ArrayList of string?
John - 08 May 2007 01:47 GMT 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) ......
Stefan Ram - 08 May 2007 02:06 GMT >arraylist1==arraylist2 java.util.Arrays.equals ( arraylist1.toArray( new java.lang.String[ 0 ]), arraylist2.toArray( new java.lang.String[ 0 ]))
Matt Humphrey - 08 May 2007 02:19 GMT | If I have two ArrayList of String of the same number of elements, can I do | comparison? [quoted text clipped - 3 lines] | | 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 matth@ivizNOSPAM.com http://www.iviz.com/
Patricia Shanahan - 08 May 2007 02:27 GMT > If I have two ArrayList of String of the same number of elements, can I do > comparison? [quoted text clipped - 3 lines] > > 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
Red Orchid - 08 May 2007 07:42 GMT Patricia Shanahan <pats@acm.org> wrote or quoted in Message-ID <f1ojni$9a8$1@ihnp4.ucsd.edu>:
> 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>
Hendrik Maryns - 08 May 2007 10:27 GMT Red Orchid schreef:
> Patricia Shanahan <pats@acm.org> wrote or quoted in > Message-ID <f1ojni$9a8$1@ihnp4.ucsd.edu>: [quoted text clipped - 17 lines] > } > </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
Piotr Kobzda - 08 May 2007 13:26 GMT > I think that "new HashSet(arraylist2)" may be unnecessary overhead > (especially where the size of list is large). How about this ? [quoted text clipped - 10 lines] > } > </code> That method is incorrect. The following is 'true':
isEqual(Arrays.asList("a", "b"), Arrays.asList("a"))
piotr
Red Orchid - 08 May 2007 14:03 GMT Piotr Kobzda <pikob@gazeta.pl> wrote or quoted in Message-ID <f1pq95$oul$1@inews.gazeta.pl>:
> 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.
Piotr Kobzda - 08 May 2007 14:09 GMT > Piotr Kobzda <pikob@gazeta.pl> wrote or quoted in > Message-ID <f1pq95$oul$1@inews.gazeta.pl>: [quoted text clipped - 9 lines] > 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
Red Orchid - 08 May 2007 15:21 GMT Piotr Kobzda <pikob@gazeta.pl> wrote or quoted in Message-ID <f1psq4$6jv$1@inews.gazeta.pl>:
> 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).
Mike Schilling - 08 May 2007 09:06 GMT >> If I have two ArrayList of String of the same number of elements, can I >> do comparison? [quoted text clipped - 14 lines] > > 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.
Chris Dollin - 08 May 2007 10:03 GMT >>> If I have two ArrayList of String of the same number of elements, can I >>> do comparison? [quoted text clipped - 17 lines] > 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?
 Signature Scoring, bah. If I want scoring I'll go play /Age of Steam/.
Hewlett-Packard Limited Cain Road, Bracknell, registered no: registered office: Berks RG12 1HN 690597 England
Hendrik Maryns - 08 May 2007 10:25 GMT Chris Dollin schreef:
>>>> If I have two ArrayList of String of the same number of elements, can I >>>> do comparison? [quoted text clipped - 21 lines] > > 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
Patricia Shanahan - 08 May 2007 14:34 GMT >>> If I have two ArrayList of String of the same number of elements, can I >>> do comparison? [quoted text clipped - 17 lines] > 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
Mike Schilling - 08 May 2007 07:52 GMT > 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.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|