aspose file tools*
The moose likes Beginning Java and the fly likes comparing two ArrayLists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "comparing two ArrayLists" Watch "comparing two ArrayLists" New topic
Author

comparing two ArrayLists

Haani Naz
Greenhorn

Joined: May 30, 2010
Posts: 23
Hi all,

i'm not sure if this has already been covered, my search didn't really answer my question so figured i'll post it anway..apologies if its already been covered - if you can direct me, that'd be great!

i'm basically trying to compare two ArrayLists.

example:

the program i'm trying to create works well, "should work" in the following way..

step 1: get user input which is stored in ArrayList userInput
step 2. check if ArrayList hits (will be explained) has any elements from
userInput
step 3: if hits is equal to an element of userInput goto step1

hits will store certain values which needs to be compared to the elements in userInput. and if it exist, new input is needed else the program can continue.

i declared hits and userInput as ArrayList<String> objects and input is accepted in String format.

i.e:
hits : [0,4,6,7] << numbers are in string format
userInput:[1,6,8,9,0]

so at the beginning of the program (as above) hits should compare all its values to userInput.

i have tried hits.equals(userInput) which always returns "false" (i suspect its only comparing the two first elements?)

i did also try hit.contains(userInput) - same result.


can somebody show me how this is done correctly? as you can see i'm very new to java..thanks!
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

i have tried hits.equals(userInput) which always returns "false" (i suspect its only comparing the two first elements?)

Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface.

Think the definition is pretty clear .... Keywords to notice -- ordered


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

and welcome to javaranch
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
What are you storing in the list and have you overridden the equals() method
to compare the list entries? When the user completes input for each list item,
you can use a for-each loop to check for it in the list.

Jim ... ...


BEE MBA PMP SCJP-6
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

I can't tell precisely from your description what the nature of the comparison should be; but between the methods of the List interface itself, and the java.util.Collections utility class, you should be able to do whatever you want in a single line of code.

For example, to test whether hits and userInput have any elements in common:



To test whether every element in userInput is in hits, you might say:



There are plenty of other interesting methods in the Collections class and the List interface, so spend a few minutes reading through the javadoc.


[Jess in Action][AskingGoodQuestions]
Haani Naz
Greenhorn

Joined: May 30, 2010
Posts: 23
cheers for your replies guys! i'll check out collections! ..as a temporary fix i dumped hits and userInput ArrayLists into Arrays and compared the content as strings which seems to work.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: comparing two ArrayLists