• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Linked List equals method

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.??
 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can get keyset of map keys by calling its keySet()
 
Vivek Singh
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rahul.p Kumar wrote:you can get keyset of map keys by calling its keySet()



Hey check this:-


This will work for all cases.
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vivek this wil work fine and probably better approch than suggested by me
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!

 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wonder if there is anything in java.util.Collections or java.util.Arrays that would help
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what about set operations like retainAll and removeAll
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
reply
    Bookmark Topic Watch Topic
  • New Topic