有没有一种方法可以在Java中从列表中移除子列表?

3

我有一个列表的结构,其中包含其他列表,如果其中一个列表包含在另一个列表中,我想要将其删除。

例如,给定以下的一个列表:

listOfLists = { {C1, C2, C3},
                  {C1, C2, C3, C4, C5, C6},
                  {C1, C2, C3, C4, C5, C6, C7} }

因此,{C1, C2, C3}{C1, C2, C3, C4, C5, C6}应该被删除,因为它们已经包含在另一个列表{C1, C2, C3, C4, C5, C6, C7}中。最终,删除后我的新的listOfLists将变成下面给出的示例;
listOfLists = { {C1, C2, C3, C4, C5, C6, C7} }

总之,是否有Java内置方法或一种方法可以删除子列表。

谢谢


你是如何将列表存储在ListOflists中的?但如果它是嵌套列表,你可以使用索引将其移除。 - Seek Addo
这些是数组,而不是 List.class。由于一个是类而另一个是原始结构,因此数组和列表的处理方式略有不同,其中一个具有方法,因为它是一个类,而另一个则通过类具有实用程序方法或者您必须手动创建函数。你甚至看过 Arrays 类吗?Arrays Java API - Mr00Anderson
@SeekAddo 值存储在类似于 List<List<>> 的结构中。按索引删除并不是问题,但如何找到这些子列表,然后将它们删除才是关键。 - Ekin
2个回答

2
你可以使用List::containsAll,假设你正在使用List<List<>>
List<List<C>> result = new ArrayList<>();

outerloop:
for(List<C> list1 : listOfLists) {
    for(List<C> list2 : listOfLists) {
        if(list1 != list2) {
            if(list2.containsAll(list1)) {
                continue outerloop; // list1 is a sub-list of list2, continue without adding
            }
        }
    }
    result.add(list1); // only adds if list1 is not contained by any other list.
}

请注意,如果您有等效的列表,则两者都将被删除。如果您不想这样做,您应该将引用比较 (list1 != list2) 改为 !list1.equals(list2)


我明白了,这非常有用。我猜诀窍在于"containsAll()"方法。 - Ekin

2
我认为没有内置的方法可以完全做到您想要的,但使用 containsAll() 并不难实现:

使用您提供的值,这里有一个快速示例来展示如何识别子列表或相等列表:

public static void main(String[] args){
     List<Integer> listOne = new ArrayList<>(Arrays.asList(1,2,3));
     List<Integer> listTwo = new ArrayList<>(Arrays.asList(1,2,3,4,5,6));
     List<Integer> listThree = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7));
     List<Integer> listFour = new ArrayList<>(Arrays.asList(1,2,3,4,5,7,6));
     List<List<Integer>> listOfLists = new ArrayList<>(Arrays.asList(listOne, listTwo, listThree, listFour));

        for(int currentIndex = 0; currentIndex < listOfLists.size(); currentIndex++) {
            List<Integer> currentList = listOfLists.get(currentIndex);
            for (int comparisonIndex = 0; comparisonIndex < listOfLists.size(); comparisonIndex++) {
                if(currentIndex == comparisonIndex) { continue; }
                List<Integer> comparisonList = listOfLists.get(comparisonIndex);
                if(comparisonList.containsAll(currentList)){
                    boolean isEqualSet = comparisonList.size() == currentList.size();
                    System.out.println(currentList + " is " + (isEqualSet ? "an equal set of: " : "a subset of: ") + comparisonList);
                    continue;
                }
            }
        }
    }


输出:

[1, 2, 3] is a subset of: [1, 2, 3, 4, 5, 6]
[1, 2, 3] is a subset of: [1, 2, 3, 4, 5, 6, 7]
[1, 2, 3] is a subset of: [1, 2, 3, 4, 5, 7, 6]
[1, 2, 3, 4, 5, 6] is a subset of: [1, 2, 3, 4, 5, 6, 7]
[1, 2, 3, 4, 5, 6] is a subset of: [1, 2, 3, 4, 5, 7, 6]
[1, 2, 3, 4, 5, 6, 7] is an equal set of: [1, 2, 3, 4, 5, 7, 6]
[1, 2, 3, 4, 5, 7, 6] is an equal set of: [1, 2, 3, 4, 5, 6, 7]

您可以根据您的标准存储列表的索引,然后在之后删除它们。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接