Java List Intersect intersectAll(final List collector, final T[] a, final T[] b)

Here you can find the source of intersectAll(final List collector, final T[] a, final T[] b)

Description

Intersects the two given arrays in bag logic.

License

Apache License

Parameter

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

Declaration

public static <T extends Comparable<T>> void intersectAll(final List<T> collector, final T[] a, final T[] b) 

Method Source Code

//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++;
                }
            }
        }
    }
}

Related

  1. intersect(List list1, List list2)
  2. intersect(List... lists)
  3. intersect(List in1, List in2)
  4. intersect(Set a, List b)
  5. intersect2orderedList(List s1, List s2)
  6. intersectDate(List oDate, List tDate)
  7. intersection(final List list1, final List list2)
  8. intersection(final List list1, final List list2)
  9. intersection(final List list1, final List list2)