Finding the different elements between two ArrayLists in Java

后端 未结 6 1275
滥情空心
滥情空心 2021-02-06 05:43

How can I know the different element between 2 array list in java? I need the exact element not a Boolean value which can be retrieved using removeAll().

相关标签:
6条回答
  • 2021-02-06 06:05
    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 讨论(0)
  • 2021-02-06 06:10
    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.

    0 讨论(0)
  • 2021-02-06 06:15

    If I understood your question correctly then following method nonOverLap in the code below should get you that:

    <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;
    }
    
    0 讨论(0)
  • 2021-02-06 06:16

    Call the method ReturnArrayListDiffElements passing two array lists. 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;   
        }
    }
    
    0 讨论(0)
  • 2021-02-06 06:18

    It depends on what do you want to check.

    1. If you want to get all the unique elements for both lists (i.e. 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:

      CollectionUtils.disjunction(a, b);
      
    2. If you want to get all unique elements only from one list (i.e. 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:

      CollectionUtils.subtract(a, b); //gives all unique elements of a that don't exist in b
      
    0 讨论(0)
  • 2021-02-06 06:24

    Use Apache Commons Collections (javadoc):

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

    0 讨论(0)
提交回复
热议问题