简体   繁体   English

在Java中查找两个ArrayLists之间的不同元素

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

How can I know the different element between 2 array list in java? 我如何知道java中2个数组列表之间的不同元素? I need the exact element not a Boolean value which can be retrieved using removeAll() . 我需要确切的元素而不是布尔值,可以使用removeAll()来检索。

Use Apache Commons Collections ( javadoc ): 使用Apache Commons Collectionsjavadoc ):

CollectionUtils.disjunction(a, b);

See also: Effective Java, 2nd edition , Item 47: Know and use the libraries (The author mentions only the JDK's built-in libraries but I think the reasoning could be true for other libraries too.) 另请参阅: Effective Java,第2版第47项:了解和使用库 (作者仅提到了JDK的内置库,但我认为其他库的推理也是如此。)

If I understood your question correctly then following method nonOverLap in the code below should get you that: 如果我正确地理解了你的问题,那么在下面的代码中跟随方法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;
}

It depends on what do you want to check. 这取决于你想要检查什么。

  1. If you want to get all the unique elements for both lists (ie sum of all elements that are unique for the first list and all elements that unique for the second list) also known as symmetric difference you can use as mentioned above disjunction method from Apache Commons Collections 4.0 : 如果要获取两个列表的所有唯一元素(即第一个列表中唯一的所有元素的总和以及第二个列表中唯一的所有元素的总和),也称为对称差异,您可以使用如上所述的Apache的 disjunction方法Commons Collections 4.0

     CollectionUtils.disjunction(a, b); 
  2. If you want to get all unique elements only from one list (ie elements that exist only in one list, but don't exist in the other) also known as relative complement you can subtract from this list the other one using subtract method from Apache Commons Collections 4.0 : 如果你想只从一个列表中获取所有唯一元素(即只存在于一个列表中但在另一个列表中不存在的元素),也称为相对补码 ,则可以使用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 )

At the end of the for loops elements in table with value=0 are the different ones. 在for循环结束时,表中值为0的元素是不同的元素。

Call the method ReturnArrayListDiffElements passing two array lists. 调用ReturnArrayListDiffElements方法传递两个数组列表。 An array list which is the difference between two passed array lists will be returned 将返回一个数组列表,它是两个传递的数组列表之间的差异

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