Finding various elements between two ArrayLists in Java

How to recognize a different element between two lists of arrays in java? I need an exact element, not a boolean, which can be obtained with removeAll() .

+8
java arraylist
source share
6 answers

If I understand your question correctly, then the following nonOverLap code in the code below should get the following:

 <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; } 
+10
source share

Use the Apache Community Collections ( javadoc ):

 CollectionUtils.disjunction(a, b); 

See also: Effective Java, 2nd edition, paragraph 47: Know and use libraries (the author mentions only the built-in JDK libraries, but I think that the argument can be true for other libraries as well.)

+10
source share

It depends on what you want to check.

  • If you want to get all unique elements for both lists (i.e. the sum of all elements that are unique to the first list and all elements that are unique to the second list), also known as symmetric difference, you can use, as mentioned above, the disjunction method from Apache Commons Collections 4.0 :

     CollectionUtils.disjunction(a, b); 
  • If you want to get all unique elements from only one list (i.e., elements that exist only in one list but do not exist in another), also known as relative addition , you can subtract another method of subtracting from Apache Commons from this list Collections 4.0 :

     CollectionUtils.subtract(a, b); //gives all unique elements of a that don't exist in b 
+3
source share
 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 elements of the for loop, different ones are used in the table with value = 0.

+1
source share
 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()); } } } 
+1
source share

Call the ReturnArrayListDiffElements method, passing two lists of arrays. A list of arrays, which is the difference between the two lists of forwarded arrays, 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
source share

All Articles