Java Utililty Methods Array Intersect

List of utility methods to do Array Intersect

Description

The list of methods to do Array Intersect are organized into topic(s).

Method

String[]intersect(String[] list1, String[] list2)
Returns intersections of two list.
if (null == list1 || 0 == list1.length || null == list2 || 0 == list2.length) {
    return null;
Map<String, Boolean> map1 = new HashMap<String, Boolean>();
for (String s : list1) {
    map1.put(s, false);
int counter = 0;
...
T[]intersect(T[] a, T[] b)
Create an intersection of two arrays, removing null values and duplicates
ArrayList<T> l = new ArrayList<T>();
for (int i = 0; i < a.length; i++) {
    if (a[i] == null)
        continue;
    for (int j = 0; j < b.length; j++) {
        if (b[j] == null)
            continue;
        if (a[i].equals(b[j])) {
...
Object[]intersect(T[] a, T[] b)
Finds the intersection of two sorted arrays.
if (a.length == 0 || b.length == 0)
    return new Object[0];
List<T> ret = new ArrayList<>();
int i = 0;
for (T val : a) {
    while (i < b.length && val.compareTo(b[i]) > 0) {
        ++i;
    if (i < b.length && val.compareTo(b[i]) == 0) {
        ret.add(val);
        ++i;
return ret.toArray();
intintersectArrays(int[] a, int[] b)
Returns the size of the intersection of two SORTED arrays.
int intersectCount = 0;
int aIndex = 0;
int bIndex = 0;
while (aIndex < a.length && bIndex < b.length) {
    if (a[aIndex] == b[bIndex]) {
        intersectCount++;
        aIndex++;
        bIndex++;
...
booleanintersectArrays(int[] targetArray, int[] selfArray)
intersect Arrays
for (int i : targetArray) {
    for (int j : selfArray) {
        if (i == j) {
            return true;
return false;
...
long[]intersection(final long[] array1, final long[] array2)
intersection
if (array1 == null || array2 == null)
    return new long[0];
final List<Long> list1 = new ArrayList<Long>();
for (final long item : array1) {
    list1.add(item);
final List<Long> list2 = new ArrayList<Long>();
for (final long item : array2) {
...
int[]intersection(int[] array1, int[] array2)
intersection
int[] intersection = new int[array1.length < array2.length ? array1.length : array2.length];
int k = 0;
if (array1.length > 0 && array2.length > 0) {
    for (int i = 0, j = 0; i < array1.length && j < array2.length;) {
        if (array1[i] == array2[j]) {
            intersection[k] = array1[i];
            k++;
            i++;
...
String[]intersection(String[] ary1, String[] ary2)
intersection
List<String> list1 = new ArrayList<String>(Arrays.asList(ary1));
List<String> list2 = new ArrayList<String>(Arrays.asList(ary2));
list1.retainAll(list2);
return list1.toArray(new String[0]);
String[]intersection(String[] ss1, String[] ss2)
intersection
int l = 0;
for (int i = 0; i < ss1.length; i++)
    if (contains(ss2, ss1[i]))
        ss1[l++] = ss1[i];
return l == ss1.length ? ss1 : Arrays.copyOf(ss1, l);
int[]intersectRanges(int[] range1, int[] range2)
Each array represents a set of closed or open Range, like so: [0,10,50,60] - Ranges are {0-9}, {50-59} [20] - Ranges are {20-} [30,40,100] - Ranges are {30-39}, {100-} All Ranges in the array have a closed lower bound.
if (range1 == null) {
    return range2;
} else if (range2 == null) {
    return range1;
int[] outputRange = new int[range1.length + range2.length];
int outputIndex = 0;
int index1 = 0, lower1, upper1;
...