ArrayList containsAll返回错误的值

时间:2014-11-04 06:37:29

标签: java android string list

让我们说:

a = ["s", "i", "n", "e", "d"];
b = ["s", "e", "n", "d"];

ab的类型为List<String>

如何确定b中是否包含a中的所有字母? - 不一定按顺序排列(在这种情况下,[s,e,n,d]位于ab之内,它是真的)

使用a.containsAll(b)并不总是有效!

另一个例子:

a=["b", "a", "z", "z", "z"]
b=["a", "a", "b", "b"]

此处我希望结果为false,因为[a,a,b,b] a中的任何排序都不会显示a.containsAll(b),但使用true会返回{{1}} !

4 个答案:

答案 0 :(得分:2)

  

只需将每个的添加到新的String变量中即可   通过使用 .contains()

找到一个String变量值是否包含另一个String
List<String> a = ["b","a","n"];
List<String> b = ["b","a","n","a","n","a"];

String newA = null;
String newB = null;

for(String strA : a) {
  newA += strA;
}
for(String strB : b) {
  newB += strB;
}

if(newA.contains(newB))
  return True;
else
  return False;

Reference for String .contains()

答案 1 :(得分:2)

试试这个:

private boolean containsAll(List<?> a, List<?> b) {
    // List doesn't support remove(), use ArrayList instead
    ArrayList<Object> x = new ArrayList<Object>();
    ArrayList<Object> y = new ArrayList<Object>();

    x.addAll(a);
    y.addAll(b);
    for (Object o : y) {
        if (!x.remove(o)) // an element in B is not in A!
            return false;
    }
    return true;          // all elements in B are also in A
}

我们的目的是从b中移除a中的每个字母。当您尝试删除不在a中的字母时,它会确认a不包含b中的所有字母。

(如果元素存在,remove()将返回true,否则false

答案 2 :(得分:2)

从较大的列表中删除未出现在较小列表中并等于它们的所有元素。如果它们相等,则较小的列表包含在较大的列表中:

static List<String> list1 = new ArrayList<String>(){{
    add("b");
    add("a");
    add("n");
    add("z");
    add("z");
    add("z");
    }};
static List<String> list2 = new ArrayList<String>(){{
    add("b");
    add("a");
    add("n");
    add("a");
    add("n");
    add("a");
}};

public static void main(String[] args) {

    if(deepContains(list1, list2)) 
        System.out.println("List2 is contained in List1"); 
}

public static boolean deepContains(List<String> one, List<String> two){     
    if (one == null && two == null){
        return true;
    }

    if((one == null && two != null) 
      || one != null && two == null){
        return false;
    }

    //to avoid messing the order and elements of the lists we will use a copy
    one = new ArrayList<String>(one);
    two = new ArrayList<String>(two);
    //This removes from one all the elements not contained in two
    one.retainAll(two); 
    int a = one.size();
    int b = two.size();

    //one has lesser elements than two, for sure two is not contained in one
    if(a < b) return false;

    //one has the same number of elements of two, check if they are the same
    if(a == b){
        Collections.sort(one);
        Collections.sort(two);      
        return one.equals(two); 
    }

    //one has more elements than two. Remove duplicate elements
    //and check for equality
    Set<String> set1 = new HashSet<String>(one);
    Set<String> set2 = new HashSet<String>(two);

    if(set1.size() == set2.size()){
        one = new ArrayList<String>(set1);
        two = new ArrayList<String>(set2);
        Collections.sort(one);
        Collections.sort(two);      
        return one.equals(two); 
    }
    return false;
}

答案 3 :(得分:1)

这是适用于任何类型的任何集合的版本:

private <E> boolean containsAllIncludingDuplicates(Collection<E> container,
        Collection<E> items) {

    Set<E> checkedItems = new HashSet<>();
    for (E item : items) {
        if (checkedItems.add(item)
                && Collections.frequency(container, item) < Collections
                        .frequency(items, item)) {
            return false;
        }
    }
    return true;
}

使用Set确保当项目中存在重复时,频次检查不会重复多次。