2014-11-04 107 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; 
} 

使用該集確保頻率當項目中存在重複時,檢查不會重複多次。