2011-05-17 89 views
8

如何知道java中兩個數組列表之間的不同元素?我需要的確切元素不是一個布爾值,可以使用removeAll()來檢索。查找Java中兩個ArrayList之間的不同元素

+5

請注意,'的removeAll()'** **變化的列表,你」重新調用該方法。所以,如果它返回「true」,那麼這意味着列表已被更改。所以這個方法並不是完全沒有用的或者什麼的。 – BalusC 2011-05-17 00:54:20

回答

1
LinkedHashMap table; 
for each element e of array A 
    if table.get(e) != null 
     table.put(e, table.get(e) + 1) 
    else 
     table.put(e, 0) 

//Do the same for array B 
for each element e of array B 
    if table.get(e) != null 
     table.put(e, table.get(e) + 1) 
    else 
     table.put(e, 0) 

在表中值爲0的for循環元素的末尾是不同的元素。

+0

oooo比我的好多了 – hvgotcodes 2011-05-17 00:34:17

+0

@hvgotcodes謝謝你。 – Enrique 2011-05-17 00:35:54

+0

不,謝謝,我學到了一些東西... – hvgotcodes 2011-05-17 00:37:01

10

如果我明白你的問題正確,則代碼如下方法nonOverLap下面應該讓你的是:

<T> Collection<T> union(Collection<T> coll1, Collection<T> coll2) { 
    Set<T> union = new HashSet<>(coll1); 
    union.addAll(new HashSet<>(coll2)); 
    return union; 
} 

<T> Collection<T> intersect(Collection<T> coll1, Collection<T> coll2) { 
    Set<T> intersection = new HashSet<>(coll1); 
    intersection.retainAll(new HashSet<>(coll2)); 
    return intersection; 
} 

<T> Collection<T> nonOverLap(Collection<T> coll1, Collection<T> coll2) { 
    Collection<T> result = union(coll1, coll2); 
    result.removeAll(intersect(coll1, coll2)); 
    return result; 
} 
2
import java.util.ArrayList; 
import java.util.Collection; 
import java.util.HashSet; 
import java.util.List; 
import java.util.Set; 

public class CompareTwoList { 
    public CompareTwoList() { 
     // TODO Auto-generated constructor stub 
    } 

    public static void main(String[] args) { 
     List<String> ls1 = new ArrayList<String>(); 
     ls1.add("a"); 
     ls1.add("b"); 
     ls1.add("c"); 
     ls1.add("d"); 

     List<String> ls2 = new ArrayList<String>(); 
     ls2.add("a"); 
     ls2.add("b"); 
     ls2.add("c"); 
     ls2.add("d"); 
     ls2.add("e"); 

     Set<String> set1 = new HashSet<String>(); 
     set1.addAll(ls1); 

     Set<String> set2 = new HashSet<String>(); 
     set2.addAll(ls2); 
     set2.removeAll(set1); 

     //set.addAll(ls1); 
     //set.addAll(ls1); 

     for (String diffElement : set2) { 
      System.out.println(diffElement.toString()); 
     } 
    } 
}  
+0

完善和簡化,適用於所有情況。謝謝 – 2015-03-26 09:14:31

9

使用Apache Commons Collectionsjavadoc):

CollectionUtils.disjunction(a, b); 

參見:Effective Java,2nd editionItem 47:知道並使用庫(作者僅提及JDK的內置庫,但我認爲其他庫的推理也可能如此)。

0

調用傳遞兩個數組列表的方法ReturnArrayListDiffElements。將返回一個數組列表,它是兩個通過的數組列表之間的區別將被返回

public ArrayList ReturnArrayListDiffElements(ArrayList arrList1, ArrayList arrList2){ 
    ArrayList<String> List1 = new ArrayList<String>(); 
    ArrayList<String> List2 = new ArrayList<String>(); 
    ArrayList<String> List3 = new ArrayList<String>(); 
    ArrayList<String> List4 = new ArrayList<String>(); 

    List1.addAll(arrList1);  
    List2.addAll(arrList2); 

    List3 = ReturnArrayListCommonElements(List1,List2); 

    List1.removeAll(List3);  
    List2.removeAll(List3);  
    if(List1.size() > 0){ 
     List4.add("Distinct elements in Array List 1");  
     List4.addAll(List1);  
    } 
    if(List2.size() > 0){  
     List4.add("Distinct elements in Array List 2"); 
     List4.addAll(List2);  
    } 

    return List4; 
} 

public ArrayList ReturnArrayListCommonElements(ArrayList arrList1, ArrayList arrList2){  
    ArrayList<String> List1 = new ArrayList<String>(); 
    ArrayList<String> List2 = new ArrayList<String>(); 
    ArrayList<String> List1A = new ArrayList<String>();  
    ArrayList<String> List2A = new ArrayList<String>();  
    ArrayList<String> List1B = new ArrayList<String>();  
    ArrayList<String> List3 = new ArrayList<String>(); 

    List1.addAll(arrList1);  
    List2.addAll(arrList2);   
    List1A.addAll(arrList1);  
    List2A.addAll(arrList2);  
    List1B.addAll(arrList1); 

    int intList1Size, intList2Size;  
    List1.removeAll(List2); 
    intList1Size = List1.size(); 

    List2.removeAll(List1A);  
    intList2Size = List2.size(); 

    if (intList1Size == 0 && intList2Size ==0) {   
     List3.addAll(List1B);  
     return List3; 
    } else { 
     List3.addAll(List1B);  
     List1B.removeAll(List2A);  
     List3.removeAll(List1B);   
     return List3; 
    } 
} 
2

這取決於你想要檢查什麼。

  1. 如果你想獲得兩個列表中的所有獨特的元素(即是第一列表獨特的全要素即和,所有元素的獨特第二列表)也被稱爲symmetric difference可以使用上面脫節方法如前所述從Apache Commons Collections 4.0

    CollectionUtils.disjunction(a, b); 
    
  2. 如果你想只從一個列表得到所有獨特的元素(只存在一個列表,即元素,但在其他不存在)阿爾斯Ø稱爲relative complement你可以從這個名單中減去方法從Apache Commons Collections 4.0使用減去另一個:

    CollectionUtils.subtract(a, b); //gives all unique elements of a that don't exist in b 
    
相關問題