2014-11-04 105 views
1

比方说:ArrayList中的containsAll返回错误值

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

ab的类型List<String>)的

我怎样才能确定是否b所有的字母都包含在a? - 不一定是为了

使用a.containsAll(b)并不总是有效(在这种情况下,由于[s,e,n,d]ab是真的)!

又如:

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

这里我要的结果是false因为[a,a,b,b]在任何种类的顺序a没有出现,但使用a.containsAll(b)将返回true

+0

那么为什么不匹配两个列表的大小。如果相等,则返回containsAll。 – 2014-11-04 06:40:03

+0

你是什么意思的“修复”?如果你想你自己的逻辑只是写一个函数,它会做 – 2014-11-04 06:40:09

+0

我想我的意思是有一种方法来改变containsAll ---就像一个。containsAll(b,1)其中1表示它将只扫描每个字母一次 – user2456977 2014-11-04 06:42:38

回答

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 
} 

这个想法是从a中删除b中的每个字母。当您尝试删除不在a中的字母时,则确认a不包含b中的所有字母。

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

+0

你的回答非常好!非常感谢。这非常有帮助。 – user2456977 2014-11-04 13:50:24

2

只需使用每个
你的整个字符串列表中添加新的字符串变量,并找到一个字符串变量的值包含其他字符串或没有,通过使用.contains()

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()

+0

这太棒了。谢谢:) – user2456977 2014-11-04 06:47:15

+0

但一个可能的问题:我不想检查字符串是否相等。我想检查一个字符串是否包含在另一个字符串 – user2456977 2014-11-04 07:01:38

+0

@ user2456977修改了我的答案,查看它。并遵循这个例子URL http://www.tutorialspoint.com/java/lang/string_contains.htm – 2014-11-04 07:11:35

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

除了最后一部分,你的方法是最好的。 set1.size并不总是等于set2.size。例如:a = [b,a,n,z,z,z,a,n,a]和b = [b,a,n,a,n,a]应该返回true,因为香蕉存在于a中。它不会在你的方法中返回true,因为删除重复的元素banz将出现在a中,并且禁止将出现在b中,并且这将导致它失败。 – Neil 2014-11-04 10:27:57

+0

upvote为您的方法的第一部分到最后一部分 – Neil 2014-11-04 10:50:04

+0

Z不会因为retainAll而存在,不是吗? – Narmer 2014-11-04 11:04:21

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

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