简体   繁体   English

检查“列表列表”中是否存在特定列表,与顺序无关

[英]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. 现在我要搜索如果上面的列表列表包含[8,1,4,3],我只是想忽略元素的顺序。 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. 遍历所有列表,对于每个列表,检查它们的长度与您要比较的列表的长度相同;如果是,则调用containsAll以检查它是否包含所有值。

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 我重命名了您的变量以符合Java命名约定

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 标准的String []不适用于此代码

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. 包含将只在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). 这可以通过在列表接口的任何实现类(在我的示例中为ArrayList)上使用.ContainsAll()方法来完成。 The parameter of ContainsAll() takes a Collection and ContainsAll() returns a boolean value. ContainsAll()的参数接受一个Collection,ContainsAll()返回一个布尔值。 Please see : List 请参阅: 列表

If you are already working with arrays you can convert them using Arrays.asList() 如果您已经在使用数组,则可以使用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. 编辑:如果您要更改数字以寻找不同的顺序,它将仍然返回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 因此,您要做的是要么计算列表中的每个数字并将其添加到HashMap中,要么按排序顺序比较两个列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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