Re: How to compare two ArrayList of string?
Mike Schilling wrote:
"Patricia Shanahan" <pats@acm.org> wrote in message
news:f1ojni$9a8$1@ihnp4.ucsd.edu...
John wrote:
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)) )
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