How to do union, intersect, difference and reverse data in java
//Union
List<Integer> c = new ArrayList<Integer>(a.size() + b.size());
addNoDups(c,a);
addNoDups(c,b);
private void addNoDups(List<Integer> toAddTo,List<Integer> iterateOver) {
for(Integer num:iterateOver){
if(toAddTo.indexOf(num) == -1) {
toAddTo.add(num);
}
}
}
//intersection
List<Integer> c = new ArrayList<Integer> (a.size() > b.size() ?a.size():b.size());
c.addAll(a);
c.retainAll(b);
//difference a-b
List<Integer> c = new ArrayList<Integer> (a.size());
c.addAll(a);
c.removeAll(b);
Intersection and union of ArrayLists in Java
Here's a plain implementation without using any third-party library. Main advantage over retainAll
, removeAll
and addAll
is that these methods don't modify the original lists input to the methods.
public class Test {
public static void main(String... args) throws Exception {
List<String> list1 = new ArrayList<String>(Arrays.asList("A", "B", "C"));
List<String> list2 = new ArrayList<String>(Arrays.asList("B", "C", "D", "E", "F"));
System.out.println(new Test().intersection(list1, list2));
System.out.println(new Test().union(list1, list2));
}
public <T> List<T> union(List<T> list1, List<T> list2) {
Set<T> set = new HashSet<T>();
set.addAll(list1);
set.addAll(list2);
return new ArrayList<T>(set);
}
public <T> List<T> intersection(List<T> list1, List<T> list2) {
List<T> list = new ArrayList<T>();
for (T t : list1) {
if(list2.contains(t)) {
list.add(t);
}
}
return list;
}
}
How to compare two arrays in java and return an element that isn't included in two list
for those kind of operation it would be better to use collections,
the method removeAll() will filter the data containers, from the doc:
Removes from this list all of its elements that are contained in the
specified collection (optional operation).
List<Integer> myVarListA = Arrays.asList(26, 13, 88, 9);
List<Integer> myVarListB = Arrays.asList(26, 1, 8, 12);
List<Integer> myVarListAcomplementB = new ArrayList<>(myVarListA);
List<Integer> myVarListBcomplementA = new ArrayList<>(myVarListB);
myVarListAcomplementB.removeAll(myVarListB);
myVarListBcomplementA.removeAll(myVarListA);
System.out.println("elements in A but no in B: " + myVarListAcomplementB);
System.out.println("elements in B but no in A: " + myVarListBcomplementA);
myVarListBcomplementA.addAll(myVarListAcomplementB);
System.out.println("both together: " + myVarListBcomplementA);
List difference in java
List<Integer> original = Arrays.asList(12,16,17,19,101);
List<Integer> selected = Arrays.asList(16,19,107,108,109);
ArrayList<Integer> add = new ArrayList<Integer>(selected);
add.removeAll(original);
System.out.println("Add: " + add);
ArrayList<Integer> remove = new ArrayList<Integer>(original);
remove.removeAll(selected);
System.out.println("Remove: " + remove);
Output:
Add: [107, 108, 109]
Remove: [12, 17, 101]
Uses Collection's removeAll method. See javadocs.
Union of N lists in java
You could use Google Guava:
List<Integer> joined = new ArrayList<>( Iterables.concat(LIST_1, LIST_2, LIST_3, LIST_4) );
or for comparison only:
Iterables.elementsEqual( LIST_1_2_3_4, Iterables.concat(LIST_1, LIST_2, LIST_3, LIST_4) );
Classical set operations for java.util.Collection
Intersection is done with Collection.retainAll
; subtraction with Collection.removeAll
; union with Collection.addAll
. In each case, as Set
will act like a set and a List
will act like a list.
As mutable objects, they operate in place. You'll need to explicitly copy if you want to retain the original mutable object unmutated.
Removing duplicates from arraylist using set
- Find the intersection
- Find the union
- Subtract the intersection from the union
Code:
public static void main(String[] args) {
Set<Integer> set1 = new HashSet<Integer>(Arrays.asList(1, 2, 3, 4, 5));
Set<Integer> set2 = new HashSet<Integer>(Arrays.asList(1, 3, 6, 7));
Set<Integer> intersection = new HashSet<Integer>(set1);
intersection.retainAll(set2);
// set1 is now the union of set1 and set2
set1.addAll(set2);
// set1 is now (union - intersection)
// All elements in set1 or set2, but not in both set1 & set2
set1.removeAll(intersection);
for(Integer n : set1) {
System.out.println(n);
}
}
Output:
2
4
5
6
7
Related Topics
How to Print Line Numbers to the Log in Java
Shared Memory Between Two Jvms
Setting Jvm Heap Size at Runtime
How to Flatten 2D Array to 1D Array
Gmail Login Using Selenium Webdriver in Java
Difference Between & and && in Java
How to Create a Topic in Kafka from the Ide Using API
How to Connect to a SQL Server 2008 Database Using Jdbc
Sending Email via Gmail Smtp Server in Java
Timer & Timertask Versus Thread + Sleep in Java
Gradle: Could Not Determine Java Version from '11.0.2'
Why Does Eclipse Complain About @Override on Interface Methods
Use Custom Fonts When Creating PDF Using Ireport
Bug with Varargs and Overloading