【问题标题】:Is there a way to remove sublists from a list of lists in Java?有没有办法从 Java 列表中删除子列表?
【发布时间】:2016-06-08 17:48:51
【问题描述】:

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

例如,我有一个列表,如下所示:

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 中的,但如果是嵌套列表,您可以使用索引将其删除
  • 这些是数组,而不是 List.class。考虑到一个有方法,数组和列表的处理方式略有不同,因为它是类与原始结构,它通过类具有实用方法,或者您必须手动为其创建函数。你有没有看过Arrays这个班级? Arrays Java API
  • @SeekAddo 这些值存储在类似 List> 的结构中。按索引删除实际上不是问题,而是如何找到这些子列表,然后将它们删除。

标签: java algorithm list sublist


【解决方案1】:

您可以使用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()”方法。
【解决方案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]

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多