[英]Find (Reduce) proper subsets of lists from list of lists in java
I want to get something like this below:我想在下面得到这样的东西:
//Input:
{
"paths": [
[
"A","B"
],
[
"A","B","C"
],
[
"A","C"
],
[
"A","D"
],
[
"A"
]
]
}
//Output:
{
"paths": [
[
"A","B","C"
],
[
"A","D"
]
]
}
What my code looks like:我的代码是什么样的:
List<LinkedList<String>> paths = new LinkedList<>();
//...
//some process that adds elements to paths.
paths.sort(Comparator.comparingInt(LinkedList::size));
List<LinkedList<String>> uniquePaths = new ArrayList<>();
for (int i = 0; i < paths.size(); i++) {
boolean unique=true;
for (int j = i+1 ; j < paths.size(); j++) {
if (paths.get(j).containsAll(paths.get(i))){
unique=false;
break;
}
}
if(unique) {
uniquePaths.add(paths.get(i));
}
}
But this logic seems a bit off.(This is resulting in something like: [[A][A,D][A,B,C]]
) Please help with some better way of solving this.但是这个逻辑似乎有点不对劲。(这会导致类似:
[[A][A,D][A,B,C]]
)请帮助解决这个问题。
Similar Question but in R: How to reduce (subset) list of lists?类似的问题,但在 R: 如何减少(子集)列表列表?
You're not checking each combination against the whole data set.您没有根据整个数据集检查每个组合。 Your mistake is rooted in the nested loop, where iteration starts from the index
j=i+1
.您的错误源于嵌套循环,其中迭代从索引
j=i+1
开始。
Since performance of the containsAll()
depends on the collection that is passed as an argument, it makes sense to generate a List of Sets out of the given list of list.由于
containsAll()
的性能取决于作为参数传递的集合,因此从给定的列表列表中生成集合列表是有意义的。
Here's how it can be implemented using Stream API.下面是如何使用 Stream API 来实现它。
List<List<String>> source = List.of(
List.of("A", "B"), List.of("A", "B", "C"),
List.of("A", "C"), List.of("A", "D"), List.of("A")
);
List<Set<String>> sets = source.stream()
.map(HashSet::new)
.collect(Collectors.toList());
List<List<String>> result = sets.stream()
.filter(set -> sets.stream().filter(s -> !s.equals(set)).noneMatch(s -> s.containsAll(set)))
.map(ArrayList::new)
.collect(Collectors.toList());
System.out.println(result);
Output: Output:
[[A, B, C], [A, D]]
Note that we need to exclude the current combination of strings while checking whether it is a subset of any of the other elements or not, see filter(s ->.s.equals(set))
in the code above.请注意,我们需要排除当前字符串组合,同时检查它是否是任何其他元素的子集,请参阅上面代码中的
filter(s ->.s.equals(set))
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.