[英]Compare two Linked Lists
What is the best way to compare two Linked lists in Java, I have two lists and want to make sure that none of the elements in one list are in the other. 比较Java中两个链接列表的最佳方法是什么,我有两个列表,并且要确保一个列表中的所有元素都不在另一个列表中。 would something like this work both lists are Lists of LocalDates. 这样的事情会两个列表都是LocalDates的列表。
boolean doesNotContain (LinkedList L1, LinkedList L2) {
for(LocalDate d:L1) {
if(l2.contains(d){
return false;
}
}
return true;
}
Your solution works in N^2 complexity. 您的解决方案的工作复杂度为N ^ 2。 If you sort both lists and iterate over them in one loop, you can do int in N log N (that is the sorting complexity). 如果对两个列表进行排序并在一个循环中对其进行迭代,则可以in N log N(即排序复杂度)进行int运算。
You can also create two new Set
s containing the elements from both lists, and then use containsAll()
method - it will do all the work. 您还可以创建两个包含两个列表中元素的新Set
,然后使用containsAll()
方法-它会完成所有工作。 It's a clean and easy to understand solution. 这是一个干净易懂的解决方案。 Might be memory consuming though. 虽然可能会占用大量内存。
Comparison methods depend on methods equals()
and hashCode()
- if you do not properly override them in your LocalDate
class, you will not get good results. 比较方法取决于方法equals()
和hashCode()
-如果您没有在LocalDate
类中正确覆盖它们,则不会获得良好的结果。
BTW1: break after return is a dead code BTW1:返回后中断是无效代码
BTW2: naming methods with "not" seems like a bad idea. BTW2:用“ not”命名方法似乎是个坏主意。 You will soon find yourself using !notFulfills( !notContains(sdflasdf))
. 您很快就会发现自己使用!notFulfills( !notContains(sdflasdf))
。 Goood luck trying to figure out what that does. 真想知道那是怎么做的。
As your implementation of doesNotContain(..)
is close to the implementation of the containsAll(..)
implementation I would propose to use that one. 由于您的doesNotContain(..)
实现接近于containsAll(..)
实现的实现,因此我建议使用该实现。 If you execute list1.containsAll(list2)
the implementation will loop over all elements of list2
to check if there is an equal object in list1
. 如果执行list1.containsAll(list2)
则实现将循环遍历list2
所有元素,以检查list1
是否存在相等的对象。 That's the reason you need to override public boolean equals(Object obj)
in LocalDate
. 这就是您需要在LocalDate
重写public boolean equals(Object obj)
的原因。
Find below a small example to show what's done by the containsAll(..)
在下面找到一个小示例,以显示containsAll(..)
the main procedure to run the check 运行检查的主要程序
public static void main(String[] args) throws Exception {
List<LocalDate> list1 = new LinkedList<>();
list1.add(new LocalDate("112233"));
list1.add(new LocalDate("223344"));
List<LocalDate> list2 = new LinkedList<>();
list2.add(new LocalDate("112233"));
list2.add(new LocalDate("112233"));
System.out.println("list1 = " + list1);
System.out.println("list2 = " + list2);
System.out.println("list1.containsAll(list2) = " + list1.containsAll(list2));
System.out.println("list2.containsAll(list1) = " + list2.containsAll(list1));
}
if you implement LocalDate
without overriding the equals method, equals
will be true only if you compare the references of the same object. 如果在不覆盖equals方法的情况下实现LocalDate
,则仅当您比较同一对象的引用时, equals
才为true。
// a naive implementation for demonstration purpose
class LocalDate {
String hour;
String minute;
String second;
LocalDate(String string) {
hour = string.substring(0, 2);
minute = string.substring(2, 4);
second = string.substring(4, 6);
}
@Override
public String toString() {
return String.format("LocalDate{%s%s%s} - hashcode: %d", hour, minute, second, this.hashCode());
}
}
the result will be 结果将是
list1 = [LocalDate{112233} - hashcode: 33039820, LocalDate{223344} - hashcode: 31311199]
list2 = [LocalDate{112233} - hashcode: 13177912, LocalDate{112233} - hashcode: 21924553]
list1.containsAll(list2) = false
list2.containsAll(list1) = false
if you override the equals method (hashcode has been omitted for demonstration purpose) 如果您覆盖equals方法(出于演示目的,哈希码已被省略)
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final LocalDate other = (LocalDate) obj;
System.out.println(toString() + ".equals(" + obj.toString() + ')');
if (!Objects.equals(this.hour, other.hour)) {
return false;
}
if (!Objects.equals(this.minute, other.minute)) {
return false;
}
if (!Objects.equals(this.second, other.second)) {
return false;
}
return true;
}
the output will be 输出将是
list1 = [LocalDate{112233} - hashcode: 33336787, LocalDate{223344} - hashcode: 12767201]
list2 = [LocalDate{112233} - hashcode: 31311199, LocalDate{112233} - hashcode: 13177912]
// generated output by the method containsAll(..)
LocalDate{112233} - hashcode: 31311199.equals(LocalDate{112233} - hashcode: 33336787)
LocalDate{112233} - hashcode: 13177912.equals(LocalDate{112233} - hashcode: 33336787)
list1.containsAll(list2) = true
// generated output by the method containsAll(..)
LocalDate{112233} - hashcode: 33336787.equals(LocalDate{112233} - hashcode: 31311199)
LocalDate{223344} - hashcode: 12767201.equals(LocalDate{112233} - hashcode: 31311199)
LocalDate{223344} - hashcode: 12767201.equals(LocalDate{112233} - hashcode: 13177912)
list2.containsAll(list1) = false
based on the printed Object.hashcode()
you can easily see that the containsAll() method loops over all elemnts in the list on which you call the method. 基于打印的Object.hashcode()
您可以轻松地看到containsAll()方法在调用该方法的列表中循环遍历所有元素。
If you want to improve the check you need to clarify first following points - are the elements in the list unique -> then better use a Set
- must the list have the same number of elements -> if yes you can compare as first step their sizes - if you need to check only that list2 has only elements which are in list1 (means the list contains unique values) -> call the containsAll method on list2 - it might also be worth to sort the lists before (depends on the contained data) 如果您想改进检查,则需要先阐明以下几点-列表中的元素是否唯一->然后最好使用Set
列表中的元素数必须相同->如果是,则可以将它们的第一步进行比较大小-如果您只需要检查list2仅包含list1中的元素(意味着列表包含唯一值)->在list2上调用containsAll方法-可能还值得对列表进行排序(取决于所包含的数据) )
Without those information it is not possible to give a suggestion which will be the best
in all cases. 没有这些信息,就不可能给出在所有情况下都是the best
的建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.