简体   繁体   中英

Is there a way to remove sublists from a list of lists in Java?

I have structure of list of lists and I want to remove a list if it is contained in another one.

For example, I have a list of lists as given below:

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

Therefore, {C1, C2, C3} and {C1, C2, C3, C4, C5, C6} should be removed, because it is already contained by another list {C1, C2, C3, C4, C5, C6, C7} . In the end, my new listOfLists becomes the example given below after removal;

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

To sum up, is there a Java built-in method or a way that could remove the sublists.

Thanks

You could use List::containsAll , assuming you're using 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.
}

Just note that if you have equivalent lists, they will both be removed. If you don't want that, you should change the reference comparison ( list1 != list2 ) to !list1.equals(list2) .

I don't think there is a built in method to do exactly what you want, but it's not too difficult to implement using containsAll() :

Using the values you've provided here's a quick example to show how to identify the sub/equal lists:

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;
                }
            }
        }
    }


Output:

 [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] 

You could store the indexes of the lists depending on your criteria and remove them afterwards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM