aspose file tools*
The moose likes Java in General and the fly likes Linked List equals method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Linked List equals method" Watch "Linked List equals method" New topic
Author

Linked List equals method

Vivek Singh
Ranch Hand

Joined: Oct 27, 2009
Posts: 92
shivendra tripathi wrote:Vivek your code won't work. It will give false even for {1,2,2,3} vs. {1,2,2,3}.


Yes you are right..

Rahul.p Kumar wrote: then best solution I can think of is use hashmap with key as unique element of corresponding list and value as no of repeating of that element. Compare two keysets for size first, if fail then fail all round, if true compare values of each key [sigh]

What will be your keyset.??
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
you can get keyset of map keys by calling its keySet()
Vivek Singh
Ranch Hand

Joined: Oct 27, 2009
Posts: 92
Rahul.p Kumar wrote:you can get keyset of map keys by calling its keySet()


Hey check this:-


This will work for all cases.
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
Vivek this wil work fine and probably better approch than suggested by me


SCJP 1.5(97%) My Blog
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

A good solution what I can suggest here is :

Repeat for each Linked List
1. Convert your linked list into an array by using <listObject>.toArray() method which returns an Object[]
2. Sort the array using Arrays.sort(Object[]) with the array you have obtained on step #1

After getting the sorted array equivalent of each linked list,
3. Invoke Arrays.equals(Object[] array1, Object[] array2) to get to know whether they are equal or not.


Note: The sorting is done based on the natural ordering of elements.


I guess for your linked list of numbers, it would be fine. (so long as it is of built in types like int, float, String etc., as they have their own ordering well defined in the corresponding classes).

As I am unable to provide the URL for the methods besides each method here, I am pasting those FYI here.

1. List.toArray() -> http://java.sun.com/j2se/1.5.0/docs/api/java/util/LinkedList.html#toArray()
2. Arrays.sort() -> http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arrays.html#sort(java.lang.Object[])
3. Arrays.equals() -> http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arrays.html#equals(java.lang.Object[], java.lang.Object[])

Hope this helps!


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

What vivek has given would be the best I would say -- using Collections.sort().

The conversion of Arrays is indeed doing the same but a little lengthier! - one of the other alternatives.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

I'm not really sure why everyone brought up sets, and hashs, and the containsAll() method. But my impression from Alex's first post was that he was supposed to CREATE a method to compare the two lists. When the word create usually appears in my assignments it means don't use built in methods, try to do it yourself. I still think the original while loop solution will work, it might take a little while, but it should work.

-Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Hunter McMillen wrote:I'm not really sure why everyone brought up sets, and hashs, and the containsAll() method. But my impression from Alex's first post was that he was supposed to CREATE a method to compare the two lists. When the word create usually appears in my assignments it means don't use built in methods, try to do it yourself. I still think the original while loop solution will work, it might take a little while, but it should work.

-Hunter


Yes indeed. I too mentioned the same in my very first post!
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I wonder if there is anything in java.util.Collections or java.util.Arrays that would help
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
what about set operations like retainAll and removeAll
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

In my opinion the test should not alter the original Lists, so any operation that would do so must use a defensive copy.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

David O'Meara wrote:In my opinion the test should not alter the original Lists, so any operation that would do so must use a defensive copy.


Exactly!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Linked List equals method