Java 比较两个链表
在Java中比较两个链表的最佳方法是什么?我有两个链表,希望确保一个链表中的元素都不在另一个链表中。这两个列表都是本地日期列表Java 比较两个链表,java,linked-list,Java,Linked List,在Java中比较两个链表的最佳方法是什么?我有两个链表,希望确保一个链表中的元素都不在另一个链表中。这两个列表都是本地日期列表 boolean doesNotContain (LinkedList L1, LinkedList L2) { for(LocalDate d:L1) { if(l2.contains(d){ return false; } } ret
boolean doesNotContain (LinkedList L1, LinkedList L2) {
for(LocalDate d:L1) {
if(l2.contains(d){
return false;
}
}
return true;
}
您的解决方案具有N^2复杂性。如果对两个列表进行排序并在一个循环中迭代,则可以在N log N中执行int,这是排序的复杂性 您还可以创建两个包含两个列表中的元素的新集合,然后使用containsAll方法—它将完成所有工作。这是一个简洁易懂的解决方案。但可能会消耗内存 比较方法依赖于方法equals和hashCode-如果您没有在LocalDate类中正确重写它们,您将不会得到好的结果 BTW1:返回后中断是一个死代码 BTW2:使用not命名方法似乎是个坏主意。你很快就会发现自己在使用!不满足!不包含DFLASDF。如果你想弄清楚它的作用,那就祝你好运了。因为你的实现不涉及。。即将实施containsAll。。我建议使用这个方法。如果执行list1.2,实现将在list2的所有元素上循环,以检查list1中是否存在相等的对象。这就是您需要在LocalDate中重写公共布尔equalsObject obj的原因 下面是一个小示例,展示ContainesAll.所做的工作 运行检查的主要过程
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));
}
结果将是
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
如果重写equals方法,则出于演示目的,hashcode已被省略
@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;
}
输出将是
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
根据printed Object.hashcode,您可以很容易地看到containsAll方法在调用该方法的列表中的所有元素上循环
如果你想改进检查,你需要澄清以下几点
-列表中的元素是否唯一->最好使用集合
-列表中必须有相同数量的元素->如果是,第一步可以比较它们的大小
-如果只需要检查list2是否只有list1中的元素,则表示该列表包含唯一值->调用list2上的containsAll方法
-根据所包含的数据对列表进行排序也是值得的
如果没有这些信息,就不可能给出一个在所有情况下都是最好的建议。嘿,看看这个,你能稍微解释一下吗?我不确定你为什么需要覆盖?从列表中列出一个哈希集。
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