【发布时间】:2017-04-23 08:35:38
【问题描述】:
给定 n 个列表,我想知道是否有两个列表具有完全相同的元素。以下是 Java 7 中的代码。
public static <T> boolean hasduplicateList(List<List<T>> lists) {
for (List<T> outerList : lists) {
int count = 0;
Set<T> outerSet = new HashSet<>(outerList);
for (List<T> innerList : lists) {
Set<T> innerSet = new HashSet<>(innerList);
if (outerSet.equals(innerSet)) {
count++;
}
if (count == 2) {
return true;
}
}
}
return false;
}
有没有更好的方法来专门使用 Java8 来实现?
【问题讨论】:
-
它们是否需要以相同的顺序才能被视为重复?
-
@shmosel 根据 OP 代码,它们不应该(OP 在比较它们之前将列表转换为集合)。
-
@Eran 对。如果 OP 确认,我将删除我的答案。
-
@Eran 你说得对。修复它
-
@shmosel 不,顺序无关紧要
标签: java list arraylist java-8 duplicates