forked from TestLeafPages/JavaPrograms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntersection.java
More file actions
47 lines (34 loc) · 1.01 KB
/
Intersection.java
File metadata and controls
47 lines (34 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package coding;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.testng.annotations.Test;
public class Intersection {
int[] arr1 = {3,2,11,4,6,7};
int[] arr2 = {1,2,8,4,9,7};
@Test(priority=1)
public void intersectionBetweenArrays() throws IOException {
List<Integer> arrayList = new ArrayList<Integer>();
for(int i=0;i<arr1.length;i++) {
for(int j=0;j<arr2.length;j++) {
if(arr1[i]==arr2[j]) {
arrayList.add(arr2[j]);
}
}
}
Collections.sort(arrayList);
System.out.println("Intersection of two arrays are: "+arrayList);
}
@Test(priority=2)
public void intersectionUsingSet() throws IOException {
List<Integer> firstList = new ArrayList<Integer>();
for (int i : arr1)
firstList.add(i);
List<Integer> secondList = new ArrayList<Integer>();
for (int i : arr2)
secondList.add(i);
firstList.retainAll(secondList);
System.out.println(firstList);
}
}