Question:
How do I compare two linked lists(java)?
anonymous
2008-02-25 11:29:45 UTC
is there a method that instantly compares them? or should i go and compare every node with a loop?

do equal lists have to mean that the elements are arranged exactly the same?

ex.
is 1-2-3 equal to 1-3-2?
Three answers:
stephene
2008-02-25 11:39:53 UTC
Here is the explanation from the API of the LinkedList equals method.



http://java.sun.com/j2se/1.4.2/docs/api/java/util/AbstractList.html#equals(java.lang.Object)



===============

public boolean equals(Object o)



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 implementation first checks if the specified object is this list. If so, it returns true; if not, it checks if the specified object is a list. If not, it returns false; if so, it iterates over both lists, comparing corresponding pairs of elements. If any comparison returns false, this method returns false. If either iterator runs out of elements before the other it returns false (as the lists are of unequal length); otherwise it returns true when the iterations complete.

=============



If your linked lists contain objects that implement the equals method such that the equals method compares values (Example Strings) then calling linkedList.equals(otherList) will work fine.



However, if you are putting custom objects into your linked lists you need to make sure that objects of those type implement the equals() method. By default, equals() (which is inherited from Object) just checks to see if that object is the same object.



Example:

CustomObject o1 = new CustomObject(1, 2);

CustomObject o2 = new CustomObject(1, 2);

CustomObject o3 = o1;



o1.equals(o2) //FALSE

o1.equals(o3) //TRUE



If your CustomObject implements the equals method such that it compares certain values (like the constructor args, in this example). Then you can make it so that

o1.equals(o2) //TRUE



And then those objects would work just fine if you called the equals() method on the LinkedList.
Kaydell
2008-02-25 11:52:07 UTC
It is up to you as the programmer to decide what the definition of equal list is. It could be as easy as the following code if everything is setup:



boolean isEqual = list1.equals(list2);



The above line of code relies on the equals() method being implemented for all of the elements of the two lists and if the lists are subclasses, that you have overridden the equals() method to yield the results that you want.
?
2016-11-29 10:38:53 UTC
observe that some GE cameras seem to be clones of Fuj cameras. So in some circumstances, you're paying for a GE digital camera and getting a Fuji... or consistent with hazard you're paying for a Fuji and getting a GE.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...