簡體   English   中英

在Java中查找兩個ArrayLists之間的不同元素

[英]Finding the different elements between two ArrayLists in Java

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

使用Apache Commons Collectionsjavadoc ):

CollectionUtils.disjunction(a, b);

另請參閱: Effective Java,第2版第47項:了解和使用庫 (作者僅提到了JDK的內置庫,但我認為其他庫的推理也是如此。)

如果我正確地理解了你的問題,那么在下面的代碼中跟隨方法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;
}

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

  1. 如果要獲取兩個列表的所有唯一元素(即第一個列表中唯一的所有元素的總和以及第二個列表中唯一的所有元素的總和),也稱為對稱差異,您可以使用如上所述的Apache的 disjunction方法Commons Collections 4.0

     CollectionUtils.disjunction(a, b); 
  2. 如果你想只從一個列表中獲取所有唯一元素(即只存在於一個列表中但在另一個列表中不存在的元素),也稱為相對補碼 ,則可以使用Apache中的 減法方法從該列表中減去另一個列表Commons Collections 4.0

     CollectionUtils.subtract(a, b); //gives all unique elements of a that don't exist in b 
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());
        }
    }
}    
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 )

在for循環結束時,表中值為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;   
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM