简体   繁体   中英

Check If a particular list is present in “List of Lists” irrespective of order

I have a list of lists like

[[0, 5, 10, -1], [1, 8, 3, 4], [2, 9, 6, 7]]

Now I want to search If the above list of lists contain [8,1,4,3] I simply want to ignore the order of elements. How can I achieve this. ** I tried contains but it fails when the order is different.

Edit: The code I'm using

for(List<Integer> innerSList : PermutaionS_toDraw)
        {int elem_trans = 0;

        if(!Final.contains(innerSList))
        {
        Final.add(PermutaionS_toDraw.get(PermutaionS_toDraw.indexOf(innerSList)));
        }
}

Iterate over all the lists, for each list check they are the same length as the one you are looking to compare and if so call containsAll to check it contains all the values.

for(List<Integer> innerList : permutaionsToDraw){
    int elemTrans = 0;

    if(final.size() != innerList.size() || !final.containsAll(innerSList)) {
        final.add(permutaionsToDraw.get(permutaionsToDraw.indexOf(innerList)));
   }
}

Note

I renamed your variables to keep with Java naming conventions

As you stated, you'll want to do a contains, however I think you're doing it at the wrong level. Instead of seeing if the main list contains your sublist, you'd want to iterate through the main list and check if each of it's values contains all of your search value. For example,

for (List<String> subList : mainList) {
   if (subList.containsAll(searchList) {
      return true;
   }
}
return false;

This will loop through your main list and check if each of it's children contains all the values of what you're searching for. Hope this helps and that I've explained it clearly!

Note: This depends on the type of collections you are using. A standard String[] won't work with this code

Edit: To react to your posting of the code. I believe all you'd need to change is the contains to a contains all. Contains will only look for an exact match within Final, however contains all will compare the contents and check if that collection of integers is contained at all within Final. So, it should look something like this

List<List<Integer>> finalCopy = new LinkedList<>();
for(List<Integer> innerSList : PermutaionS_toDraw){
   for (List<Integer> subList : Final) {
      if(!subList.containsAll(innerSList)) {
         finalCopy.add(innerSList);
      }
   }
}
Final.addAll(finalCopy);

This can be done by using .ContainsAll() method on any Implementing classes of List Interface (ArrayList in my example). The parameter of ContainsAll() takes a Collection and ContainsAll() returns a boolean value. Please see : List

If you are already working with arrays you can convert them using Arrays.asList()

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test {



public static void main(String[] args) {
    List<Integer> integersList = new ArrayList<>();
    integersList.add(1);
    integersList.add(2);
    integersList.add(3);
    integersList.add(4);

    List<Integer> integersList2 = new ArrayList<>();
    integersList2.add(5);
    integersList2.add(6);
    integersList2.add(7);
    integersList2.add(8);

    List<Integer> integersList3 = new ArrayList<>();
    integersList3.add(1);
    integersList3.add(2);
    integersList3.add(3);
    integersList3.add(4);

    List<List> listOfLists = new ArrayList<>();
    listOfLists.add(integersList);
    listOfLists.add(integersList2);
    listOfLists.add(integersList3);

    List<Integer> numbersToLookFor = new ArrayList<>();
    numbersToLookFor.add(1);
    numbersToLookFor.add(2);
    numbersToLookFor.add(3);
    numbersToLookFor.add(4);

    for (List l : listOfLists) {
        if (l.containsAll(numbersToLookFor)) {
            System.out.println(l + "does contain " + numbersToLookFor);
        }
    }

}
}

Output:

[1, 2, 3, 4] does contain [1, 2, 3, 4]
[5, 6, 7, 8] does NOT contain [1, 2, 3, 4]
[1, 2, 3, 4] does contain [1, 2, 3, 4]

Edit: if you were to change the numbers to look for to a different order it will still return true.

 //CHANGED from 1 2 3 4 --> 3 1 4 2
    List<Integer> numbersToLookFor = new ArrayList<>();
    numbersToLookFor.add(3);
    numbersToLookFor.add(1);
    numbersToLookFor.add(4);
    numbersToLookFor.add(2);

    for (List l : listOfLists) {
        if (l.containsAll(numbersToLookFor)) {
            System.out.println(l + " does contain " + numbersToLookFor);
        } else
            System.out.println(l + " does NOT contain " + numbersToLookFor);
    }
}

Output:

[1, 2, 3, 4] does contain [3, 1, 4, 2]
[5, 6, 7, 8] does NOT contain [3, 1, 4, 2]
[1, 2, 3, 4] does contain [3, 1, 4, 2]

This should do the trick. for every sublist this snippet checks if the searchlist is the same size, contains the same elements (in any order) and is not already in the Final list of lists.

for(List<Integer> innerSList : PermutaionS_toDraw)
        {    
        if(searchlist.size() == innerSList.size() && searchList.containsAll(innerSList)
           && !Final.contains(searchList))
        {
        Final.add(PermutaionS_toDraw.get(PermutaionS_toDraw.indexOf(innerSList)));
        }
}

If you want to check if the sublist contains elements regardless of order that is the same thing as checking if they have the same number of each element, or their sorted lists are equal. So what you want to do is either count each number in your list and add it to a HashMap, or compare the two lists in sorted order

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