Here you can find the source of intersectAll(final List
Parameter | Description |
---|---|
collector | is the Collection to which the intersected element will be written |
a | is the first union operand |
b | is the second union operand |
public static <T extends Comparable<T>> void intersectAll(final List<T> collector, final T[] a, final T[] b)
//package com.java2s; //License from project: Apache License import java.util.*; public class Main { /**// w w w . j a v a 2 s .c o m * Intersects the two given arrays in bag logic. The two arrays must be sorted and the intersection elements will be * added in sorted order to the given collector. * * @param collector * is the {@link Collection} to which the intersected element will be written * @param a * is the first union operand * @param b * is the second union operand */ public static <T extends Comparable<T>> void intersectAll(final List<T> collector, final T[] a, final T[] b) { int index1 = 0; int index2 = 0; while (true) { if (index1 >= a.length || index2 >= b.length) { break; } else { final T candidate1 = a[index1]; final T candidate2 = b[index2]; final int comparison = candidate1.compareTo(candidate2); if (comparison < 0) { index1++; } else if (comparison > 0) { index2++; } else { collector.add(candidate2); index1++; index2++; } } } } }